Life Cycle of Java Applet
Last Updated :
05 Feb, 2025
An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works on the client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server. The Applet Container manages the entire life cycle of an applet. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class. Applets are not stand-alone programs. They run either within a web browser or an applet viewer.
Note:
Java applet is deprecated because it's no longer widely used on the web. The popularity of applets has decreased over the years as browser support for applets has declined, and more advanced technologies such as web-based applications and JavaScript have become more prevalent. Additionally, applets are considered a security risk as they can execute arbitrary code on the client machine, and many browsers now disable them by default. As a result, Java's applet technology is no longer seen as a valuable feature for Java developers and is removed from the newer versions of Java.
We can view our Applet with the help of a standard applet viewer tool called Applet Viewer. Unlike the general executions and outputs of the java programs, applet execution does not begin at main() method, and the output of an applet window is not catered by System.out.println(). Rather it is handled with various Abstract Window Toolkit (AWT) methods, such as drawString().
Let us do see a hierarchy of Applet before landing up on stages in the lifecycle of the java applet that is as follows in the below media:
Stages in the Life Cycle of Java Applet
- Initializing an Applet
- Starting the Applet
- Painting the Applet
- Stopping the Applet
- Destroying the Applet
In order to implement the Applet we need to import awt package,
java.awt.applet.*;
Life Cycle of Applet
Step 1: Initialization
Syntax of init():
public void init()
There is no main method unlike our normal java programs. Every Applet will start it's execution from init() method. It is executed only once
Step 2: Start
Syntax of start():
public void start()
After init() method start() method is invoked. Executed when the browser is maximized
Step 3: Paint
Syntax of paint():
public void paint (Graphics g)
Paint method is used to display the content on the applet. We can create the objects or components to the applet or we can directly write a message on the applet. It will take Graphics class as a parameter.
Step 4: Stop
Syntax of stop():
public void stop()
stop() method is used to stop the applet. It is executed when the browser is minimized.
Step 5: Destroy
Syntax of Destroy():
public void destroy()
destroy() method is used to completely close the applet. It is executed when the applet is closed.
Java Applet Implementation
Implementation of java Applet can be done in two ways as follows:
- Using HTML file
- Applet viewer tool
Way 1: Using HTML file
<HTML>
<applet>
code,width,height
</applet>
</HTML>
Note: Drawbacks of using HTML file is you need a plugin (java plugin) to run it on your browser.
Way 2: Applet viewer tool
Java Applet Life Cycle Methods
There are five methods of an Applet Life Cycle namely;
- init()
- start()
- paint()
- stop()
- destroy()
All these are available in AWT Packagejava.awt.applet.* and in order ton import paint (Graphics g) we do use java.awt.component package
Let's understand each method in a detailed manner :
1. init()
- This is the first method to be called
- Variables can be initialized here
- This method can be called only once during the run time of the applet
- It is invoked at the time of Initialization
Syntax of init():
public void init()
{
// To initialize objects
}
2. start()
- This method is called after init() method
- start() method is used for starting the applet
- It is also called to restart an applet after it has been stopped. i.e. to resume the applet
Syntax of start():
public void start()
{
// To start the applet code
}
Note: init() is called once i.e. when the first time an applet is loaded whereas start( ) is called each time an applet’s HTML document is displayed onscreen.
3. paint()
void paint(Graphics g){ }
- paint() method is used for painting any shapes like square, rectangle, trapeziums, etc.
- paint() method has one parameter of type Graphics Class, this Graphics class enables the painting features in an applet.
- This parameter will contain the graphics context, which is used whenever output for the applet is required.
Syntax of paint():
public void paint(Graphics graphics)
{
// Any shape's code
}
Note: This is the only method among all the method mention above, which is parameterized.
4. stop()
- It is invoked every time the browser is stopped, minimized or when there is an abrupt failure in the application.
- After stop()method called, we can also use start() method whenever we want.
- This method mainly deals with clean up code.
- The stop( ) method is called when a web browser leaves the HTML document containing the applet when it goes to another page, for example, when stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.
Syntax of stop():
public void stop()
{
// To stop the applet code
}
5. destroy()
- destroy() method is used to destroy the application once we are done with our applet work. It can be invoked only once.
- Once applet is destroyed we can’t start() the applet (we cannot restore the applet again)
- The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory.
Syntax of destroy():
public void destroy()
{
// To destroy the applet
}
Note: The stop( ) method is always called before destroy( )
Syntax: Entire Applet Life Cycle
Java
Class AppletLifeCycle extends Applet
{
public void init()
{
// Initializes objects
}
public void start()
{
// Starts the applet code
}
public void paint(Graphics graphics)
{
// Any shape's code
}
public void stop()
{
// Stops the applet code
}
public void destroy()
{
// Destroys the applet code
}
}
Implementation:
Example 1: In order to begin with Java Applet, let's understand a simple code to make the Applet
Java
// Java Program to Make An Applet
// Importing required classes from packages
import java.awt.*;
import java.awt.applet.*;
// Class 1
// Helper class extending Applet class
public class AppletDemo extends Applet
// Note: Every class used here is a derived class of applet,
// Hence we use extends keyword Every applet is public
{
public void init()
{
setBackground(Color.black);
setForeground(Color.yellow);
}
public void paint(Graphics g)
{
g.drawString("Welcome", 100, 100);
}
}
// Save file as AppletDemo.java in local machine
HTML
<html>
<applet code = AppletDemo
width = 400
height = 500>
</applet>
</html>
<!-- Save as Applet.html -->
Compilation methods
Now in order to generate output, do follow below undersigned to compile and run the above file:
- Method 1: Using command
- Method 2: Include the applet code in our java program.
Method 1: Using the command
Compilation:
c:> javac.AppletDemo.java
Execution:
Double click on Applet.html
This won't work on browser as we don't have the proper plugins.
Method 2: Include the applet code in our java program make sure to put this html applet code as comments as it is important evil as demonstrated below as follows:
Example:
Java
// Java Program to Illustrate Insertion of HTML File in
// Applet As Commands
// Importing required classes
import java.applet.*;
import java.awt.*;
// Note: Insertion of HTM:L file as comments
/* <applet code = AppletDemo width=400 height=500>
</applet>*/
// Java Program
// Class extending Applet
public class AppletDemo extends Applet {
public void init()
{
setBackground(Color.black);
setForeground(Color.yellow);
}
public void paint(Graphics g)
{
g.drawString("Welcome to Applets", 50, 50);
}
}
Compilation:
c:\> javac AppletDemo.java
Execution:
c:\> appletviewer AppletDemo.java
Similar Reads
Life Cycle of JSP
The life cycle of a JavaServer Page (JSP) consists of various phases that start from its creation, followed by its translation into a servlet, and finally managed by the servlet lifecycle. The JSP engine handles this process automatically. Steps of JSP Life Cycle Translation of JSP page to ServletCo
2 min read
Java Applet Class
Java Applet is a special type of small Java program embedded in the webpage to generate dynamic content. The specialty of the Java applet is it runs inside the browser and works on the Client side (User interface side). Note:Applets, which are small programs that can be run inside a web page, are no
4 min read
Life Cycle of a Servlet
The entire life cycle of a Servlet is managed by the Servlet container, which uses the jakarta.servlet.Servlet interface to understand the Servlet object and manage it. So, before creating a Servlet object, let's first understand the life cycle of the Servlet object, which is actually understanding
7 min read
Java - Close AWT Window
Java Abstract Window Toolkit (AWT) is a package that creates dynamic and interactive Graphical User Interfaces (GUIs) for Java applications. The ability to properly close AWT windows is an important aspect of developing user-friendly AWT applications. In this article, we will look into different met
7 min read
Java Applet Basics
Java Applets was once a very popular feature of web applications. Java Applets were small programs written in Java that ran inside a web browser. Learning about Applet helps us understand how Java has evolved and how it handles graphics. Note: java.applet package has been deprecated in Java 9 and la
9 min read
How to Use Swing Applet in Java?
In this article, we will be using the swing Applet or JApplet in Java. Here, we will make a simple multiplication application that will multiply the two input numbers. Approach to Using Swing Applet in JavaWe need to import the packages for Swing and AWT Components.Once the packages are imported, we
4 min read
Draw a Smiley in Java Applet
Given task is to draw a smiley face in Java Applet.Approach:Â Â Create three Ovals, one for the face, two for the eyes.Fill eyes oval with black color.Create an arc for the smile in the face. Below is the implementation of the above approach:Applet Program:Â [GFGTABS] Java // Java program to Draw a /
1 min read
Java Applet | Digital Stopwatch
This article will provide an instance of creating one type of stopwatch with Java Applet, AWT and Thread. We shall be using all these library to make a working model of a stopwatch. The GUI shall have 3 buttons for interaction, namely, start (to start the time), reset (to reset the time to default v
4 min read
Draw a Polygon in Java Applet
A polygon is a closed figure with a finite set of line segments joining one vertex to the other. The polygon comprises of set of (x, y) coordinate pairs where each pair is the vertex of the polygon. The side of the polygon is the line drawn between two successive coordinate pairs, and a line segment
3 min read
Draw an Olympic Symbol in Java Applet
Given task is to draw an Olympic symbol in Java Applet. Expected Output: Â Â Approach: Use drawOval() method to draw a circle with x and y coordinates and length and breadth. Below is the implementation of the above approach:Â Applet Program:Â [GFGTABS] Java // Java program to Draw an Olympic // Symb
1 min read