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

    // This class adds KeyListener functionality to MouseDrawing2.java
public class MouseDrawing3 extends Frame implements ActionListener {
    // The labels for the buttons.
    static final String clearString = "Clear";
    static final String quitString = "Quit";

    public MouseDrawing3(){
        final int width = 400, height = 400;
        setSize(400,400);
        setBackground(Color.white);

        Panel panel = new Panel();
        // Add the buttons ...
        panel.add(newButton(clearString));
        panel.add(newButton(quitString));
        add(panel,"South");
        // ... and the drawing area.
        ClearableCanvas canvas = getCanvas();
        add(canvas,"Center");
        // Setup up an object to draw on the canvas.
        new MouseResponder(canvas);
        // Add a key listener to the canvas.
        KeyResponder keyboard = new KeyResponder(canvas);
        canvas.addKeyListener(keyboard);
    }

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

        if(command.equals(clearString)){
            // Clear the canvas.
            getCanvas().clear();
        }
        else if(command.equals(quitString)){
            System.exit(0);
        }
        else{
            System.err.println("Unknown command: "+e);
        }
    }

    // Create a new button with label and command.
    // Use the label as the associated command.
    protected Button newButton(String label){
        Button b = new Button(label);
        b.addActionListener(this);
        b.setActionCommand(label);
        return b;
    }

    protected ClearableCanvas getCanvas(){
        return canvas;
    }

    // Where to draw.
    private ClearableCanvas canvas = new ClearableCanvas();
}
