public class ScribblePane1 extends JPanel implements MouseListener, MouseMotionListener
A simple JPanel subclass that uses event listeners to allow the user
to scribble with the mouse. Note that scribbles are not saved or redrawn.
Fields Summary
protected int
last_x
protected int
last_y
Constructors Summary
public ScribblePane1()
// This component registers itself as an event listener for
// mouse events and mouse motion events.
this.addMouseListener(this);
this.addMouseMotionListener(this);
// Give the component a preferred size
setPreferredSize(new Dimension(450,200));
int x = e.getX(); // Get the current mouse position
int y = e.getY();
// Draw a line from the saved coordinates to the current position
this.getGraphics().drawLine(last_x, last_y, x, y);
last_x = x; // Remember the current position
last_y = y;