Java applets are small programs that run inside a web browser or an applet viewer. They are designed to create interactive web applications using Java. Applets extend the Applet
class from the java.applet
package, which provides several methods for managing an applet’s lifecycle and interacting with its environment.
Understanding the methods of the Applet class in Java is essential for developing applet-based applications. This topic covers the lifecycle methods and other important methods that help in creating functional and interactive Java applets.
What is an Applet in Java?
An applet is a Java program that runs inside a browser or an applet viewer. Unlike standalone Java applications, applets do not have a main()
method. Instead, they rely on predefined lifecycle methods that the Java runtime calls automatically.
Key Features of Java Applets
✔ Executed inside a web browser using a Java plugin
✔ Does not have a main()
method, unlike regular Java programs
✔ Cannot access system resources directly for security reasons
✔ Used for animations, games, and interactive web applications
Lifecycle Methods of Applet
The Applet class defines a set of lifecycle methods that control an applet’s behavior from initialization to destruction. These methods are called automatically by the Java runtime environment.
1. init() Method
The init()
method is called once when the applet is first loaded. It is used for initialization tasks, such as setting up UI components or initializing variables.
Syntax:
public void init() {// Initialization code}
Example:
public void init() {System.out.println("Applet Initialized");}
✔ Best used for: Initializing variables, setting up UI components
2. start() Method
The start()
method is called after init() and each time the applet is restarted (e.g., when a user revisits the page). It is often used to start animations or background processes.
Syntax:
public void start() {// Code to start animation or thread}
Example:
public void start() {System.out.println("Applet Started");}
✔ Best used for: Starting animations, opening network connections
3. stop() Method
The stop()
method is called when the applet is stopped or the user leaves the page. It is used to pause ongoing tasks.
Syntax:
public void stop() {// Code to stop animation or thread}
Example:
public void stop() {System.out.println("Applet Stopped");}
✔ Best used for: Stopping animations, releasing resources
4. destroy() Method
The destroy()
method is called when the applet is permanently removed from memory. It is used to clean up resources.
Syntax:
public void destroy() {// Code to release resources}
Example:
public void destroy() {System.out.println("Applet Destroyed");}
✔ Best used for: Cleaning up memory, closing database connections
5. paint(Graphics g) Method
The paint(Graphics g)
method is used to draw graphics on the applet’s screen.
Syntax:
public void paint(Graphics g) {// Drawing code}
Example:
import java.applet.Applet;import java.awt.Graphics;public class MyApplet extends Applet {public void paint(Graphics g) {g.drawString("Hello, Java Applet!", 50, 50);}}
✔ Best used for: Drawing shapes, displaying text
Other Important Methods of Applet
Apart from lifecycle methods, the Applet class provides several utility methods to interact with the environment.
1. getParameter(String name)
Retrieves parameters passed from the HTML file.
Example:
String paramValue = getParameter("username");
✔ Best used for: Getting user inputs from an HTML page
2. getCodeBase() and getDocumentBase()
-
getCodeBase()
returns the URL of the applet’s.class
file. -
getDocumentBase()
returns the URL of the web page hosting the applet.
Example:
URL codeBase = getCodeBase();URL docBase = getDocumentBase();
✔ Best used for: Loading images, fetching resources
3. showStatus(String message)
Displays a message in the status bar of the browser.
Example:
showStatus("Applet Loaded Successfully");
✔ Best used for: Showing applet status updates
Creating and Running a Java Applet
Step 1: Write the Applet Code
Save the following Java program as MyApplet.java
.
import java.applet.Applet;import java.awt.Graphics;public class MyApplet extends Applet {public void paint(Graphics g) {g.drawString("Welcome to Java Applet!", 50, 50);}}
Step 2: Create an HTML File
Create an HTML file (applet.html
) to load the applet.
<html><body><applet code="MyApplet.class" width="300" height="200"></applet></body></html>
Step 3: Compile and Run the Applet
Compile the Java Code
Run this command in the terminal:
javac MyApplet.java
Run the Applet Using Applet Viewer
appletviewer applet.html
Advantages and Disadvantages of Java Applets
Advantages
✔ Lightweight and does not require installation
✔ Runs securely inside a web browser
✔ Can display graphics and animations easily
Disadvantages
✖ Requires Java plugin, which is no longer supported in modern browsers
✖ Cannot access local system resources
The Applet class in Java provides various methods for managing the applet’s lifecycle and interacting with the environment. Understanding these methods is crucial for working with graphical and interactive Java applications.
Key Takeaways
✔ init()
initializes the applet
✔ start()
begins execution
✔ stop()
pauses the applet
✔ destroy()
cleans up resources
✔ paint(Graphics g)
is used for drawing
Even though Java applets are now obsolete in modern web development, they remain an important concept for learning graphical programming in Java.