Way2Java

Applet Life Cycle

1st page given Applet Life Cycle, 2nd page given Applet API support methods and 3rd page given Applet Example.

Various states, an applet, undergoes between its object creation and object removal (when the job is over) is known as Applet Life Cycle. Each state is represented by a method. There exists 5 states represented by 5 methods. That is, in its life of execution, the applet exists (lives) in one of these 5 states.

These methods are known as "callback methods" as they are called automatically by the browser whenever required for the smooth execution of the applet. Programmer just write the methods with some code but never calls.

Following are the methods.

  1. init() method
  2. start() method
  3. paint() method
  4. stop() method
  5. destroy() method

These methods are known as Applet Life Cycle methods. These methods are defined in java.applet.Applet class except paint() method. The paint() method is defined in java.awt.Component class, an indirect super class of Applet.

Browser Responsibilities

The Applet Life Cycle methods are called as callback methods as they are called implicitly by the browser for the smooth execution of the applet. Browser should provide an environment known as container for the execution of the applet. Following are the responsibilities of the browser.

  1. It should call the callback methods at appropriate times for the smooth execution of the applet.
  2. It is responsible to maintain the Applet Life Cycle.
  3. It should have the capability to communicate between applets, applet to JavaScript and HTML, applet to browser etc.

Description of Applet Life Cycle methods

Even though, the methods are called automatically by the browser, the programmer should know well when they are called and what he can do with the methods. Following is the schematic representation of the methods.



Brief Description of Life Cycle Methods

Following is the brief description of the above methods.

  1. init(): The applet's voyage starts here. In this method, the applet object is created by the browser. Because this method is called before all the other methods, programmer can utilize this method to instantiate objects, initialize variables, setting background and foreground colors in GUI etc.; the place of a constructor in an application. It is equivalent to born state of a thread.
  2. start(): In init() method, even through applet object is created, it is in inactive state. An inactive applet is not eligible for microprocessor time even though the microprocessor is idle. To make the applet active, the init() method calls start() method. In start() method, applet becomes active and thereby eligible for processor time.
  3. paint(): This method takes a java.awt.Graphics object as parameter. This class includes many methods of drawing necessary to draw on the applet window. This is the place where the programmer can write his code of what he expects from applet like animation etc. This is equivalent to runnable state of thread.
  4. stop(): In this method the applet becomes temporarily inactive. An applet can come any number of times into this method in its life cycle and can go back to the active state (paint() method) whenever would like. It is the best place to have cleanup code. It is equivalent to the blocked state of the thread.
  5. destroy(): This method is called just before an applet object is garbage collected. This is the end of the life cycle of applet. It is the best place to have cleanup code. It is equivalent to the dead state of the thread.

After knowing the methods, let us know when they are called by the browser.

Observe, the init() and destroy() methods are called only once in the life cycle. But, start(), paint() and stop() methods are called a number of times.

HTML for Applet

We cannot open an applet directly in a browser, even though, the browser is meant to execute an applet. For every applet, a HTML file is to be written in which we give the name and address etc. of the applet.

Following is the example code for embedding applet in a HTML file.

	

The <applet> tag comes with many attributes of which code, width and height are important. The other, optional, we discuss later. The code attribute includes the name of the applet. width and height attributes give the size of applet window in the browser, in pixels.

Applet Execution Styles

There exist two styles of running the HTML file embedding the applet.

  1. As usual using a browser
  2. Using appletviewer

When you do not have a browser to test or run the applets, the JDK gives a tool, appletviewer. &quotappletviewer" can be run from the command-prompt.


Java API support for Applets

Java includes many libraries (known as packages) and classes, commonly known as Java API. java.applet is a small package used to develop applets. Following are the classes and interfaces of "java.applet" package.

Interfaces

  1. AppletContext: Used by the programmer to communicate with the execution environment of an applet. This is required to communicate between two applets.
  2. AppletStub: Used to develop custom applet viewers. Programmer can utilize this interface to communicate between applet and the browser environment
  3. AudioClip: Used to play audio clips. It comes with methods like play(), loop() and stop().

Class

  1. Applet: This class should be extended by our applet program. Applet is a class inherited from Panel of java.awt package.

Following is the hierarchy of Applet class.

Applet Hierarchy


Using Applet Life Cycle methods

With this knowledge of applets, let us write a small applet to understand the syntax of developing an applet. Let us start from life cycle methods.

As you can guess, two programs exist, one applet (LifeTest.java) and one HTML (Life.html) file.

1st Program: Applet program – LifeTest.java

import java.awt.Graphics;  
import java.applet.Applet;       
public class LifeTest extends Applet  
{  
    public void init()   
    {
 	System.out.println("init(): applet started");
    }
    public void start()   
    {
	System.out.println("start(): applet activated");
    }
    public void paint(Graphics g)   
    {
	System.out.println("paint(): applet running");
    }
    public void stop()   
    {
        System.out.println("stop(): applet inactivated ");
    }
    public void destroy()   
    {
	System.out.println("destroy(): applet destroyed");
    }
}  

2nd Program: HTML Program – Life.html





Screenshot of Life.html of Applet Life Cycle

Compilation and Execution

In compilation, no difference with applets; only execution differs.

Compilation C:\snr\way2java\applets> javac LifeTest.java
Execution C:\snr\way2java\applets> appletviewer Life.html

As discussed earlier, the above HTML file can be run in two ways. Anyhow, let us run this with appletviewer as println() methods do not work in a browser. We use browser in later applets.

All the five methods of Applet Life Cycle are included in the above applet program and stuffed with just println() statements; the best way to understand which method is called when. When applet is destroyed, the browser calls stop() and destroy() methods. The output will be obtained at DOS prompt. The above screenshot shows clearly.