Applet Life Cycle



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




Applet Life Cycle
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.

24 thoughts on “Applet Life Cycle”

  1. Thanx for ur explanation sir it is very easy to learn sir i have a doubt

    In the above ‘LifeTest.java’ when am executing this file am not writing ‘init()’ method even though it is executing remaining methods but in ur explanation ‘init()’ method starts ‘start()’ method but how ‘start()’ method will called with out writing ‘init()’ method? please clear me sir

Leave a Comment

Your email address will not be published.