by David Wood

Events

Some programmers love them, some hate them, and most people just haven't the slightest idea what they mean. Truly event-based programs operate on a different level from what those of you who've learned programming techniques using QuickBasic or C are probably used to.

Unlike the point-A-to-point-B programming methodologies of years past, events are about response as opposed to action.

Depending on your ultimate goals, this can make for very elegant solutions or it can hamstring you. But let's start with the basics. Last week we demonstrated a program that made use of events in a very simple way. In fact, of the myriad of possible events our program could have instructions to handle, we only defined three: init, mouseDown, and paint.

Going back even further, you recall that, as an applet, our sample applet inherited its applet status from the Applet class. Imagine this as a framework, taking definitions for just about everything a normal applet could need from all over the Java API and placing them at the disposal of our program. This includes data about an incredible variety of facilities within the Java Virtual Machine (things like constants defined for and used by standard Java methods), as well as definitions of standard methods themselves.


import java.awt.Graphics;
import java.awt.Color;
public class FirstTry extends java.applet.Applet {
   StringBuffer buffer = new StringBuffer("Click Me!");
   public void init() {
      resize (150,25);
   }

   public boolean mouseDown (java.awt.Event evt,int x,int y) {
      buffer = new StringBuffer("Thank You!");
      repaint();
      return(false);
   }

   public void paint(Graphics g) {
      g.setColor(Color.darkGray);
      g.draw3DRect(0,0,149,24,true);
      g.setColor(Color.black);
      g.drawString(buffer.toString(),50,18);
   }
}



This is how we, as programmers, relate to events. As you can see in the sample program above, we've defined a method called mouseDown; this is a standard Java method that is executed any time the Java environment senses this event. (In this case, whenever the user's mouse button clicks down within the Java window on the screen.)

When we inherit the Applet class, this method is automatically included, in a manner of speaking, within our own program. However, until we define it ourselves, it does nothing. This is the case with a majority of the standard methods corresponding to system events.

MouseDown, along with a plethora of other useful methods that are available to you, the applet designer, is located in a class called java.awt.Component. If you look at the description of this class we just pointed to, you may realize that there's much more going on here than we're letting on. Have patience, we'll get there. In the mean time, a thorough look through this potentially confusing piece of documentation might bring attention to a few other methods that appear to be named after common events: mouseDrag, mouseEnter, mouseExit, and so forth. Think about them. Contemplate the possibilities.

We don't want to give the mistaken impression that all Java applets have to be helpless puppets to the whim of the user; your use of an event-related method need not be so limited as the one in our sample applet. If your task requires straightforward procedures and has less need of sensitivity to the user's behavior, clever use of several specific (and cardinal) methods allow for applets to be as straightforward in execution as you can stand. Without further ado, let's look at those methods.

init()
--Executed the first time the applet is loaded (or in some cases, reloaded).

start()
--Executed at the actual start of the applet's execution; for example, when it's first viewed on a page and then every time the user returns to that page.

stop()
--Executed when the applet's execution is supposed to stop; for instance, when the user leaves the Web page the applet is on or is closing a browser window.

destroy()
--Executed for purposes of "final cleanup." For a majority of Netscape users, this only means when the browser itself is about to quit.

A much more detailed explanation about these routines and their effective uses are provided in the Java tutorial. For further inspiration, you might want to check out the class that contains them; not surprisingly, it's the one called java.applet.Applet. And for creative ideas about what you can actually do with these methods now that you know they exist, try us again next Friday.

Past installments of Java Jolt

http://www.internet.com/