|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
Each applet has a life cycle. An applet is initialized (once and only once), started and stopped (one or more times during
its life), and destroyed (once and only once). The browser calls one of four methods at significant points during that life
cycle to indicate that the applet has entered another phase of existence. Those methods are init(), start(), stop(), and destroy().
The browser calls init() method after creating a class_name object (a task that occurs behind the scenes). Generally, you would perform one-time initialization, such as creating the
program's GUI, in that method. init() features the following signature: public void init ().
The start() method is called immediately after init() and whenever the Webpage containing the applet is revisited. That method represents a good location to begin a background
thread for performing animation (which we'll investigate in a future article). It has the following signature: public void start ().
The browser calls the stop() method prior to leaving a Webpage and just before calling the destroy() method. You would normally place code that terminates a background thread in stop(). It has the following signature: public void stop ().
Finally, the browser calls destroy() before exiting or replacing a stopped applet in its cache. That method presents an appropriate spot for canceling any global
initialization such as closing a file that was opened in init() (unrestricted applets only). destroy() has the following signature: public void destroy ().
To give you an idea of what a simple applet looks like, I've written a small applet called Lifecycle. The source code is presented in Listing 3.
Listing 3. Lifecycle.java
// Lifecycle.java
// To run applet under Netscape Communicator 4.7 browser or Internet
// Explorer 5.0 internal JVM, compile using javac -target 1.1 Lifecycle.java
import java.awt.*;
public class Lifecycle extends java.applet.Applet
{
String msg = "Hello World";
public void init ()
{
System.out.println ("init called");
String s = getParameter ("message");
if (s != null)
msg = s;
}
public void start ()
{
System.out.println ("start called");
}
public void paint (Graphics g)
{
g.drawString (msg, 10, 30);
}
public void stop ()
{
System.out.println ("stop called");
}
public void destroy ()
{
System.out.println ("destroy called");
}
}
When the browser calls either init(), start(), stop(), or destroy(), those methods in turn call System.out.println to send a message to the standard output device.
I've declared a String variable, called msg, and initialized that variable to Hello World (all the characters between the double quote characters). Behind the scenes, an object of type String is created, and the characters between the double quotes are copied into that object. (You'll learn more about strings in
a future article.)
The init() method calls the getParameter() method (inherited from Applet) to retrieve the value of an applet parameter. An external name/value pair, an applet parameter dynamically configures an applet without recompilation. The name of the
parameter is identified as message -- the sole String argument to getParameter(). If message exists, getParameter() returns its value. If it doesn't exist, a special null value returns. Java supplies the null reserved word to indicate a null reference -- similar to a zero pointer in C or C++. We'll look more closely at null in next month's article.