×

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:

Applet Life Cycle in Java
  • 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

Related Topics

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.