Back to Examples of Using the JDK 1.1 AWT
JDK 1.1 Event Examples
This simple demo contains four applets
that get progressively better at handling events.
The final result is a simple-minded game.
Note:
You must use a 1.1 browser such as
HotJava
or the
JDK Applet Viewer
to view the event examples.
If you have trouble running the examples,
go here.
This code was used in Hans Muller's exposition
on 1.1 event handling,
which was part of the
AWT Advanced Tutorial
at JavaOne.
Note:
The following applets rely on 1.1 code.
They do not run in 1.0-based browsers.
You can run them in the latest
JDK Applet Viewer
or
HotJava.
Using Anonymous Classes to Implement an Event Listener
The red box at the bottom of the game screen tracks the mouse because
the panel that contains is handling MouseMotion
events. The handler is an anonymous class that implements
MouseMotionListener.
Using a Static Nested Class to Implement an Event Listener
Each column of boxes, the targets, at the top of the game
screen is a Panel. By adding a listener that handles enter/exit
events to each panel we can make it highlight when the mouse
passes over. In this case we've used instances of a static
listener class to handle the enter/exit events. Unfortunately
there's a bug here: the red "puck" isn't tracking the mouse
correctly anymore!
Redispatching Events to Another Component
The target panels are consuming (and discarding) the MouseMotion
events that should be delivered to the game Panel. To redispatch
the events we translate their coordinates to the Panels origin
and pass them along with dispatchEvent()
.
Complete Example
Click the mouse as many times as there are boxes in a column to
get a satisfying "beep" and to make the boxes disappear. To win
the game clear the entire panel. If you find yourself playing
this game more than once, seek professional help immediately.
Hans Muller
Last modified: Tue Mar 11 11:33:42 PST