The Applet class in Java is part of the java.applet package and is the base class for creating applets. Applets are small Java programs that run within a web browser or an applet viewer. The Applet class provides the foundational methods and functionality required to develop applet-based applications.
Inheritance Hierarchy
The Applet class extends the Panel class from the AWT package, which in turn extends the Container class. The hierarchy is as follows:
java.lang.Object
↳ java.awt.Component
↳ java.awt.Container
↳ java.awt.Panel
↳ java.applet.Applet
Key Features
- Event Handling: The Applet class can handle user input events such as mouse clicks and keyboard interactions using the AWT event-handling model.
- Graphics Support: It provides methods to render graphical components using the Graphics class.
- Lifecycle Management: The Applet class defines the lifecycle of an applet through methods like init(), start(), stop(), and destroy().
- HTML Integration: Applets can interact with HTML parameters passed via the <applet> tag using the getParameter() method.
Important Methods in the Applet Class
Method | Description |
init() | Initializes the applet. Called once after the applet is loaded. |
start() | Starts or resumes the execution of the applet. Called after init() or when the applet becomes visible. |
stop() | Pauses the execution of the applet when it is no longer visible. |
destroy() | Cleans up resources before the applet is terminated. |
paint(Graphics g) | Used to draw graphics or text on the applet. |
getParameter(String name) | Retrieves a parameter passed to the applet from the HTML page. |
getAppletContext() | Returns the context of the applet, allowing communication with other applets or the environment. |
resize(int width, int height) | Resizes the applet window. |
Lifecycle of an Applet
The Applet class lifecycle has four main phases, each represented by a method:
- Initialization (init()):
- Executed once when the applet is loaded.
- Used for initializing variables, setting up the UI, or allocating resources.
- Starting (start()):
- Executed each time the applet becomes visible or active.
- Used for starting animations or resuming activities.
- Stopping (stop()):
- Executed when the applet is no longer visible or the user navigates away from the page.
- Used for pausing threads or stopping animations.
- Destruction (destroy()):
- Executed once before the applet is removed from memory.
- Used for releasing resources like files, threads, or sockets.
Example of an Applet
import java.applet.Applet;
import java.awt.Graphics;
public class AppletExample extends Applet {
// Initialization method
@Override
public void init() {
System.out.println(“Applet Initialized”);
}
// Start method
@Override
public void start() {
System.out.println(“Applet Started”);
}
// Stop method
@Override
public void stop() {
System.out.println(“Applet Stopped”);
}
// Destroy method
@Override
public void destroy() {
System.out.println(“Applet Destroyed”);
}
// Paint method
@Override
public void paint(Graphics g) {
g.drawString(“Hello, Applet!”, 50, 50);
}
}
HTML to Run the Applet
html
Copy code
<html>
<body>
<applet code=”AppletExample.class” width=”300″ height=”200″>
</applet>
</body>
</html>
- Save this HTML file in the same directory as the compiled AppletExample.class file.
Running the Applet
- Compile the Java file:
javac AppletExample.java
- Use the appletviewer tool to view the applet:
appletviewer AppletExample.html
Advantages of Applet Class
- Simplifies the creation of graphical and interactive web-based applications.
- Provides secure execution in a sandboxed environment.
- Allows interaction with HTML content using parameters.
Limitations of the Applet Class
- Deprecated Technology: Browsers have phased out support for applets due to security and compatibility concerns.
- Performance Issues: Applets can be slower compared to modern alternatives like JavaFX or JavaScript.
- Dependency on Java Plugin: Requires a compatible Java plugin, which is no longer supported in most modern browsers.