The Applet class in Java is used to create applets, which are small Java programs that run within a web browser or an applet viewer. These applets are typically used for interactive web applications, such as games, animations, and dynamic graphics.
Java applets extend the Applet
class from the java.applet
package and are executed within a secure sandbox environment. The Applet class provides several built-in methods to control the applet’s lifecycle, handle user interactions, and manage graphics.
In this topic, we will explore:
- What Java Applets are
- The lifecycle methods of the Applet class
- Other important methods used in Java applets
- How to create and run an applet in Java
What is an Applet in Java?
An applet is a special type of Java program that runs inside a web browser or an applet viewer. Unlike standalone Java applications, applets do not have a main()
method. Instead, they rely on callback methods provided by the Applet
class.
Key Features of Java Applets
✔ Executed inside a browser using a Java plugin
✔ Requires an HTML file to run inside a browser
✔ Cannot access system resources directly for security reasons
✔ Lightweight and interactive, used for animations and graphics
Lifecycle Methods of Applet Class
The Applet
class provides several lifecycle methods that are automatically called during an applet’s execution. These methods define how an applet is initialized, started, stopped, and destroyed.
1. init() Method
The init()
method is called only once when the applet is first loaded. It is used to initialize variables and perform setup operations.
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 whenever the applet is restarted. It is used to start animations or threads.
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 minimized. It is used to pause animations or threads.
Syntax:
public void stop() {// Code to stop animation or thread}
Example:
public void stop() {System.out.println("Applet Stopped");}
✔ Best used for: Pausing animations, stopping background tasks
4. destroy() Method
The destroy()
method is called when the applet is permanently removed. It is used to release 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 Class
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.