Java Reference
In-Depth Information
infinite) loops. To perform a time-consuming or repetitive task, such as animation,
an applet must create its own thread, over which it does have complete control.
The task of writing an applet, then, comes down to defining the appropriate meth-
ods. A number of these methods are defined by the Applet class:
init()
Called when the applet is first loaded into the browser or viewer. It typically
performs applet initialization, in preference to a constructor method. (The
web browser doesn't pass any arguments to an applet's constructor method,
so defining one isn't too useful.)
destroy()
Called when the applet is about to be unloaded from the browser or viewer.
It should free any resources, other than memory, the applet has allocated.
start()
Called when the applet becomes visible and should start doing whatever it
does. Often used with animation and with threads.
stop()
Called when the applet becomes temporarily invisible, for example, when the
user has scrolled it off the screen. Tells the applet to stop performing an ani-
mation or other task.
getAppletInfo()
Called to get information about the applet. Should return a string suitable for
display in a dialog box.
getParameterInfo()
Called to obtain information about the parameters the applet responds to.
Should return strings describing those parameters.
In addition to these Applet methods, there are a number of other methods, inher-
ited from superclasses of Applet , that the browser invokes at appropriate times,
and which an applet should override. The most obvious of these methods is
paint() , which the browser or viewer invokes to ask the applet to draw itself on
the screen. A related method is print() , which an applet should override if it
wants to display itself on paper differently than it does on the screen. There are
quite a few other methods applets should override to respond to events. For
example, if an applet wants to respond to mouse clicks, it should override mouse-
Down() . (We'll see more about event handling in applets later in this chapter.)
The Applet class also defines some methods that are commonly used (but not
overridden) by applets:
getImage()
Loads an image file from the network and returns a java.awt.Image object.
getAudioClip()
Loads a sound clip from the network and returns a java.applet.AudioClip
object.
Search WWH ::




Custom Search