Previous | Next | Trail Map | Writing Applets | The Anatomy of an Applet


More Methods to Override

class Simple extends Applet {
    . . .
    public void paint(Graphics g) { . . . }
    . . .
}
The Simple applet, like most other applets, overrides the paint() method. The paint() method is one of two display methods that the Applet class provides for applets to override:
paint()
the basic display method; most applets implement this method to draw the applet's representation within a browser page
update()
a method you can use along with paint() to make improvements in drawing performance
The Applet class also provides a group of methods for event handling:
keyDown()
notifies the applet that the user pressed a key on the keyboard
mouseDown(), mouseUp()
notify the applet that the user pressed/released a mouse button
mouseEnter(), mouseExit()
notify the applet that the cursor entered/exited the applet's display area
mouseDrag()
notifies the applet that the user moved the mouse while pressing a mouse button
mouseMove()
notifies the applet that the mouse moved but no mouse button was pressed
An applet overrides the method corresponding to the event the applet needs to react to. For example, to make the Simple applet respond to mouse clicks, we must add a mouseDown() method.
public void mouseDown(int x, int y) {
    addItem("click!... ");
}
Below is the resulting applet. When you click within its rectangle, it displays the word "click!...".

Drawing and event handling are covered in detail later in this trail, in [PENDING]. For more information on the methods listed above, you can also refer to the Applet API reference page.


Previous | Next | Trail Map | Writing Applets | The Anatomy of an Applet