by David Wood

As promised, we're going to take yet another pass at user interaction within a Java AWT-based interface. More specifically, we're going to, for the moment, do a final wrap-up on events.

Last week we discussed using the action() method from the java.awt.Component class; specifically, how we could begin to use it to tie use of the objects in our interface into intelligent behavior.

Before that we discussed the even simpler ways we could redefine shortcut methods like mouseDown to perform simple tasks within our program. What we've been leading up to is that, conceptually (at the very least), these methods (literally and figuratively) have descended from the all-important handleEvent.

Strictly speaking, the Java tutorial advises against implementing handleEvent yourself if it's not "absolutely necessary." Most find, however, that it is quite necessary, and the primary danger in using it has to do with the path a piece of event information takes through a Java applet or application.

Simply put, handleEvent is a step above all the other methods for event handling discussed earlier. action(), along with all the shortcut event-related methods, only exist because they're called for in the predefined handleEvent() method. That brings us to a primary reason why programming with it can be "dangerous"; if you define your own handleEvent(), it must (typically) finish with a call to it's own superclass, or all the other events that depend on being kicked off properly in handleEvent() will break down.

Don't let the jargon mislead you. The following is an example of a basic use of handleEvent:


import java.awt.*;
import java.applet.Applet;
public class ShowEvent extends Applet {

   public void paint (Graphics g) {
      g.setColor(Color.black);
      g.draw3DRect(0,0,299,49,true);
   }

   public boolean handleEvent(Event evt) {
      System.out.println(evt.id);
      return super.handleEvent(evt);
   }
}



The box above is blank intentionally. The real work that the applet does is only visible in your Java console. Netscape 2.0/2.01 users can select "Show Java Console" from their Options menu. Next move your mouse over the box. The numbers corresponding to the events generated will be printed in the console.

You'll notice different Event ID numbers for a mouse entering, exiting, clicking, or moving within the applet's area, as well as (if your applet area is properly selected in your windowing system) events for keyboard activity--one for "key-down" and the other for "key-up." To those unfamiliar with making this distinction, rest assured, it's standard to split the event report of a keypress like this, and the different ways these events are used compose the subtleties of a user interface. For instance, even though millions of people use Apple's Finder interface every day, relatively few notice that a button is "clicked" when the mouse's button is released over it as opposed to pressed down.

As we were saying before, handleEvent must call its own "superclass"; this is nothing more complicated than saying that, after our version of handleEvent runs, it returns with a call to a predefined handleEvent. Because of this the other functions of Java that are pillared up by the normal (default) operation of this method continue to work normally.

More generally, a superclass is the definition of a class that's next up in the hierarchy from the present one, just as the superclass of ShowEvent.class above is the Applet class. (Java offers a programming tutorial on subclasses and superclasses.)

We've found that choosing one technique over another so as to allow your program to react to events tends to be dictated by the kind of interface you've chosen. This portion of a tutorial presents a fairly thorough (although in apparently rough-draft form) treatment of handleEvent.

If you're wondering why all this is so important, think about this. For a significant majority of the applets running on the Web, these methods of interaction are the only way the user communicates and affects the program at all.

Forget the INPUT and PRINT statements that we might have learned about in school. For instance, the brutally simple applet we've shown here is only a few changes away from applets you might have seen on professionally designed Web sites. Simply paint an image inside your frame and then choose a new image and repaint() depending on the x and y coordinates that pass through handleEvent every time the mouse moves over the applet.

Bang, you've now created an "Active Imagemap" or an animation that flows when the user passes over it. You could also call up and, depending on where the pointer is, play a sound that provides a soundtrack to your presentation, or a different text box that offers an explanation of items in your image.

Next week we're going to attempt to cover a few more of the basic elements of Java interface, starting with the basics on how to draw images and create sounds from within your applet. Stay tuned.

Past installments of Java Jolt

http://www.internet.com/