This section presents an example applet program for the purposes of discussing its structure and operational life-cycle. The name of the applet is BasicApplet. An applet extends the JApplet class as is shown in figure 21-1.
Figure 21-1: BasicApplet Inheritance Hierarchy
As you can see, BasicApplet already has a lot of functionality. It is a JApplet, an Applet, a Panel, a Container, a Component, and, like all reference types, an Object. The Applet class provides, among other things, methods to ease the handling of audio clips and images. The JApplet class is the swing version of Applet and provides enhanced swing component handling features including the capability to add menus to an applet.
The code for BasicApplet is given in example 21.1 below.
Example 21.1: BasicApplet.java
1 import java.applet.*; 2 import java.awt.*; 3 import javax.swing.*; 4 5 public class BasicApplet extends JApplet { 6 7 private static JTextArea textarea = null; 8 private static JScrollPane scrollpane = null; 9 10 public void init(){ 11 textarea = new JTextArea(10, 30); 12 scrollpane = new JScrollPane(textarea); 13 getContentPane().add(scrollpane); 14 textarea.append("BasicApplet init() method called.\n"); 15 System.out.println("BasicApplet init() method called."); 16 } 17 18 public void start(){ 19 System.out.println("BasicApplet start() method called."); 20 textarea.append("BasicApplet start() method called.\n"); 21 } 22 23 public void stop(){ 24 System.out.println("BasicApplet stop() method called."); 25 textarea.append("BasicApplet stop() method called.\n"); 26 } 27 28 public void destroy(){ 29 System.out.println("BasicApplet destroy() method called."); 30 textarea.append("BasicApplet destroy() method called.\n"); 31 } 32 } // end BasicApplet class definition
Referring to example 21.1 — BasicApplet extends JApplet and implements the four primary applet methods, init(), start(), stop(), and destroy(). These four methods represent milestones in an applet’s life cycle. I discuss these life-cycle methods in greater detail below in the section titled Applet Life-Cycle Stages.
BasicApplet contains a JTextArea and a JScrollPane. Lines 7 and 8 contain the declarations for the textarea and scrollpane references. Both are initialized in the body of the init() method. The init(), start(), stop(), and destroy() methods append a line of text to the textarea as well as print out a short message to the console stating the name of the life-cycle method called. Compile this code as you would a regular Java application, however, before you can run the program you must create an HTML page and use the <applet> tag to embed information about the applet you wish to run.
Example 21.2 gives the HTML code for a minimal web page that will allow you to run the BasicApplet program.
Example 21.2: basicapplet.html
1 <html> 2 3 <applet code="BasicApplet.class" height=200 width=300 4 Your browser does not support applets if you are reading this message. 5 </applet> 6 7 </html>
Referring to example 21.2 — the <applet> tag embeds information about an applet in an HTML page. The <applet> tag has several attributes, three of which are utilized in this example: code, height, and width. The code attribute identifies the name of the applet class file to execute. The height and width attributes set the height and width of the applet display area. The line of text on line 4 will display if a browser does not recognize the <applet> tag.
Before you can run the BasicApplet program you must package it with the HTML page. You can do this simply by creating a directory into which you co-locate the BasicApplet.class and basicapplet.html files.
You can test the BasicApplet program in several ways. You can run it by opening the basicapplet.html file using your web browser. If you do this you are running the applet locally. If you have web-server software installed on your computer you could deploy the directory containing the BasicApplet files to your web server and test it locally or via a remote computer. Figure 21-2 shows the BasicApplet program running in a web browser in the Apple OS X environment. Figure 21-3 shows the console log and the messages that result from initializing and starting BasicApplet.
Figure 21-2: BasicApplet Running In Web Browser
Figure 21-3: Console Log Showing BasicApplet Life Cycle Messages
Referring to figures 21-2 and 21-3 — When the browser loads the basicapplet.html page the <applet> tag directs it to load and execute the BasicApplet.class file. This results in a call to the applet’s init() and start() methods.
Clicking the browser’s back button, refresh button, or shutting down the browser program, results in a call to the applet’s stop() and destroy() methods as is shown in figure 21-4.
Figure 21-4: Console Log Showing BasicApplet Life Cycle Messages After Browser Shuts Down
Applets have four life cycle stages: initialized, started, stopped, and destroyed. These stages correspond with the applet methods init(), start(), stop(), and destroy() respectively. Applets are not required to implement all four methods and in fact many do not. You will most likely need to implement only the init() method at a minimum. Whether or not you need to implement the remaining life-cycle methods depends on what your applet is doing.
Figure 21-5 illustrates the stages of the applet life cycle. Each stage of the applet life cycle is discussed in detail below.
Figure 21-5: Applet Life Cycle Stages
The init() method is called when the applet is first loaded or re-loaded by the browser. The init() method is akin to a constructor method although an applet can have constructor methods as well. If an applet contains constructor methods, one of them must be a no-argument constructor.
The start() method is called after the init() method when an applet has been loaded by a browser.
The stop() method is called when the browser exits the page containing the applet or when the browser unloads the applet class. The stop() method is called before the destroy() method.
The destroy() method is called when the browser unloads the applet class. The destroy() method is a good place to put code that releases any resources retained by the applet such as shutting down socket connections, etc.
Figure 21-6 shows the <applet> tag along with its mandatory and optional attributes. Optional attributes are shown in brackets.
<applet code = applet_class_name width = width_in_pixels height = height_in_pixels [archive = jar_file_name, [jar_file_name],...] [codebase = base_applet_uri] [object = serialized_applet] [alt = alternative_text] [name = applet_name] [align = applet_alignment] [vspace = vertical_margin_space_in_pixels] [hspace = horizontal_margin_space_in_pixels] > [<param name = parameter_name value = parameter_value>] ... [alternative_text] </applet>