An applet is a small Java program that is designed to run within a web browser or an applet viewer. It is primarily used for creating dynamic and interactive web applications. Applets are part of the java.applet package and are now considered a legacy technology due to the evolution of more modern web development frameworks.
Key Features of Applets
- Platform-Independent: Applets are written in Java, making them platform-independent.
- GUI Support: They can create graphical user interfaces (GUIs) using the Abstract Window Toolkit (AWT).
- Embedded in Web Pages: Applets can be embedded in HTML pages and executed using a compatible browser.
- Secure Execution: Applets run in a restricted environment (sandbox) to ensure security.
- Dynamic and Interactive: Applets are often used for animations, games, and real-time data processing on websites.
Life Cycle of an Applet
An applet has a well-defined life cycle with the following methods:
- init():
- Called once when the applet is first loaded.
- Used for initializing resources like variables or starting background threads.
- start():
- Called after init() or when the applet becomes visible again (e.g., the user returns to the webpage).
- Used to resume operations or start animations.
- stop():
- Called when the applet is no longer visible (e.g., the user navigates away from the webpage).
- Used to pause animations or stop background threads.
- destroy():
- Called when the applet is removed from memory.
- Used to release resources like threads or file handles.
- paint(Graphics g):
- Called to draw the applet’s content on the screen.
- The Graphics object provides methods for rendering shapes, text, and images.
Creating a Simple Applet
Example:
import java.applet.Applet;
import java.awt.Graphics;
public class SimpleApplet extends Applet {
// Called when the applet is first loaded
public void init() {
System.out.println(“Applet initialized”);
}
// Called each time the applet is started
public void start() {
System.out.println(“Applet started”);
}
// Called to repaint the applet
public void paint(Graphics g) {
g.drawString(“Welcome to Java Applet!”, 50, 50);
}
// Called when the applet is stopped
public void stop() {
System.out.println(“Applet stopped”);
}
// Called when the applet is destroyed
public void destroy() {
System.out.println(“Applet destroyed”);
}
}
Running an Applet
- Create an HTML File: Applets are embedded in an HTML page using the <applet> tag (deprecated). For example:
<html>
<body>
<applet code=”SimpleApplet.class” width=”300″ height=”150″></applet>
</body>
</html>
- Use an Applet Viewer: Java provides the appletviewer tool to run applets for development purposes:
appletviewer SimpleApplet.html
Advantages of Applets
- Easy to embed dynamic content in web pages.
- Secure execution due to the sandbox model.
- Platform-independent and portable.
Disadvantages of Applets
- Browser Support: Most modern browsers have removed support for Java Applets due to security concerns.
- Performance: Applets may not be as fast as native applications or modern web technologies.
- Obsolete Technology: Applets are now deprecated in favor of newer frameworks like JavaFX, HTML5, and JavaScript-based libraries.
Alternatives to Applets
Due to the decline in applet usage, alternatives include:
- JavaFX: A modern framework for creating rich desktop and web applications.
- HTML5 and JavaScript: Popular for interactive web content.
- React.js and Angular: Frameworks for dynamic and responsive web applications.