import javax.swing.*; import javax.swing.plaf.*; import java.awt.*; import java.awt.event.*; import java.util.*; /** * DrawPanel implements a JPanel that can be drawn on using * the mouse * * @author Stuart Hansen * @version September 28, 2008 */ // We specialize the JPanel to contain a drawing. public class DrawPanel extends JPanel { // The model used for the JPanel is a list of curves. ArrayList listOfCurves; // Two additional state variables to aid us in creating the drawing Curve currentCurve; // the current curve being drawn Color currentColor; // the current drawing color public DrawPanel() { super(); // Initialize the model reset(); // replace the view setUI (new DrawPanelUI()); // add specialized control addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); } // A setter for the color public void setColor(Color col) { currentColor = col; } // a getter for the color public Color getColor() { return currentColor; } // reset the drawPanel model public void reset() { listOfCurves = new ArrayList(); currentColor = Color.BLACK; repaint(); } // Each curve has a color and a list of points. // The points form a series of line segments, so it is really a poly-Line, // not a true curve. // We use an inner class to model the curve. private class Curve { private Color color; // the color of the curve private ArrayList points; // the points on the curve // the constructor initializes the color and the list public Curve (Color c, Point p) { color = c; points = new ArrayList(); points.add(p); } // get the Color Color getColor() { return color; } // returns an iterator over the points public Iterator iterator() { return points.iterator(); } // adds a Point public void add (Point p) { points.add(p); } } // The DrawPanelUI class knows how to display the drawing private class DrawPanelUI extends ComponentUI { public void paint (Graphics g, JComponent c) { // We iterate across the list of curves, drawing each Iterator curveIterator = listOfCurves.iterator(); while (curveIterator.hasNext()) { // We iterate across each curve drawing it Curve curve = curveIterator.next(); Iterator pointIterator = curve.iterator(); // The first thing in a curve is its color g.setColor(curve.getColor()); // The remainder of the curve is a sequence of Points Point p1 = pointIterator.next(); while (pointIterator.hasNext()) { Point p2 = pointIterator.next(); g.drawLine((int)p1.getX(), (int)p1.getY(), (int)p2.getX(), (int)p2.getY()); p1 = p2; } } } } // The following event handlers are part of the JPanel's control // This class adds points to the current vector private class MouseMotionHandler extends MouseMotionAdapter{ public void mouseDragged (MouseEvent e){ if (SwingUtilities.isLeftMouseButton(e)){ currentCurve.add(e.getPoint()); repaint(); } } } // When the mouse is pressed a new curve is started private class MouseHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)){ currentCurve = new Curve(currentColor, e.getPoint()); currentCurve.add(e.getPoint()); listOfCurves.add(currentCurve); } } } }