FileDocCategorySizeDatePackage
ScribblePane2.javaAPI DocExample3429Sat Jan 24 10:44:32 GMT 2004je3.gui

ScribblePane2

public class ScribblePane2 extends JPanel
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
These are the coordinates of the the previous mouse position
protected int
last_y
Color
color
This field holds the current drawing color property
Constructors Summary
public ScribblePane2()

	// Give the component a preferred size
	setPreferredSize(new Dimension(450,200));

	// Register a mouse event handler defined as an inner class
	// Note the call to requestFocus().  This is required in order for
	// the component to receive key events.
	addMouseListener(new MouseAdapter() {
		public void mousePressed(MouseEvent e) { 
		    moveto(e.getX(), e.getY());  // Move to click position
		    requestFocus();              // Take keyboard focus
		}
	    });

	// Register a mouse motion event handler defined as an inner class
	// By subclassing MouseMotionAdapter rather than implementing
	// MouseMotionListener, we only override the method we're interested
	// in and inherit default (empty) implementations of the other methods.
	addMouseMotionListener(new MouseMotionAdapter() {
		public void mouseDragged(MouseEvent e) {
		    lineto(e.getX(), e.getY());  // Draw to mouse position
		}
	    });

	// Add a keyboard event handler to clear the screen on key 'C'
	addKeyListener(new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
		    if (e.getKeyCode() == KeyEvent.VK_C) clear();
		}
	    });
    
Methods Summary
public voidclear()
Clear the drawing area, using the component background color. This method works by requesting that the component be redrawn. Since this component does not have a paintComponent() method, nothing will be drawn. However, other parts of the component, such as borders or sub-components will be drawn correctly.

 repaint(); 
public java.awt.ColorgetColor()
This is the property "getter" method for the color property

 return color; 
public voidlineto(int x, int y)
Draw from the last point to this point, then remember new point

	Graphics g = getGraphics();          // Get the object to draw with
	g.setColor(color);                   // Tell it what color to use
	g.drawLine(last_x, last_y, x, y);    // Tell it what to draw
	moveto(x, y);                        // Save the current point
    
public voidmoveto(int x, int y)
Remember the specified point

	last_x = x;
	last_y = y;
    
public voidsetColor(java.awt.Color color)
This is the property "setter" method for the color property

 
               
         this.color = color;