This chapter demonstrates the techniques of applet writing. It proceeds from a trivial "Hello World" applet to more sophisticated applets. Along the way, it explains how to:
Study the examples carefully. They are the important part of this chapter! You may find it useful to refer to the quick reference in Chapter 17, The java.applet Package while reading these examples.
Note that this chapter merely introduces the framework for writing applets. Applets, like other Java programs, use features from throughout the Java API. See Chapter 7, Events, in particular, for details on event processing in Java applets and applications.
An applet, as the name implies, is a kind of mini-application, designed to be run by a Web browser, or in the context of some other "applet viewer." Applets differ from regular applications in a number of ways. One of the most important is that there are a number of security restrictions on what applets are allowed to do. An applet often consists of untrusted code, so it cannot be allowed access to the local file system, for example. The details of applet security and the restrictions placed on applets are discussed at the end of this chapter.
From a programmer's standpoint, one of the biggest differences between applets and applications is that applets do not have a main() method, or other single entry point from which the program starts running. Instead, to write an applet, you subclass the Applet class and override a number of standard methods. At appropriate times, under well-defined circumstances, the Web browser or applet viewer invokes the methods you have defined. The applet is not in control of the thread of execution; it simply responds when the browser or viewer tells it to. For this reason, the methods you write must take the necessary action and return promptly--they are not allowed to enter time-consuming (or infinite) loops. In order 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 methods. A number of these methods are defined by the Applet class:
Called when the applet is first loaded into the browser or viewer. It is typically used to perform 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.)
Called when the applet is about to be unloaded from the browser or viewer. It should free any resources, other than memory, that the applet has allocated.
Called when the applet becomes visible and should start doing whatever it is that it does. Often used with animation and with threads.
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 animation or other task.
Called to get information about the applet. Should return a string suitable for display in a dialog box.
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, inherited from superclasses of Applet, that the browser invokes at appropriate times, and that 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. In Java 1.1, 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 that applets should override to respond to events. For example, if an applet wants to respond to mouse clicks, it should override mouseDown(). (As we'll see in Chapter 7, Events, however, there are other, preferred, ways to receive mouse events in Java 1.1.)
The Applet class also defines some methods that are commonly used by applets:
Loads an image file from the network and returns an Image object.
Loads a sound clip from the network and returns an AudioClip object.
Looks up and returns the value of a named parameter, specified in the HTML file that refers to the applet with the <PARAM> tag.
Returns the base URL from which the applet class file was loaded.
Returns the base URL of the HTML file that refers to the applet.
Displays a message in the status line of the browser or applet viewer.
Returns the AppletContext object for the applet. AppletContext defines the useful showDocument() method that asks the browser to load and display a new Web page.