Life Cycle of an Applet

Introduction

 
Applets are small Java programs that are embedded in Web pages. They can be transported over the Internet from one computer (web server) to another (client computers). They transform the web into rich media and support the delivery of applications via the Internet. It is also a special Java program that can be embedded in HTML documents. A Java applet is a Java program written in a special format to have a graphical user interface. The graphical user interface is also called a GUI, and it allows a user to interact with a program by clicking the mouse, typing information into boxes, and performing other familiar actions. With a Java applet, GUIs are easy to create even if you've never run into such GUI before.
 

Life Cycle Of An Applet

 
These are the different stages involved in the life cycle of an applet:
  • Initialization State
  • Running state
  • Idle or stopped state
  • Dead state
  • Display state
appletlife.gif
Applet Life Cycle
 
Initialization State: This state is the first state of the applet life cycle. An applet is created by the method init(). This method initializes the created applet. It is Called exactly once in an applet's life when applet is first loaded, which is after object creation, e.g. when the browser visits the web page for the first time. Used to read applet parameters, start downloading any other images or media files, etc. Init() method should be overridden in our applet.
  1. Public void init()  
  2. {  
  3.     bgColor = Color.cyan;  
  4.     setBackground(bgColor);  
  5. }  
Running State: This state is the second stage of the applet life cycle. This state comes when start() method is called. Called at least once. Called when an applet is started or restarted, i.e., whenever the browser visits the web page.
  1. Public void start()  
  2. {  
  3.     super.start();  
  4. }  
Idle or Stopped State: This state is the third stage of the applet life cycle. This state comes when stop() method is called implicitly or explicitly.stop() method is called implicitly. It is called at least once when the browser leaves the web page when we move from one page to another. This method is called only in the running state and can be called any number of times.
 
It should be overridden in our applet.
  1. Public void stop()  
  2. {  
  3.     super.stop();  
  4. }  
Dead State: This state is the fourth stage of the applet life cycle. This state comes when destroy() method is called. In this state the applet is completely removed from the memory. It called exactly once when the browser unloads the applet. Used to perform any final clean-up. It occurs only once in the life cycle. It should be overridden in our applet.
  1. Public void destroy()  
  2. {  
  3.     super.destroy();  
  4. }  
Display State: This state is the fifth stage of the applet life cycle. This state comes when use s the applet displays something on the screen. This is achieved by calling paint() method.Paint() method can be called any number of times. This can be called only in the running state. This method is a must in all applets. It should be overridden in our applet.
  1. Public void paint(Graphics obj)  
  2. {  
  3.     super.paint(g);  
  4. }  
Example Application of Applet: Here we create two applications to understand the applet working.
 
App.java: In this application we simple print the "Hello Apllet world".
  1. import java.applet.Applet;  
  2. import java.awt.*;  
  3. public class app extends Applet   
  4. {  
  5.     Color bgColor;  
  6.     public void init()  
  7.     {  
  8.         bgColor = Color.cyan;  
  9.         setBackground(bgColor);  
  10.     }  
  11.     public void stop() {}  
  12.     public void paint(Graphics g)   
  13.     {  
  14.         g.drawString("Hello,Applet world!"20,15);  
  15.         g.drawArc(50,40,30,30,0,360);  
  16.     }  
  17. }  
AppletApplication.java: In this application we make a rectangle and fill it with color.
  1. import java.awt.*;  
  2. import java.applet.*;  
  3.   
  4. public class AppletApplication extends Applet  
  5. {  
  6.     Font bigFont;  
  7.     Color redColor;  
  8.     Color weirdColor;  
  9.     Color bgColor;  
  10.   
  11.     public void init()  
  12.     {  
  13.         bigFont = new Font("Arial",Font.BOLD,16);  
  14.         redColor = Color.red;  
  15.         weirdColor = new Color(60,60,122);  
  16.         bgColor = Color.cyan;  
  17.         setBackground(bgColor);  
  18.     }  
  19.     public void stop()  
  20.     {  
  21.     }  
  22.     public void paint(Graphics g)  
  23.     {  
  24.         g.setFont(bigFont);  
  25.         g.drawString("Shapes and Colors",80,20);  
  26.         g.setColor(redColor);  
  27.         g.drawRect(100,100,100,100);  
  28.         g.fillRect(110,110,80,80);  
  29.         g.setColor(weirdColor);  
  30.         g.fillArc(120,120,60,60,0,360);  
  31.         g.setColor(Color.yellow);  
  32.         g.drawLine(140,140,160,160);  
  33.         g.setColor(Color.black);  
  34.     }  
  35. }  
Run the Application: Run it on the browser.
 
App.java
 
hello,world.gif
 
ApplicationApplet
 
appletapplication.gif