Home
<<Table of Contents
<<Java Packages
Animating Applets>>

EVENT-DRIVEN PROGRAMMING

B efore Graphical User Interfaces (GUI's; Buzzword number three, but who's counting) user's could only perform actions on the computer if the user set up the program to intercept the statement. Such as BASIC's "Input" statement that allowed users to type something. Otherwise, the keyboard was ignored. In GUI's however, the user can always perform actions on your applet. The user can click in it, type in it, or put another window over it. You applet has to be prepared for this, and event-driven programming allows this.

  1. What is Event-Driven Programming?
  2. The Applet Class
  3. Overridable Events
  4. Counter: An Event-Driven Programming Example


Top

What is Event-Driven Programming?

This is where I finally describe Event-Driven Programming. When the user interacts with your program, it triggers a special section of your code designed to deal with this action. For example, when the user clicks on your applet, it calls the "mouseDown" and "mouseUp" methods. The key to do doing this is in the Applet class.



Top


The Applet Class

All Applets you create are derived from the Applet class. This means that your applet may freely call any method that is part of the applet class. It also means that your applet may override any of the Applet class' methods. A good is example is the "mouseUp" method. Whenever you lift up the mouse button in Java the Applet class calls the empty "mouseUp" method. If you override this method, whatever code you put in the method will be executed when the user lifts up the mouse button.



Top


Overridable Events

You can override any of the methods in this list to make your applet respond to events.

public void init() {
This method is called when your applet begins.

public void destroy() {
This is called when your applet is unloaded. This is when the browser containning your applet is exited or when the user leaves the page.

public void start() {
This is called after the "init" event. It is also called at times when the user is not using your applet and starts to use it again such as when a minimized browser containning your applet is maximized.

public void stop() {
This is called when the user stops using your applet. Such as when he minimizes the browser. You generally want to stop any processor intensive operations when this is called.

public boolean mouseDown(Event evt, int x, int y) {
This is called when the user presses the mouse button down. This event recieves three paramaters. The first is of the type "Event" found in the Java "awt" package. This parameter is actually not very useful. This just means that you must include the statement "import java.awt.Event;" or "import java.awt.*" at the beginning of your program. The next two paramater - "x" and "y" contain the coordinates where the button was pressed down. At the end of this method, you should always include the statement "return true" because the method has a return value of "boolean." This tells the Applet that you did override the method. Because many platforms (such as the Macintosh) have mice with only one button Java only supports one button.

public boolean mouseUp(Event evt, int x, int y) {
This is much like the mouseDown event except it is called when the mouse button is released.

public boolean mouseMove(Event evt, int x, int y) {
This event is called when the mouse if moved over the applet. The coordinates are the current coordinates of the mouse pointer.

public boolean mouseDrag(Event evt, int x, int y) {
The same as the "mouseMove" event except it is called when the mouse is moved with its button held down.

public boolean mouseEnter(Event evt, int x, int y) {
This event is called when the cursor is moved into the applet. The coordinates are the point where the mouse pointer enters the applet.

public boolean mouseExit(Event evt, int x, int y) {
This event is called when the mouse pointer leaves the applet. The coordinates are the point where the pointer left the applet.

public boolean keyDown(Event evt, int x) {
This is called when one of the keys is pressed down on the keyboard. The "x" contains a code representing the key which was pressed.

public void paint(Graphics g) {
This is called when the applet needs to be repainted (because something moved over it) or when the "repaint" method is called. It is where you will want to draw things on the screen. The Graphics class is a graphics context that allows you to draw to the screen. You must include "import java.awt.Graphics;" or "import java.awt.*" in the beginning of the applet to use this event.

public void Update(Graphics g) {
This method is actually not an event. It is called when the "repaint" sub is called. The method draws the gray background to the screen, and then calls the "paint" method. You may want to override this to prevent the gray background from being drawn and to prevent screen flicker in animations. If you do, make sure to call the paint method at the end of the method and pass the Graphics class to it.



Top

View the source code.


Counter: An Event-Driven Programming Example

Our next example program uses event-based programming to make an applet which counts the number of times it has been clicked and the coordinates of the mouse pointer inside the applet. Here is the applet:


Sorry, your browser does not support Java. To see this example, you must use a Java-Enabled browser such as the browser provide by Sun, Netscape 2.0 or higher, or Internet Explorer 3.0 or higher.

I hope you enjoyed the applet. Please have a look at the source code.

You should notice a few things. The "mouseMove" and "mouseClick" methods are only called if the pointer is inside the applet. The code is not complicated. First, the variables "Clicks," "CurrentX," and "CurrentY" are not inside any of the methods so they are global variables. Whenever you click on the applet, it calls "MouseUp" method which adds one to the variable "Clicks," and whenever you move the pointer, it calls the "mouseMove" method which sets the variables "CurrentX" and "CurrentY" to the position of the cursor. Both of these methods call the "Repaint" method at the end. To be very specific, the "Repaint" calls the "update" method. The "update" method paints the background gray, and then calls the "paint" method. This allows the applet to update the information on the screen. The paint method writes the three global variables to the screen with the Graphics class' "drawString" method described in the first chapter - "Introduction to Java." The first parameter given to the drawString method is "new Integer(Clicks).toString()." This converts the Clicks varible, which is an integer variable, to a string which is required to be the first parameter of the "drawString" method. It does this by creating a new class (with the new operator) of the type "Integer" (found in java.lang) and calling its method - "toString." The return value of this method is a string containing the integer in the "Clicks" value. The "drawString" method excepts this and draws it to the screen.



Home
<<Table of Contents
<<Java Packages
Animating Applets>>

Our next chapter explains two more important concepts for the Java applet model - Multi-threading and double buffering. It is surprising how much more control over your applet these techniques give you. You will also enjoy creating your first useful applet - a vertically scrolling Java marquee.