The Events

In this section we’re going to categorize the events your applet should respond to. You may not always need to respond to each one. For some of the events we’ll also include new methods for the EventTutor applet that show a little more information. When we do you should replace the old method with the new one.

init
The init() method is called when your applet begins executing. Netscape is also known to call this method at other times such as when an applet is reloaded or you return to a page containing an applet. Generally you use this method to set up any data structures or perform any tasks you need to get ready to run the applet. Since it’s only called once it’s easy to miss the init() method in the EventTutor applet. If necessary redirect the standard output to a file and look at the first line of that file to see it.

public void init() {
System.out.println(”init event”);
}

paint
We’ve already seen the paint() method. Almost any applet is going to need to override this method. This is the method in which you will do all your drawing. You can only write to the applet screen in the paint method. However there are times when you’ll want to write to an offscreen image in another method and then just quickly copy that image to the screen in your paint() method.

public void paint(Graphics g) {
theList.addItem(”paint event”);
}

stop
A stop() message says the user is no longer looking at the page that contains the applet. This is usually because the user left the page or minimized the window. At this time you should stop any CPU eating activities that don’t matter when the user isn’t looking at your page. For instance your Doom applet should stop tracking monster movement if the user isn’t actually playing. On the other hand a spreadsheet applet in the middle of a long calculation should continue calculating because the user is likely to want the result later. Once the user returns to the page the start() method is called.

public void stop() {
theList.addItem(”stop event”);
}

start
The start() method is called when a user brings their attention back to an applet, for instance after maximizing a window or returning to the applet’s page. It is called after the init() method. Initialization code that needs to be performed every time an applet is restarted should be put here.

public void start() {
theList.addItem(”start event”);
}

destroy
The destroy() method is called before the applet is unloaded completely. It is called after the stop() method. Users may reload the applet later, but if they do it will be as if they’ve never seen it before. All variables, static, member, local or otherwise will be initialized to their initial state. If you have any final cleanup to do (for instance sending output back to the httpd server) do it here.

public void destroy() {
theList.addItem(”destroy event”);
}

update
The update() method is called automatically by the system when ????. It’s often overridden when you want to use offscreen Images to avoid flicker.

public void update(Graphics g) {
theList.addItem(”update event”);
}

mouseUp
The mouseUp() method is called whenever the mouse button is released in your applet. In most cases this is the event you’ll want to watchout for, not mouseDown. A button is typically highlighted when the mouse button is pressed on it, but it is not activated till the user releases the mouse button. This gives the user a chance to change their mind by moving the cursor off the object without releasing it.

The exception would be when you want an action to continue as long as the mouse button is held down, a fast forward button on a movie playing applet for instance.

mouseUp() methods also receive the coordinates of the point where the mouse was released.

public boolean mouseUp(Event e, int x, int y) {
theList.addItem(”mouseUp event at (” + x + “,” + y + “)”);
return false;
}

mouseDown
The mouseDown() method is called whenever the mouse button is pressed in your applet. In most cases you’ll want to wait for a mouseUp before taking any action though.

mouseDown() methods also receive the coordinates of the point where the mouse was released.

public boolean mouseDown(Event e, int x, int y) {
theList.addItem(”mouseDown event at (” + x + “,” + y + “)”);
return false;
}

mouseDrag
mouseDrag() methods occur when a user moves the mouse while holding down the mouse button. mouseDrag() methods receive the coordinates of the point where the mouse is when the event occurs.

public boolean mouseDrag(Event e, int x, int y) {
theList.addItem(”mouseDrag event at (” + x + “,” + y + “)”);
return false;
}

mouseMove
mouseMove methods occur when a user moves the mouse without holding down the mouse button. mouseMove methods receive the coordinates of the point where the mouse is when the event occurs.

public boolean mouseMove(Event e, int x, int y) {
theList.addItem(”mouseMove event at (” + x + “,” + y + “)”);
return false;
}

mouseEnter
Your applet receives a mouseEnter event whenever the cursor enters your applet from somewhere else. You’ll also receive the coordinates of the point at which the cursor entered your applet. After this happens its typically followed by a Stream of mouseMoved events as the cursor continues through the applet so it can be hard to see.

public boolean mouseEnter(Event e, int x, int y) {
theList.addItem(”mouseEnter event at ” + x + “,” + y + “)”);
return false;
}

mouseExit
Your applet receives a mouseExit event whenever the cursor leaves your applet. You’ll also receive the coordinates of the point at which the cursor exited your applet.

public boolean mouseExit(Event e, int x, int y) {
theList.addItem(”mouseExit event at (” + x + “,” + y + “)”);
return false;
}

getFocus

public void getFocus() {
theList.addItem(”getFocus event”);
}

gotFocus
public void gotFocus() { theList.addItem(”gotFocus event”); }
lostFocus

public void lostFocus() {
theList.addItem(”lostFocus event”);
}

keyDown
A keydown event is generated whenever the user presses a key while your applet is active. An integer keycode is returned indicating which key was pressed. As a general rule you’ll want to cast this to a char to get the actual letter.

public boolean keyDown(Event e, int x) {
theList.addItem(”The ” + (char) x + ” key was pressed.”);
return false;
}

Comments are closed.