|
|
Overview of Applets |
TheSimpleapplet defines its onscreen appearance by overriding thepaintmethod:class Simple extends Applet { . . . public void paint(Graphics g) { . . . } . . . }The
paintmethod is one of two display methods an applet can override:
paint- The basic display method. Many applets implement the
paintmethod to draw the applet's representation within a browser page.update- A method you can use along with
paintto improve drawing performance.Applets inherit their
paintandupdatemethods from theAppletclass, which inherits them from the Abstract Window Toolkit (AWT)Componentclass. For an overview of theComponentclass, and the AWT in general, see the Overview of the Java UIlesson. Within the overview, the architecture of the AWT drawing system is discussed on the Drawing
page.
Applets inherit a group of event-handling methods from the
Componentclass. (The architecture of the AWT event system is discussed on the Eventspage.) The
Componentclass defines several methods, such asactionandmouseDown, for handling particular types of events, and then one catch-all method calledhandleEvent.To react to an event, an applet must override either the appropriate event-specific method or the
handleEventmethod. For example, adding the following code to theSimpleapplet 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; }
Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go toexample-1dot1/SimpleClick.html.
|
|
Overview of Applets |