Home Engineering Applet Life Cycle in Java With Example

Applet Life Cycle in Java With Example

0
Applet Life Cycle in Java With Example

An applet Life cycle in java is all states of the panel that allows interaction with a Java program. An applet in java may move from one state to another depending upon a set of default behaviors inherited in the form of methods from the Applet class.

What is Applet Life Cycle in Java?

Applet life cycle refers to how an object is generated, started, halted, and destroyed throughout the course of an application’s operation.

The browser invokes these methods to execute the init(), start(), stop(), aint(), and destroy() methods in the applet life cycle.

Because the applet runs on the client-side, it takes less time to process. 

Applet Life Cycle Method Work:

There are five techniques and states in the applet life cycle in Java.

  • init() 
  • start() 
  • paint() 
  • stop() 
  • destroy()

◎ init() in Java

The init() method is used to start an applet. It is only invoked once during the initialization process. The web browser creates initialized objects. This approach is comparable to a Thread class birth state. 

◎ start() in Java

 The applet is started using the start() method. It is called after the init() method has been called. It is called every time the browser loads or refreshes. The start() method is dormant until the init() method is called. This technique is comparable to the Thread class’s initial state. 

◎ stop() in Java

The applet is stopped using the stop() method. It is called if the browser is closed or minimized, or when the application crashes unexpectedly. After calling the stop() method, we can use the start() method whenever we want. This approach is primarily used to tidy up code. This function is comparable to the Thread class’s blocked state.

◎ destroy() in Java

When we are finished with our applet task, we utilize the destroy() method to destroy the application. It is only used once. We can’t start() the applet once it’s been deleted. This approach is comparable to the Thread class dead state. 

paint() in Java: 

 The paint() method is used to paint any shape, such as a square, rectangle, trapezium, eclipse, and so on. The ClassGraphics parameter is utilized in the paint() method. In an applet, this Graphics class provides painting functionality. This technique is comparable to the Thread class’s runnable state. 

Must Read ➜ Application Layer Protocols

✪ How does an Applet Life Cycle work in JAVA?

  • An applet is a Java application that works in a client-side window and runs in any web browser. An applet is designed to be embedded within an HTML page because it runs in the browser and does not have a main () method.
  • Init(), start(), stop(), and destroy() methods are available in thejava.applet.Appletclassclass. 
  • Another paint method is provided by java.awt.Componentclass(). 
  • Any class that wants to be an Applet Class in Java must inherit the Applet class.

✪ Applet Life-Cycle Methods Syntax

► Init() Method in Applet Life Cycle

The syntax is as follows:

public void init() 

{ 

//initialized objects 

} 

► Start() Method in Applet Life Cycle

The syntax is as follows:

public void start() 

{ 

//initialize the applet's code 

} 

► Stop() Method in Applet Life Cycle

The syntax is as follows:

public void stop() 

{ 

//put an end to the applet code 

} 

► Destroy() Method in Applet Life Cycle

The syntax is as follows: 

public void destroy() 

{ 

//remove the applet's code 

} 

► Paint() Method in Applet Life Cycle

The syntax is as follows:

public void paint(Graphics graphics) 

{ 

//code for any forms 

}

The browser calls all of the methods listed above automatically.

We don’t need to make an explicit call. Even though, as we explained earlier, each method has its own specification to fully meet the criteria.

Must Read ➜ Congestion Control

► Applet Life Cycle Method Stages

The flow of the methods throughout an applet’s life cycle. 

applet life cycle stages

► The Life Cycle of an Applet in Java – syntax format

The syntax is as follows:

Applet is extended by the class MyLifeCycle. 

{ 

 public void init () 

{ 

//objects that have been initialized 

} 

public void start() 

{ 

//initialize the applet's code 

} 

public void paint(Graphics graphics) 

{ 

//code for any forms 

} 

public void stop() 

{ 

//put an end to the applet code 

} 

public void destroy 

{ 

//remove the applet's code 

} 

} 

► Life Cycle Of An Applet is managed by Java Plug-in software.

  • Using an HTML document.
  • The applet viewer tool was used to test the program, but in real-time, we only utilized it with an HTML file. 

Applet Life Cycle Examples

The following are some examples of how to use an HTML file to implement Applet Life Cycle: 

Example 1 

Java Code: AppletLifeCycle.java 

import java.applet.Applet;
import java.awt.Graphics;
@SuppressWarnings("serial")
public class AppletLifeCycle extends Applet
{
public void init()
{
System.out.println("1.I am init()");
}
public void start()
{
System.out.println("2.I am start()");
}
public void paint(Graphics g)
{
System.out.println("3.I am paint()");
}
public void stop()
{
System.out.println("4.I am stop()");
}
public void destroy()
{
System.out.println("5.I am destroy()");
}
} 

HTML Code: AppletLifeCycle.html 

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Applet Life Cycle</title>
</head>
<body>
<applet code="AppletLifeCycle.class" width="300" height="300"></applet>
</body>
</html> 

Output:

Applet Life Cycle in Java

After minimizing the applet-Output: 

  1. I am init()
  2. I am start()
  3. I am paint()
  4. I am stop()

After maximizing the applet-Output: 

Applet Life Cycle maximizing

After closing the applet-Output: 

  1. I am init()
  2. I am start()
  3. I am paint()
  4. I am stop()
  5. I am start()
  6. I am paint()
  7. I am stop()
  8. I am destroy()

The explanation for the above example: 

  • As we can see from the outputs, the init() method was only called once, as we described earlier. 
  • The init(), start(), and paint() functions are called in order when the application is run. 
  • If the applet is maximized, the start() and pain() methods are called one after the other.

Must Read ➜ Types of Routing Protocols

Example 2 

By using applet viewer tool applet life cycle: No need to write HTML code. Just write Java code and run. It is a testing purposes only. 

JavaCode: AppletLifeCycleWithAppletViewer.java 

import java.applet.Applet;
import java.awt.Graphics;
@SuppressWarnings("serial")
public class AppletLifeCycleWithAppletViewer extends Applet
{
public void init()
{
System.out.println("1.I am init()");
}
public void start()
{
System.out.println("2.I am start()");
}
public void paint(Graphics g)
{
System.out.println("3.I am paint()");
}
public void stop()
{
System.out.println("4.I am stop()");
}
public void destroy()
{
System.out.println("5.I am destroy()");
}
} 

Output: 

applet example

After minimizing the applet-Output: 

  1. I am init()
  2. I am start()
  3. I am paint()
  4. I am stop()

After maximizing the applet-Output: 

 After closing the applet-Output: 

  1. I am init()
  2. I am start()
  3. I am paint()
  4. I am stop()
  5. I am start()
  6. I am paint()
  7. I am stop()
  8. I am destroy()

Showing result shown in rectangle area on the applet window:

Example #3 

Java Code: AppletRectangleArea.java 

import java.applet.Applet;
import java.awt.Graphics;
@SuppressWarnings("serial")
public class AppletRectangleArea extends Applet {
private int breadth;
private int length;
public void init() {
length = 10;
breadth = 20;
}
public void paint(Graphics graphics) {
String rectangleArea="Area of rectangle is=>"+length*breadth;
graphics.drawString(rectangleArea, 20, 20);
}
} 

Output: 

The explanation for the above example: 

  • We didn’t specify start(), stop(), or destroy() methods in the preceding code, as you can see.
  • Even though the application completes the entire life cycle because JVM calls all of these methods. 
  • Initialize the rectangle length and width in the init() method. 
  • The rectangle area was shown using the drawstring() technique in the paint() method.