Overview of Applets |
The Simple applet defines its onscreen appearance by overriding theclass Simple extends Applet { . . . public void paint(Graphics g) { . . . } . . . }paint()
method. Thepaint()
method is one of two display methods that applets can override:
paint()
- The basic display method. Many applets implement the
paint()
method to draw the applet's representation within a browser page.update()
- A method you can use along with
paint()
to improve drawing performance.Applets inherit their
paint()
andupdate()
methods from the Applet class, which inherits them from the Abstract Window Toolkit (AWT) Component class. For an overview of the Component class, and the AWT in general, see the Overview of the Java UI lesson. Within the overview, the architecture of the AWT drawing system is discussed on the Drawing page.From the Component class, applets inherit a group of methods for event handling. (Within the overview, the architecture of the AWT event system is discussed on the Events page.) The Component class defines several methods (such as
action()
andmouseDown()
) for handling particular types of events, and then one catch-all method,handleEvent()
.To react to an event, an applet must override either the appropriate specialized method or the
handleEvent()
method. For example, adding the following code to the Simple applet makes it respond to mouse clicks.Below is the resulting applet. When you click within its rectangle, it displays the word "click!...".import java.awt.Event; . . . public boolean mouseDown(Event event, int x, int y) { addItem("click!... "); return true; }
Overview of Applets |