// WebSitesApplet.java
// Applet to display a set of buttons on the screen that
// allow the user to access a selection of web sites.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;

public class WebSitesApplet
    extends Applet
    implements ActionListener
{
    // Initialize an array with the names of the Web sites
    String[] webSiteNames =
        {"Oxford Brookes University",
         "Oxford Computing Laboratory",
         "Jones & Bartlett",
         "Java Development",
         "Windows 95 utilities"};

    // Initialize an array with the corresponding URLs of those Web sites
    String[] webURLs =
        {"http://www.brookes.ac.uk",
         "http://www.comlab.ox.ac.uk",
         "http://www.jbpub.com",
         "http://java.sun.com",
         "http://www.windows95.com"};

    // Array of buttons that will denote the different Web sites
    Button[] button = new Button[webSiteNames.length];

    // Override method init to display the group of buttons
    public void init()
    {
        setLayout(null);
        setBackground(Color.yellow);
        for (int index=0; index!=webSiteNames.length; index++)
        {
            button[index] = new Button(webSiteNames[index]);
            button[index].setLocation(10, 25*index);
            button[index].setSize(200, 20);
            button[index].setBackground(Color.cyan);
            button[index].addActionListener(this);
            add(button[index]);
        }
    }

    // Implement the actionPerformed method
    // in the ActionListener interface
    public void actionPerformed(ActionEvent event)
    {
        // Find the name of the button that was pressed
        Object source = event.getActionCommand();
        for (int index=0; index != webSiteNames.length; index++)
        {
            // Inspect each web site name in the array of Web site names
            if (source.equals(webSiteNames[index]))
            {
                // Display the relevant page from the Web site
                // if and when a match is found
                try
                {
                    URL site = new URL(webURLs[index]);
                    getAppletContext().showDocument(site);
                }
                catch (MalformedURLException m)
                {
                    System.exit(1);
                }
                return;
            }
        }
    }
}
