
Example 7−2: An Applet That Uses the Java 1.0 Event Model (continued)
this.requestFocus(); // Ask for keyboard focus so we get key events
}
/** Respond to mouse clicks */
public boolean mouseDown(Event e, int x, int y) {
lastx = x; lasty = y; // Remember where the click was
return true;
}
/** Respond to mouse drags */
public boolean mouseDrag(Event e, int x, int y) {
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;
}
/** Respond to key presses: erase drawing when user types 'e' */
public boolean keyDown(Event e, int key) {
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;
}
/** Respond to button clicks: erase drawing when user clicks button */
public boolean action(Event e, Object arg) {
if (e.target = = erase_button) {
g.setColor(this.getBackground());
g.fillRect(0, 0, bounds().width, bounds().height);
return true;
}
else return false;
}
}
Inc luding Applets in HTML Files
Applets are typically embedded in HTML files using the <APPLET> tag. Another
alter native relies on a Java Plug-in and uses the
<EMBED> and <OBJECT> tags. Multi-
ple applet files can be combined into a single JAR (Java Archive) file that a web
br owser can read as a single, compressed file, substantially reducing download ...