Scribblepublic 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 |
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) {
Graphics g = getGraphics();
g.setColor(this.getBackground());
g.fillRect(0, 0, bounds().width, bounds().height);
return true;
}
else return false;
| public void | init()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 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")) {
Graphics g = getGraphics();
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
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;
|
|