Java Events
Volume Number: 12
Issue Number: 12
Column Tag: Getting Started
Java Events
By Dave Mark
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Originally I was planning to cover double-buffering in this month’s column. I started
writing a cool banner animator, then I got a little side-tracked playing with Java’s
event-handling mechanism. As I explored (and as my animation applet took on a life of
its own!), I realized that we never really covered events. Since events are the heart
and soul of your applet’s user interaction, and since I ended up writing this nifty little
event doodad anyway, I thought we would dive into events now and put off animation for
the moment.
The Ultimate Event Handler
This month’s applet is called eventHandler. For you Primer readers,
eventHandler is similar to eventTracker. As you click, drag, and type, the events
associated with those actions are displayed in a scrolling list. Figure 1 shows
eventHandler running in the Metrowerks Java applet viewer. One thing I learned from
this exercise is that no two applet viewers behave exactly the same way. For example,
the Metrowerks Java viewer swallows keyUp events. In Netscape Gold 3.0 (see Figure
2), the keyUp events show up, but mouseMove events are not reported properly. The
Sun JDK Applet Viewer 1.0.2 (not shown) doesn’t handle clipping correctly. .
Well, at least these applet viewers are much better than their predecessors!
Figure 1. eventHandler running in Metrowerks’ Applet Viewer. Notice that keyUp
events are swallowed. See Figure 2.
Figure 2. eventHandler running in Netscape Gold 3.0. The keyUp events are there, but
mouseMove events (not shown) don’t work right.
eventHandler consists of two major areas (each with its own label). On the top is
a Canvas with a yellow background. All the events trapped by this Canvas are listed in
the scrolling TextArea on the bottom. When the keyboard focus is on the Canvas, its
border is drawn in red. When the Canvas loses the keyboard focus, the border is
redrawn as yellow.
If you click or drag in the Canvas, the appropriate events get listed in the
scrolling list. If you type while the focus is in the Canvas, the actual key names are
listed when the keyDown event is reported. Note that I don’t report the mouseMove
event, which is supposed to occur when you move the mouse. Unfortunately, the
Metrowerks viewer is the only one that handles this correctly. Both Netscape and the
Sun viewer report a steady stream of mouseMove events, even when the mouse is
perfectly still! This can be a real pain, since any other events tend to get swept away in
a flood of incorrectly reported mouseMove events. Add a mouseMove handler to the code
below, just to see this for yourself.
The eventHandler Source Code
Create a new project using the Java Applet stationery. Create a source code file
named eventHandler.java and add it to the project. Here’s the source code:
import java.awt.*;
public class eventCanvas extends Canvas
{
boolean hasFocus;
eventCanvas( int width, int height )
{
setBackground( Color.yellow );
resize( width, height );
hasFocus = false;
}
public boolean mouseUp( Event e, int x, int y )
{
eventHandler.reportEvent( “mouseUp” );
return true;
}
public boolean mouseDown( Event e, int x, int y )
{
eventHandler.reportEvent( “mouseDown” );
return true;
}
public boolean mouseDrag( Event e, int x, int y )
{
eventHandler.reportEvent( “mouseDrag” );
return true;
}
public boolean mouseEnter( Event e, int x, int y )
{
eventHandler.reportEvent( “mouseEnter” );
return true;
}
public boolean mouseExit( Event e, int x, int y )
{
eventHandler.reportEvent( “mouseExit” );
return true;
}
public boolean keyDown( Event e, int key )
{
String eventString = “keyDown: “;
String keyName, modifierName;
modifierName = getModifierName( e );
if ( modifierName != null )
eventString += modifierName;
keyName = getKeyName( key );
if ( keyName != null )
eventString += keyName;
else if (( key >= 32 ) && ( key <= 127 ))
eventString += new Character( (char)key ).toString();
else
eventString += key;
eventHandler.reportEvent( eventString );
return true;
}
public String getModifierName( Event e )
{
if ( e.controlDown() )
return( “Control-” );
if ( e.metaDown() )
return( “Meta-” );
if ( e.shiftDown() )
return( “Shift-” );
return null;
}
public String getKeyName( int key )
{
switch ( key )
{
case Event.F1: return “F1”;
case Event.F2: return “F2”;
case Event.F3: return “F3”;
case Event.F4: return “F4”;
case Event.F5: return “F5”;
case Event.F6: return “F6”;
case Event.F7: return “F7”;
case Event.F8: return “F8”;
case Event.F9: return “F9”;
case Event.F10: return “F10”;
case Event.F11: return “F11”;
case Event.F12: return “F12”;
case Event.HOME: return “HOME”;
case Event.END: return “END”;
case Event.LEFT: return “Left Arrow”;
case Event.RIGHT: return “Right Arrow”;
case Event.UP: return “Up Arrow”;
case Event.DOWN: return “DownArrow”;
case Event.PGUP: return “Page Up”;
case Event.PGDN: return “Page Down”;
}
return null;
}
public boolean keyUp( Event e, int key )
{
eventHandler.reportEvent( “keyUp” );
return true;
}
public boolean gotFocus(Event e, Object what)
{
hasFocus = true;
eventHandler.reportEvent( “gotFocus” );
repaint();
return true;
}
public boolean lostFocus(Event e, Object what)
{
hasFocus = false;
eventHandler.reportEvent( “lostFocus” );
repaint();
return true;
}
public void paint( Graphics g )