AhmadLang / Java, How To Program, 2004
.pdf
[Page 968 (continued)]
20.4. Applet Life-Cycle Methods
Now that you have created an applet, let's consider the five applet methods that are called by the applet container from the time the applet is loaded into the browser to the time that the applet is terminated by the browser. These methods correspond to various aspects of an applet's life cycle. Figure 20.9 lists these methods, which are inherited into your applet classes from class JApplet. The table specifies when each method gets called and explains its purpose. Other than method paint, these methods have empty bodies by default. If you would like to declare any of these methods in your applets and have the applet container call them, you must use the method headers shown in Fig. 20.9. If you modify the method headers (e.g., by changing the method names or by providing additional parameters), the applet container will not call your methods. Instead, it will call the superclass methods inherited from JApplet.
[Page 969]
Figure 20.9. JApplet life cycle methods that are called by an applet container during an applet's execution.
Method |
When the method is called and its purpose |
|
|
public |
void init() |
|
Called once by the applet container when an applet is loaded for execution. |
|
This method initializes an applet. Typical actions performed here are initializing |
|
fields, creating GUI components, loading sounds to play, loading images to |
|
display (see Chapter 20, Multimedia: Applets and Applications) and creating |
|
threads (see Chapter 23, Multithreading). |
public |
void start() |
|
Called by the applet container after method init completes execution. In |
|
addition, if the user browses to another Web site and later returns to the |
|
applet's HTML page, method start is called again. The method performs any |
|
tasks that must be completed when the applet is loaded for the first time and |
|
that must be performed every time the applet's HTML page is revisited. Actions |
|
performed here might include starting an animation (see Chapter 21) or |
|
starting other threads of execution (see Chapter 23). |
public |
void paint( Graphics g ) |
|
Called by the applet container after methods init and start. Method paint is |
|
also called when the applet needs to be repainted. For example, if the user |
|
covers the applet with another open window on the screen and later uncovers |
|
the applet, the paint method is called. Typical actions performed here involve |
|
drawing with the Graphics object g that is passed to the paint method by the |
|
applet container. |
public |
void stop() |
|
This method is called by the applet container when the user leaves the applet's |
|
Web page by browsing to another Web page. Since it is possible that the user |
|
might return to the Web page containing the applet, method stop performs |
|
tasks that might be required to suspend the applet's execution, so that the |
|
applet does not use computer processing time when it is not displayed on the |
|
screen. Typical actions performed here would stop the execution of animations |
|
and threads. |
public |
void destroy() |
|
This method is called by the applet container when the applet is being removed |
|
from memory. This occurs when the user exits the browsing session by closing |
|
all the browser windows and may also occur at the browser's discretion when |
the user has browsed to other Web pages. The method performs any tasks that are required to clean up resources allocated to the applet.
[Page 970]
Common Programming Error 20.2
Declaring methods init, start, paint, stop or destroy with method headers that differ from those shown in Figure 20.9 results in methods that will not be called by the applet container. The code specified in your versions of the methods will not execute.
The only statements that should be placed in an applet's init method are those that should execute only once when the applet is initialized.
[Page 972 (continued)]
20.6. Sandbox Security Model
It would be dangerous to allow applets, which are typically downloaded from the Internet, to read and write files on a client computer or access other system resources. For example, what would happen if you downloaded a malicious applet? The Java platform uses the sandbox security model to prevent code that is downloaded to your local computer from accessing local system resources, such as files.
Code executing in the "sandbox" is not allowed to "play outside the sandbox." For information on security and applets, visit
developer.java.sun.com/developer/technicalArticles/Security/Signed
For information on the Java 2 Platform security model, visit
java.sun.com/j2se/5.0/docs/guide/security/spec/security-spec.doc1.html
[Page 972 (continued)]
20.7. Internet and Web Resources
If you have access to the Internet, a large number of Java applet resources are available to you. The best place to start is at the sourcethe Sun Microsystems Java Web site, java.sun.com. The Web page
java.sun.com/applets
contains several Java applet resources, including free applets that you can use on your own Web site, the demonstration applets from the JDK and other applets (many of which you can download). In a section entitled "Applets at Work," you can read about the uses of applets in industry.
If you do not have Java installed and configured for your browser, you can visit
java.com
and click the Get It Now button to download and install Java in your browser. Instructions are provided for various versions of Windows, Linux, Solaris and Mac OS.
[Page 973]
The Sun Microsystems Java Web site
java.sun.com
includes technical support, discussion forums, technical articles, resources, announcements of new Java features and early access to new Java technologies.
For various free online tutorials, visit the site
java.sun.com/learning
Another useful Web site is JARSoriginally called the Java Applet Rating Service. The JARS site
www.jars.com
originally was a large Java repository for applets. It rated every applet registered at the site as top 1%, top 5% or top 25%, so you could view the best applets on the Web. Early in the development of the Java language, having your applet rated here was a great way to demonstrate your Java programming abilities. JARS is now an all-around resource for Java programmers.
The resources listed in this section provide hyperlinks to many other Java-related Web sites. If you have Internet access, spend some time browsing these sites, executing applets and reading the source code for the applets when it is available. This will help you to rapidly expand your Java knowledge.
[Page 973 (continued)]
20.8. Wrap-Up
In this chapter, you learned the fundamentals of Java applets. You leaned basic HTML concepts that allowed you to embed an applet in a Web page and execute the applet in an applet container such as the appletviewer or a Web browser. In addition, you learned the five methods that are called automatically by the applet container during an applet's life cycle. In the next chapter, you will see several additional applets as we present basic multimedia capabilities. In Chapter 23, Multithreading, you will see an applet with start and stop methods that are used to control multiple threads of execution. In Chapter 24, Networking, we demonstrate how to customize an applet via parameters that are specified in an applet HTML element.
[Page 973 (continued)]
Summary
Applets are Java programs that can be embedded in HyperText Markup Language (HTML) documents.
When a browser loads a Web page containing an applet, the applet downloads into the Web browser and executes.
The browser that executes an applet is generically known as the applet container. The JDK
includes the appletviewer applet container for testing applets before you embed them in a Web page.
To reexecute an applet in the appletviewer, click the appletviewer's Applet menu and select the Reload menu item.
To terminate the appletviewer, select Quit from the appletviewer's Applet menu.
[Page 974]
Every Java applet is a graphical user interface on which you can place GUI components or draw.
Class JApplet from package javax.swing is used to create applets.
An applet container can create only objects of classes that are public and extend JApplet (or the Applet class from early versions of Java).
An applet container expects every Java applet to have methods named init, start, paint, stop
and destroy, each of which is declared in class JApplet. Each new applet class you create inherits default implementations of these methods from class JApplet.
When an applet container loads an applet, the container creates an object of the applet's type,
then calls the applet's init, start and paint methods. If you do not declare these methods in your applet, the applet container calls the inherited versions.
The superclass methods init and start have empty bodies, so they do not perform any tasks. The superclass method paint does not draw anything on the applet.
To enable an applet to draw, override its method paint. You do not call method paint explicitly
in an applet. Rather, the applet container calls paint to tell the applet when to draw, and the applet container is responsible for passing a Graphics object as an argument.
The first statement in method paint should be a call to the superclass version of method paint.
Omitting this can cause subtle drawing errors in applets that combine drawing and GUI components.
Before you can execute an applet, you must create an HTML (HyperText Markup Language)
document that specifies which applet to execute in the applet container. Typically, an HTML document ends with an ".html" or ".htm" file-name extension.
Most HTML elements are delimited by pairs of tags. All HTML tags begin with a left angle bracket, <, and end with a right angle bracket, >.
An applet element tells the applet container to load a specific applet and defines the size of the applet's display area (its width and height in pixels) in the applet container.
Normally, an applet and its corresponding HTML document are stored in the same directory.
Typically, a browser loads an HTML document from a computer (other than your own) connected to the Internet.
When an applet container encounters an HTML document that contains an applet, the applet
container automatically loads the applet's .class file(s) from the same directory on the computer in which the HTML document resides.
The appletviewer understands only the <applet> and </applet> HTML tags and ignores all other tags in the document.
The appletviewer is an ideal place to test an applet and ensure that it executes properly. Once
the applet's execution is verified, you can add its HTML tags to a Web page that others can view in their Web browsers.
There are five applet methods that are called by the applet container from the time the applet is
loaded into the browser to the time that the applet is terminated by the browser. These methods correspond to various aspects of an applet's life cycle.
Method init is called once by the applet container when an applet is loaded for execution. This method initializes the applet.
Method start is called by the applet container after method init completes execution. In
addition, if the user browses to another Web site and later returns to the applet's HTML page, method start is called again.
Method paint is called by the applet container after methods init and start. Method paint is also called when the applet needs to be repainted.
[Page 975]
Method stop is called by the applet container when the user leaves the applet's Web page by browsing to another Web page.
Method destroy is called by the applet container when the applet is being removed from
memory. This occurs when the user exits the browsing session by closing all the browser windows and may also occur at the browser's discretion when the user has browsed to other Web pages.
