Applet Life Cycle in Java

Reviews of webeduclick.com

Applet:

Applets are small java programs that are primarily used in internet computing. It can be transported over the internet from one computer to another and run using the Applet Viewer or any web browser that supports Java. An applet is like an application program, it can do many things for us. It can perform arithmetic operations, display graphics, create animation & games, play sounds and accept user input, etc.

Every Java applet inherits a set of default behaviours from the Applet class. As a result, when an applet is loaded, it undergoes a series of changes in its states. The methods to execute in the Applet Life Cycle are init(), start(), stop(), destroy().

The Applet Life Cycle is included in Fig:
applet life-cycle

  • Born or Initialization State
  • Running State
  • Idle State
  • Dead State

1. Born or Initialization State:

Applets enters the initialization state when it is first loaded. It is achieved by init() method of Applet class. Then applet is born.

public void init()
{
.............
.............
(Action)
}

2. Running State:

Applet enters the running state when the system calls the start() method of Applet class. This occurs automatically after the applet is initialized.

public void start()
{
.............
.............
(Action)
}

3. Idle State:

An applet becomes idle when it is stopped from running. Stopping occurs automatically when we leave the page containing the currently running applet. This is achieved by calling the stop() method explicitly.

public void stop()
{
.............
.............
(Action)
}

4. Dead State:

An applet is said to be dead when it is removed from memory. This occurs automatically by invoking the destroy() method when we want to quit the browser.

public void destroy()
{
.............
.............
(Action)
}

Applet Life Cycle Program:

import java.awt.*;
import java.applet.Applet;
import javax.swing.JOptionPane;
 public class AppletLifeCycle extends Applet 
{
  TextArea messages = new TextArea(10, 25);
   public AppletLifeCycle() {
        add(messages);
        messages.append("Constructor executed\n");
  }
   public void init() {        
        messages.append("init method executed\n");
   }
   public void start() 
 {
        messages.append("start method executed\n");
  }
  public void paint(Graphics g) 
{
        messages.append("Paint called\n");        
 }
  public void stop() 
 {
        JOptionPane.showMessageDialog(null, "Stop method executed");
  }
  public void destroy() 
 {        
        JOptionPane.showMessageDialog(null, "Destory method executed");
  }
}

<html>
   <head>
   </head>
   <body>
         <applet code = "AppletLifeCycle.class" width = "240" height = "360"></applet>
   </body>
</html>

Output:

applet-life-cycle-program-output