Applet Life Cycle in Java
In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example program.
Applet
A Java application that could be included in a website page is called an applet. It functions client-side and operates inside the internet browser. The APPLET as well as OBJECT element is used to include an applet into an HTML page, which is then stored on a web application. The Applet Container is responsible for overseeing an applet's whole life cycle. All applets are subclasses of the java.applet.Applet class, either explicitly or implicitly. Applets do not operate independently. They function either through an applet viewer or an internet browser.
A common applet viewer software named Applet Viewer can be used to see our applet. Applet processing does not start at the main() method, and System.out.println()is not used to handle the outputs of an applet frame, unlike the typical iterations and outputs of Java applications . Instead, it is handled via different AWT (Abstract Window Toolkit) techniques, like drawString().
Applet Stages
In life cycle of an applet there exist generally 5 stages. The stages are named after the methods. They are namely:

- Initiating an applet
- Activating the applet
- Painting of an applet
- Closing the applet
- Destruction of the applet
Now we discuss each stage mentioned above and what are the methods used in every stage.
Initiating an applet
The applet is initialized by running the init() method first. It can only be used once for initialization. The instantiated objects are created by the web browser, which executes the init() method inside this applet after confirming the security controls.
Activating the applet
The applet is started via the start() method, which also includes the applet's real code. It is called immediately following the init() method. The start() method is used each time the website is updated or reloaded. Additionally, it is called if the applet is restored, enlarged, or navigated between browser tabs. Prior to calling the init() method, it is in an idle state.
Painting of an applet
The data of the applet is shown by using paint() approach. Either explicitly compose a message on the applet or develop the necessary objects or modules for the applet. The class for graphics will be a parameter.
Closing the applet
The applet's running is stopped with the stop() method. Every time the applet is shrunk, paused, or moved from one page to the next in the browsers, this stop() method is called. Start() is called once more when we return to that webpage.
Destruction of the applet
Just after applet has completed its task, the destroy() method terminates it. Whenever the window displaying the webpage is closed or just the applet window is closed, it is called. It only runs once and deletes the applet instance from memory. Once it is destroyed, we are unable to restart the applet.
Applet Life Cycle Mechanism
- The life cycle of the applet is managed by the Java plug-in technology.
- An applet is an user Java application that runs in any internet browser. It operates in the browser, hence it lacks the main() method. Thus, it is designed to be included to an HTML page.
- The applet.Applet class is where the init(), start(), stop(), and destroy() methods exist.
- The awt.Component class contains the paint() function.
- In Java, we must inherit the Applet class if we intend to convert a class into an applet class.
- An instance of the current Applet class is created each time an applet is created. We are able to use all of that class's methods as a result.
Syntax
Class AppletLC extends Applet
{
public void init()
{
// Initiating an applet
}
public void start()
{
// Activating the applet
}
public void paint(Graphics)
{
// Painting of an applet
}
public void stop()
{
// Terminating the applet
}
public void destroy()
{
// Destruction of the applet
}
}
Now lets us understand the applet life cycle with an example program
File name: Appletlifecycle1.java
import java.awt.*;
import java.applet.*;
public class Appletlifecycle1 extends Applet
{
String msg="";
public void init() //this method is executed when an applet is loaded
{
System.out.println("Initializing the applet");
setBackground(Color.green); //set background color for applet frame
setForeground(Color.red); //set foreground color for text in frame
Font f=new Font("Arial",Font.BOLD,25); //set font for text in applet
msg=msg+" init "; //store method name in msg
}//init
public void start() //this method is executed after init()
{
System.out.println("Starting of the applet");
msg=msg+" start ";
}//start
public void stop() //to stop the applet
{
System.out.println("Stopping the applet");
msg=msg+" stop ";
}//stop
public void destroy() //to remove applet from memory
{
System.out.println("Terminating the applet");
msg=msg+" destroy ";
}//destory
public void paint(Graphics g) //to display message in applet window
{
System.out.println("Displaying the content in the applet");
msg=msg+" paint ";
g.drawString(msg,50,30);
g.drawRect(10,50,40,30);
g.fillRect(60,60,30,70);
g.fillOval(140,160,170,170);
g.setColor(Color.RED);
g.drawArc(280,210,250,220,30,90);
g.drawLine(100,140,230,10);
showStatus("This is status bar in applet");
}//paint
}//AppletLifeCycle
Output
Initializing the applet
Starting of the applet
Displaying the content in the applet
Stopping the applet
Starting of the applet
Displaying the content in the applet
Terminating the applet