FileDocCategorySizeDatePackage
Scribble.javaAPI DocExample2308Sat Jan 24 10:44:38 GMT 2004je3.applet

Scribble

public class Scribble extends Applet
This applet lets the user scribble with the mouse. It demonstrates the Java 1.0 event model.

Fields Summary
private int
lastx
private int
lasty
Button
erase_button
Constructors Summary
Methods Summary
public booleanaction(java.awt.Event e, java.lang.Object arg)
Respond to Button clicks: erase drawing when user clicks button

	if (e.target == erase_button) {
	    Graphics g = getGraphics();
	    g.setColor(this.getBackground());   
	    g.fillRect(0, 0, bounds().width, bounds().height);
	    return true;
	}
	else return false;
    
public voidinit()
Initialize the erase button, ask for keyboard focus

	erase_button = new Button("Erase");  
	this.add(erase_button);
	this.setBackground(Color.white);  // Set background color for scribble
	this.requestFocus();  // Ask for keyboard focus so we get key events
    
public booleankeyDown(java.awt.Event e, int key)
Respond to key presses: Erase drawing when user types 'e'

	if ((e.id == Event.KEY_PRESS) && (key == 'e")) {
	    Graphics g = getGraphics();
	    g.setColor(this.getBackground());   
	    g.fillRect(0, 0, bounds().width, bounds().height);
	    return true;
	}
	else return false;
    
public booleanmouseDown(java.awt.Event e, int x, int y)
Respond to mouse clicks

	lastx = x; lasty = y;             // Remember where the click was
	return true;
    
public booleanmouseDrag(java.awt.Event e, int x, int y)
Respond to mouse drags

	Graphics g = getGraphics();
	g.drawLine(lastx, lasty, x, y);   // Draw from last position to here
	lastx = x; lasty = y;             // And remember new last position
	return true;