Write a class that extends java.applet.Applet
or
javax.swing.JApplet
, and use some or all of the
applet methods. Start with Applet
if you want to
use plain
AWT and be portable to all browsers; use
JApplet
if you want Swing
capabilities in your applet (but see the note at the end of this
recipe under Section 17.3.4).
The four
Applet
“life
cycle” methods that an applet writer can implement are
init( )
, start( )
,
stop( )
, and destroy( )
(see
Table 17-2). The applet’s life cycle is more
complex than that of a regular application, since the user can make
the browser move to a new page, return to a previous page, reload the
current page, etc. What’s a poor applet to do?
Applets normally use their init( )
method to
initialize their
state, the same functionality as a
constructor in a non-applet class. This may seem a bit odd for those
used to constructors in an OO language. However, it is mandatory for
any methods that will call applet-specific methods, such as the
all-important getParameter( )
. Why? In brief,
because the
browser
will first construct the applet -- always with the no-argument
constructor form, which is much easier for the browser (see Section 25.4) -- and then call its setStub( )
method.[39] The AppletStub
is an object provided
by the browser, which provides a method getAppletContext( )
,
which of course returns an AppletContext
object.
These are both delegates (in the design patterns
sense). The AppletStub
object contains the actual
implementation of getParameter( )
,
getCodeBase( )
, and getDocumentBase( )
. The AppletContext
object contains the
real implementations of most other applet-specific routines,
including showStatus( )
,
getImage( )
, and showDocument( )
.
So, an applet constructor can’t call getParameter( )
, getImage( )
, or showStatus( )
because the AppletStub
isn’t set
until the applet’s constructor returns. About the most a
constructor can do is add GUI elements. Therefore, it is generally
preferable to do all the applet’s initialization in one place,
so it might as well be the init( )
method, which a
sane browser will call only once for each applet instance. This is
why, in practice, most applets don’t have any constructors: the
default (no-argument) constructor is the only one ever called.
The start( )
method is called when the browser has
fully loaded the applet and it’s ready to go. This is the
normal time for your applet to start threads (Chapter 24), audio or video (see Chapter 12), or anything else that takes time. The
stop( )
method is called when the user gets bored
and leaves the page.
The least commonly used applet method is destroy( )
; it is called when the
browser
removes your applet instance from memory and allows you to close
files, network connections, etc. After that, it’s all over.
All four methods are public
, all return
void
, and all take no arguments. They are shown
together in Example 17-1.
Example 17-1. AppletMethods.java
import java.applet.*; import java.awt.*; import java.net.*; /** AppletMethods -- show stop/start and AudioClip methods */ public class AppletMethods extends Applet { /** AudioClip object, used to load and play a sound file. */ AudioClip snd = null; /** Yes, applets can have constructors! */ public AppletMethods( ) { System.out.println("In Appletmethods::<init> No Arg form"); } /** Initialize the sound file object and the GUI. */ public void init( ) { System.out.println("In AppletMethods.init( )"); try { snd = getAudioClip(new URL(getCodeBase( ), "laugh.au")); } catch (MalformedURLException e) { showStatus(e.toString( )); } setSize(200,100); // take the place of a GUI } /** Called from the Browser when the page is ready to go. */ public void start( ) { System.out.println("In AppletMethods.start( )"); if (snd != null) snd.play( ); // loop( ) to be obnoxious... } /** Called from the Browser when the page is being vacated. */ public void stop( ) { System.out.println("In AppletMethods.stop( )"); if (snd != null) snd.stop( ); // stop play() or loop( ) } /** Called from the Browser (when the applet is being un-cached?). * Not actually used here, but the println will show when it's called. */ public void destroy( ) { System.out.println("In AppletMethods.destroy( )"); } public void paint(Graphics g) { g.drawString("Welcome to Java", 50, 50); } /** An alternate form of getParameter that lets * you provide a default value, since this is so common. */ public String getParameter(String p, String def) { return getParameter(p)==null?def:getParameter(p); } }
Applets based on
Applet
and using AWT
will work on most browsers. Applets based on
JApplet
and/or using Swing components will need
the Java Plug-in (see Section 23.6) to ensure that a compatible runtime is
available.
[39] It didn’t have to be this way. At the beginning of Java-browserdom, they could have said, “Let’s just pass in the applet stub as an argument when constructing the applet.” But they didn’t, “and now it’s too late,” as Dr. Seuss once said.
18.223.170.63