//******************************************************************** // DoodleCanvas.java Author: Lewis and Loftus // // Represents a drawing surface for creating simple doodles. //******************************************************************** import java.awt.*; import java.awt.event.*; class DoodleCanvas extends Canvas implements MouseListener, MouseMotionListener { private final int CANVAS_WIDTH = 200; private final int CANVAS_HEIGHT = 200; private int lastX, lastY; //----------------------------------------------------------------- // Creates an initially empty canvas. //----------------------------------------------------------------- public DoodleCanvas () { addMouseListener (this); addMouseMotionListener (this); setBackground (Color.white); setSize (CANVAS_WIDTH, CANVAS_HEIGHT); } //----------------------------------------------------------------- // Determines up the initial point for a new doodle line. //----------------------------------------------------------------- public void mousePressed (MouseEvent event) { Point first = event.getPoint(); lastX = first.x; lastY = first.y; } //----------------------------------------------------------------- // Draws a line from the last point to the current point. //----------------------------------------------------------------- public void mouseDragged (MouseEvent event) { Point current = event.getPoint(); Graphics page = getGraphics(); page.drawLine (lastX, lastY, current.x, current.y); lastX = current.x; lastY = current.y; } //----------------------------------------------------------------- // Clears the canvas. //----------------------------------------------------------------- public void clear () { Graphics page = getGraphics(); page.drawRect (0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); repaint(); } //----------------------------------------------------------------- // Provide empty definitions for unused event methods. //----------------------------------------------------------------- public void mouseReleased (MouseEvent event) {} public void mouseClicked (MouseEvent event) {} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} }