import java.awt.*;
import java.awt.event.*;

        // Draw an oval and allow its colour to be set
        // via three buttons.
class PanelFrame extends Frame implements ActionListener {
    public PanelFrame(){
        final int width = 200, height = 200;
        setSize(width,height);
        setBackground(Color.white);

        // Create a panel for the three buttons.
        Panel buttonPanel = setupButtonPanel();
        // Add the panel to the Frame
        add(buttonPanel,"North");

        // ... and the drawing area.
        add(getCanvas(),"Center");

        // Add the quit button directly to the Frame.
        Button quitButton = new Button(quitString);
        quitButton.addActionListener(this);
        quitButton.setActionCommand(quitString);
        add(quitButton,"South");
    }

    // Work out which button was pressed.
    public void actionPerformed(ActionEvent e){
        String command = e.getActionCommand();

        if(command.equals(quitString)){
            System.exit(0);
        }
        else if(command == redString){
            getCanvas().setForeground(Color.red);
        }
        else if(command == blueString){
            getCanvas().setForeground(Color.blue);
        }
        else if(command == greenString){
            getCanvas().setForeground(Color.green);
        }
        else{
            System.err.println("Unknown command: "+e);
        }
        // Repaint the canvas in the current colour.
        getCanvas().repaint();
    }

        // Create a panel for the three buttons.
    protected Panel setupButtonPanel(){
        Panel buttonPanel = new Panel();
        // Add the three colour buttons to the panel.
        addButton(redString,buttonPanel);
        addButton(greenString,buttonPanel);
        addButton(blueString,buttonPanel);
        return buttonPanel;
    }

    // Add a new button with label and command s.
    // Use the label as the associated command.
    protected void addButton(String label, Panel buttonPanel){
        Button b = new Button(label);
        b.addActionListener(this);
        b.setActionCommand(label);
        buttonPanel.add(b);
    }

    protected Canvas getCanvas(){
        return canvas;
    }

    // Where to draw the oval.
    private OvalCanvas canvas = new OvalCanvas();
    // The labels for the buttons.
    private static final String quitString = "Quit", redString = "Red",
                                greenString = "Green", blueString = "Blue";
}
