Applet life cycle with exmaple

Java applet derive features from the class Applet. whenever an applet is design, it experience series of changes from initialization to destruction. different- different stages of an applet life cycle are characterize in the figure applet life cycle.

How Applet life cycle works

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

applet life cycle methods in Brief

  1. [highlight color=”green”]init():[/highlight] 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. [highlight color=”green”]start(): [/highlight]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. [highlight color=”green”]paint():[/highlight] 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. [highlight color=”green”]stop():[/highlight] 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.

[highlight color=”green”]destroy():[/highlight] 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.
Applet life cycle Program Example

/*
   Applet Life Cycle Example
   This java example explains the life cycle of Java applet.
*/
import java.applet.Applet;
import java.awt.Graphics;
/*
 *
 * Applet can either run by browser or appletviewer application.
 * Define <applet> tag within comments as given below to speed up
 * the testing.
 */
 
/*
<applet code="AppletLifeCycleExample" width=100 height=100>
</applet>
*/

public class AppletLifeCycleExample extends Applet{
       
         /*
         * init method is called first.
         * It is used to initialize variables and called only once.
         */
        public void init() {
                super.init();
        }
       
        /*
         * start method is the second method to be called. start method is
         * called every time the applet has been stopped.
         */
        public void start() {
                super.start();
        }
       
        /*
         * stop method is called when the the user navigates away from
         * html page containing the applet.
         */
        public void stop() {
                super.stop();
        }
       
        /* paint method is called every time applet has to redraw its
         * output.
         */
        public void paint(Graphics g) {
                super.paint(g);
        }
       
        /*
         * destroy method is called when browser completely removes
         * the applet from memeory. It should free any resources initialized
         * during the init method.
         */
        public void destroy() {
                super.destroy();
        }
}