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

        // In this version (compare MouseDrawing1.java) the mouse
        // listener methods are implemented by a separate
        // class from the ClearableCanvas class.
public class MouseDrawing2 extends Frame implements ActionListener {
    public MouseDrawing2(){
        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.
        add(getCanvas(),"Center");
        // Create an object to draw on the canvas.
        new MouseResponder(getCanvas());
    }

    // 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();
    // The labels for the buttons.
    private static final String clearString = "Clear";
    private static final String quitString = "Quit";
}
