Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheSimple
applet defines its onscreen appearance by overriding thepaint
method:class Simple extends Applet { . . . public void paint(Graphics g) { . . . } . . . }The
paint
method is one of two display methods an applet 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 theApplet
class, which inherits them from the Abstract Window Toolkit (AWT)Component
class. For an overview of theComponent
class, and the AWT in general, see the Overview of the Java UI trail. 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
Component
class. (The architecture of the AWT event system is discussed on the Events page.) TheComponent
class defines several methods, such asaction
andmouseDown
, 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
handleEvent
method. For example, adding the following code to theSimple
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; }
Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the J2SE JRE or SDK. We strongly recommend that you install the latest version; at least 1.3.1 is required for all our applets. You can find more information in the Java Plug-in home page.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.