Scribblepublic class Scribble extends Applet A simple applet using the Java 1.0 event handling model |
Fields Summary |
---|
private int | lastx | private int | lasty | Button | erase_button | Graphics | g |
Methods Summary |
---|
public boolean | action(java.awt.Event e, java.lang.Object arg)Respond to Button clicks: erase drawing when user clicks button
if (e.target == erase_button) {
g.setColor(this.getBackground());
g.fillRect(0, 0, bounds().width, bounds().height);
return true;
}
else return false;
| public void | init()Initialize the button and the Graphics object
erase_button = new Button("Erase");
this.add(erase_button);
g = this.getGraphics();
this.requestFocus(); // Ask for keyboard focus so we get key events
| public boolean | keyDown(java.awt.Event e, int key)Respond to key presses: Erase drawing when user types 'e'
if ((e.id == Event.KEY_PRESS) && (key == 'e")) {
g.setColor(this.getBackground());
g.fillRect(0, 0, bounds().width, bounds().height);
return true;
}
else return false;
| public boolean | mouseDown(java.awt.Event e, int x, int y)Respond to mouse clicks
lastx = x; lasty = y; // Remember where the click was
return true;
| public boolean | mouseDrag(java.awt.Event e, int x, int y)Respond to mouse drags
g.setColor(Color.black);
g.drawLine(lastx, lasty, x, y); // Draw from last position to here
lastx = x; lasty = y; // And remember new last position
return true;
|
|