 
/****************************************************************
/*  DoodleCanvasIC2.java       Authors: Lewis and Loftus
/*                             Updated: Bary W Pollack
/*
/*  A DoodleCanvasIC2 is a drawing surface for creating doodles.
/*  Demonstrates use of inner classes and adapters.
/***************************************************************/

import java.awt.*;
import java.awt.event.*;

class DoodleCanvasIC2 extends Canvas {
    //------------------------------------------------------------
    //  Creates an initially empty canvas.
    //------------------------------------------------------------
    public DoodleCanvasIC2() {
        addMouseListener(new MouseHandler());
        addMouseMotionListener(new MouseMotionHandler());

        setBackground(Color.white);
        setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
    }

    class MouseHandler extends MouseAdapter {
        //------------------------------------------------------------
        //  Sets up the initial point for a new doodle line.
        //------------------------------------------------------------
        public void mousePressed(MouseEvent event) {
            Point first = event.getPoint();
            lastX = first.x;
            lastY = first.y;
        }
    }

    class MouseMotionHandler extends MouseMotionAdapter {
        //------------------------------------------------------------
        //  Draws a line from the last point to the current point.
        //------------------------------------------------------------
        public void mouseDragged(MouseEvent event) {
            Point current = event.getPoint();
    
            Graphics g = getGraphics();
            g.drawLine(lastX, lastY, current.x, current.y);
    
            lastX = current.x;
            lastY = current.y;
        }
    }

    //------------------------------------------------------------
    //  Clears the canvas.
    //------------------------------------------------------------
    public void clear() {
        Graphics g = getGraphics();
        g.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
        repaint();
    }

    private final int CANVAS_WIDTH  = 200;
    private final int CANVAS_HEIGHT = 200;
    private int lastX, lastY;
}
