Java Reference
In-Depth Information
Example 15−3: Scribble.java (continued)
g.setColor(this.getBackground());
g.fillRect(0, 0, bounds().width, bounds().height);
return true;
}
else return false;
}
}
Java 1.0 Event Details
Example 15-4 shows an applet that handles all user input events that can occur in
an applet and displays the event details. These are mouse and keyboard events
primarily; the program does not define any GUI components, so it does not han-
dle the higher-level semantic events those components generate. This example is
interesting because it shows how to interpret modifiers and how to make sense of
the various types of key events. If you find yourself writing complex event han-
dling code, you may want to model pieces of it after this example.
Example 15−4: EventTester.java
package com.davidflanagan.examples.applet;
import java.applet.*;
import java.awt.*;
import java.util.*;
/** An applet that gives details about Java 1.0 events */
public class EventTester extends Applet {
// Handle mouse events
public boolean mouseDown(Event e, int x, int y) {
showLine(mods(e.modifiers) + "Mouse Down: [" + x + "," + y + "]");
return true;
}
public boolean mouseUp(Event e, int x, int y) {
showLine(mods(e.modifiers) + "Mouse Up: [" + x + "," + y + "]");
return true;
}
public boolean mouseDrag(Event e, int x, int y) {
showLine(mods(e.modifiers) + "Mouse Drag: [" + x + "," + y + "]");
return true;
}
public boolean mouseMove(Event e, int x, int y) {
showLine(mods(e.modifiers) + "Mouse Move: [" + x + "," + y + "]");
return true;
}
public boolean mouseEnter(Event e, int x, int y) {
showLine("Mouse Enter: [" + x + "," + y + "]"); return true;
}
public boolean mouseExit(Event e, int x, int y) {
showLine("Mouse Exit: [" + x + "," + y + "]"); return true;
}
// Handle focus events
public boolean gotFocus(Event e, Object what) {
showLine("Got Focus"); return true;
}
public boolean lostFocus(Event e, Object what) {
Search WWH ::




Custom Search