×

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

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

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.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.