Updated March 28, 2023
Introduction to Applet Life Cycle
Applet life cycle defined as how the object created, started, stopped and destroyed during the entire execution of the application is said to applet life cycle. Applet life cycle has 5 methods init(), start(), stop(), aint() and destroy().These methods are invoked by the browser to execute. Applet works on the client side so, less processing time.
Methods of Applet Life Cycle
Applet life cycle has 5 methods. Methods are init(), start(), paint(), stop() and destroy().
- init(): init() method is used to initialize an applet. It is invoking only once at the time of initialization. Initialized objects are created by the web browser. We can compare this method with a Thread class born state.
- start(): start() method is used to start the applet. It is invoking after the init()method invoked. It is invoking each time when browser loading or refreshing. Until init() method is invoked start() method is inactive state. We can compare this method with Thread class start state
- stop(): stop() method is used to stop the applet. It is invoked every time when browser stopped or minimized or abrupt failure of the application. Afterstop()method called, we can also start() method whenever we want. This method mainly deals with clean up code. We can compare this method with Thread class blocked state
- destroy(): destroy() method is used to destroy the application once we have done with our applet work. It is invoked only once. Once applet is destroyed we can’t start()the applet. We can compare this method with Thread class dead state
- paint(): paint() method is used for painting any shapes like square, rectangle, trapezium, eclipse, etc. paint() method has parameter as ClassGraphics. This Graphics class gives painting features in an applet. We can compare this method with Thread class runnable state.
How does Applet Life-Cycle Works in Java?
1. Applet is a Java application that runs in any web browser and working at a client-side window. As it is running in the browser so, it doesn’t have a main () method so Applet is designed to be placed within an HTML page.
2. java.applet.Appletclass provides init(), start(), stop() and destroy() methods.
3. java.awt.Componentclass provides another method of paint().
4. In Java, if any class wants to become Applet Class, it must inherit the Applet class.
5. init() method
Syntax:
public void init()
{
//initialized objects
}
6. start() method
Syntax:
public void start()
{
//start the applet code
}
7. stop() method
Syntax:
public void stop()
{
//stop the applet code
}
8. destroy() method
Syntax:
public void destroy()
{
//destroy the applet code
}
9. paint() method
Syntax:
public void paint(Graphics graphics)
{
//any shapes code
}
10. All of the above methods are automatically called by the browser. We no need to call explicitly. Even though each method has its own specification to full fill the requirement as we discussed above.
The flow of the methods is given below.
11. Entire Applet Life Cycle
Syntax:
Class MyLifeCycle extends Applet
{
public void init()
{
//initialized objects
}
public void start()
{
//start the applet code
}
public void paint(Graphics graphics)
{
//any shapes code
}
public void stop()
{
//stop the applet code
}
public void destroy()
{
//destroy the applet code
}
}
12. Applet life cycle is managed by Java Plug-in software.
13. There are 2 ways to run the applet.
- By using an HTML file.
- By using the applet viewer tool, this is for testing the application but real-time we simply used it with HTML file only.
Examples to Implement
Below are the examples to implement Applet Life Cycle by using HTML file:
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:
After minimizing the applet-Output:
After maximizing the applet-Output:
After closing the applet-Output:
Explanation:
- As we can see in the outputs init() method invoked only once as we discussed in the above.
- When we run the application init(), start() and paint() methods are invoked sequentially.
- If you minimize the applet then stop () method invoked.
- Again if you maximize the applet then start() and pain() methods invoked one after the other.
- If you close the application then destroy() method will be invoked.
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 purpose 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:
After minimizing the applet-Output:
After maximizing the applet-Output:
After closing the applet-Output:
Showing 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:
Explanation:
- As you can see in the above code we did not specify start(), stop() and destroy() methods. Even though the program executes the entire life cycle because all this methods are invoked by JVM.
- In the init() method initialize the rectangle length and breadth.
- In the paint() method showed the rectangle area by using drawstring() method.
- Object for this is created by the web browser.
Recommended Articles
This is a guide to Applet Life Cycle. Here we discuss the How does Applet Life Cycle Methods Work in Java and its different methods along with its examples. You can also go through our other suggested articles to learn more –