Java Reference
In-Depth Information
The init method is executed once when the applet is first loaded, such as when
the browser or appletviewer initially views the applet. Therefore the init method
is the place to initialize the applet's environment and permanent data.
The start and stop methods of an applet are called when the applet becomes
active or inactive, respectively. For example, after we use a browser to initially
load an applet, the applet's start method is called. We may then leave that page
to visit another one, at which point the applet becomes inactive and the stop
method is called. If we return to the applet's page, the applet becomes active again
and the start method is called again.
Note that the init method is called once when the applet is loaded, but start
may be called several times as the page is revisited. It is good practice to imple-
ment start and stop for an applet if it actively uses CPU time, such as when it
is showing an animation, so that CPU time is not wasted on an applet that is not
visible.
Also note that reloading the Web page in the browser does not necessarily
reload the applet. To force the applet to reload, most browsers provide some key
combination for that purpose. For example, in Netscape Navigator, holding down
the Shift key while clicking the Reload button with the mouse not only reloads
the Web page but also reloads (and reinitializes) all applets linked to that page.
The getCodeBase and getDocumentBase methods are useful to determine
where the applet's bytecode or HTML document resides. An applet could use the
appropriate URL to retrieve additional resources, such as an image or audio clip
by using the applet methods getImage or getAudioClip , respectively.
Security is an issue with applets. As you browse Web pages, you may open
a page containing an applet, and suddenly an unknown program is executing
on your machine. Because of the dangers inherent in that process, applets are
restricted in the kinds of operations they can perform. For instance, an applet
cannot write data to a local drive.
In the Graphics Track sections throughout this topic, we explore issues related
to the development of programs that use graphical user interfaces (GUIs). The
examples in those sections are presented as Java applications, using JFrame com-
ponents as the primary heavyweight container. An applet can also be used to pres-
ent GUI-based programs. Like a JFrame , a JApplet is a heavyweight container.
Applets are useful for small, isolated programs, such as a game or calculator.
Because of their security restrictions and processing overhead, they are not fre-
quently used for larger systems. Generally, other technologies are used to support
fully integrated, dynamic Web sites.
Search WWH ::




Custom Search