The Applet life cycle consists of a series of methods that are invoked sequentially during the execution of an applet. These methods provide a well-defined structure for initializing, starting, stopping, and destroying an applet. The life cycle is managed by the Java Virtual Machine (JVM) and is essential for applet-based applications.
Phases of the Applet Life Cycle
An applet’s life cycle consists of four key phases, each represented by a specific method:
- Initialization (init())
- Starting (start())
- Stopping (stop())
- Destruction (destroy())
In addition, the paint(Graphics g) method is used to render the applet’s content visually.
Applet Life Cycle Methods
1. init()
- Purpose: Initializes the applet. Called only once when the applet is loaded into memory.
- Usage: Used to allocate resources, initialize variables, and set up the user interface.
- Example:
public void init() {
System.out.println(“Applet initialized”);
// Initialization logic
}
2. start()
- Purpose: Starts or resumes the applet’s execution. Called after init() or when the applet becomes visible again.
- Usage: Used to start animations, threads, or any activity that needs to run when the applet is active.
- Example:
public void start() {
System.out.println(“Applet started”);
// Start or resume tasks
}
3. stop()
- Purpose: Pauses the applet’s execution. Called when the applet is no longer visible (e.g., the user navigates away from the page).
- Usage: Used to stop animations, threads, or background tasks.
- Example:
public void stop() {
System.out.println(“Applet stopped”);
// Pause or stop tasks
}
4. destroy()
- Purpose: Cleans up resources before the applet is removed from memory. Called once when the applet is terminated.
- Usage: Used to release resources like threads, file handles, or network connections.
- Example:
public void destroy() {
System.out.println(“Applet destroyed”);
// Cleanup logic
}
5. paint(Graphics g)
- Purpose: Used to render the applet’s visual content. Called whenever the applet needs to redraw itself.
- Usage: Typically used to display text, shapes, or images.
- Example:
public void paint(Graphics g) {
g.drawString(“Welcome to the Applet Life Cycle!”, 50, 50);
}
Applet Life Cycle Flow
- Loading the Applet:
- The applet is loaded, and the init() method is invoked to perform initialization.
- Starting the Applet:
- The start() method is called immediately after init() and whenever the applet becomes visible.
- Running the Applet:
- The applet can handle user interactions, execute threads, or render visual elements.
- Stopping the Applet:
- The stop() method is called when the user navigates away from the applet’s webpage.
- Destroying the Applet:
- The destroy() method is called when the applet is removed from memory, typically during browser shutdown or page unload.
Complete Example: Applet Life Cycle
import java.applet.Applet;
import java.awt.Graphics;
public class LifeCycleApplet extends Applet {
// Called once when the applet is loaded
public void init() {
System.out.println(“init() called”);
}
// Called every time the applet starts
public void start() {
System.out.println(“start() called”);
}
// Called to draw the applet’s content
public void paint(Graphics g) {
g.drawString(“Applet Life Cycle Demo”, 50, 50);
System.out.println(“paint() called”);
}
// Called when the applet is stopped
public void stop() {
System.out.println(“stop() called”);
}
// Called once before the applet is destroyed
public void destroy() {
System.out.println(“destroy() called”);
}
}
HTML Code to Run the Applet
<html>
<body>
<applet code=”LifeCycleApplet.class” width=”300″ height=”150″>
</applet>
</body>
</html>
Execution Steps
- Compile the Applet:
javac LifeCycleApplet.java
- Run with Applet Viewer:
appletviewer AppletLifeCycle.html
Applet Life Cycle Sequence
- init() is called once when the applet is loaded.
- start() is called immediately after init() and whenever the applet becomes active.
- paint(Graphics g) is called whenever the applet needs to be redrawn.
- stop() is called when the applet is no longer visible.
- destroy() is called when the applet is removed from memory.
Advantages of Applet Life Cycle
- Provides a structured framework for applet execution.
- Ensures proper resource allocation and cleanup.
- Simplifies event handling through predefined methods.
Limitations
- Modern browsers no longer support applets due to security concerns.
- Applets require a Java plugin, which is obsolete in many environments.
- Limited to the functionality provided by the browser’s Java environment.