Published: 22 Nov 2025 | Reading Time: 5 min read
In web development, Java applets were a standard tool for delivering dynamic and interactive content on web pages. Due to browser maturity and security concerns, applets have become ineffective. While this is still the case, understanding applets is an integral part of learning about web technology and more about how earlier interactive content was integrated into sites.
This article discusses all the fundamentals applet in java, starting from their definition and life cycle to talking to web pages, handling user events, showing images, and even sounds. We will also discuss how to convert typical Java programs into applets and learn their pros and cons.
An applet in Java is a small program that is executed within a web browser. In contrast to stand-alone Java programs, which are executed on a user's desktop, applets are written to be embedded into a web page. Applets are based on the Java plugin in browsers, which executes the applet's bytecode.
Java applets, when they were popular, were the tools that developers could use to make visually appealing, interactive web pages without the need for the browser's native functionalities or a complex client-server setup. Currently, applets are predominantly used in the continued existence of old-style applications. Due to the presence of more secure and versatile technologies like HTML5, CSS3, JavaScript, and WebAssembly, applets have not been used substantially in modern web development.
Java applets are small Java programs that operate within a web browser from a Java plugin. They were once used to offer interactive content such as games, animations, and forms on a web page. Java applets need the Java Runtime Environment (JRE) installed on the client machine.
Key Characteristics:
IFTTT applets are programmable features through which individuals can program triggers and operations for specific events between various applications and services. A sample of an IFTTT applet could be an instance that posts automatically on Twitter whenever an individual posts a fresh image on Instagram.
Key Characteristics:
Grail applets are the tiny components or coding fragments used in the Grail project, a web browser for Python programming. The Grail browser is experimental, and its applets were designed to provide additional functionality to the browser, like an enhanced user interface or exotic web interactions.
Key Characteristics:
A local applet is executed on the user's local machine (e.g., a personal computer). It does not need internet access and can interact with local resources such as files or hardware devices.
Key Characteristics:
A remote applet is executed from a remote server over the Internet. It can be controlled by a web browser and read or send data to a remote server.
Key Characteristics:
Web-based applets are tiny applications downloaded and executed via a web browser. Mobile applets, however, are tiny applications intended to run on cellular devices (smartphones and tablets). Both applets usually interact with the Internet and are utilized for web browsing, media streaming, or other lightweight functionality.
Key Characteristics:
Signed applets are digitally signed Java applets by a trusted certificate authority (CA). They assure their authenticity, have not been altered, and can be given extra privileges to access resources on the user's machine.
Key Characteristics:
Self-signed applets are Java applets that are signed digitally, but the signature is generated by the applet builder, not by some trusted certificate authority. Such applets can likely check for their own integrity, but they will remain untrusted to other users unless they explicitly accept the signature.
Key Characteristics:
Unsigned applets are Java applets that are not digitally signed. Without a certificate, web browsers will typically identify them as being insecure, and they will not run in new browsers or operating systems that rigorously follow security policy.
Key Characteristics:
The classes of Java applets form an explicit hierarchy, which essentially represents their structure, inheritance, and features. Knowing this hierarchy is to a large extent the basis for understanding the operation of applets and their connection with other Java classes.
Applets are basically subclasses of the Applet class, a class in the java.applet package. The class is the primary support framework for an applet, managing the life cycle of the applet by methods like init(), destroy(), and resource management. So, any applet you write is by definition a subclass of the Java Applet class, and, therefore, it must be extended to get the functional inheritance.
The typical inheritance hierarchy for an AWT-based applet is as follows:
For Swing-based applets, the hierarchy extends further:
An applet skeleton refers to the minimal structure required for an applet. It typically involves creating a class that extends the Applet class and overrides methods such as init(), start(), stop(), destroy(), and paint(). These methods manage the applet's lifecycle and graphical output.
The Java Applet lifecycle defines an applet's various stages from start to finish. The browser or executing applet viewer controls the lifecycle and includes the following key methods, which are part of the java.applet.Applet (or javax.swing.JApplet for Swing) class.
Example:
public void init() {
// Initialization code, like setting up graphics or loading resources
System.out.println("Applet Initialized");
}
Example:
public void start() {
// Code to start animations or threads
System.out.println("Applet Started");
}
Example:
public void paint(Graphics g) {
// Drawing code
g.drawString("Hello, World!", 20, 20);
}
Example:
public void stop() {
// Stop animations or threads
System.out.println("Applet Stopped");
}
Example:
public void destroy() {
// Cleanup code, release resources
System.out.println("Applet Destroyed");
}
Executing an applet is a little old-fashioned these days since applets were originally intended to be executed within web browsers through Java. Nevertheless, Java applets have mostly fallen out of use, and their support has been disabled in most web browsers. Still, if you're using Java applets and wish to execute one locally or within a system that still maintains support for them, this is how you accomplish it:
Ensure you've installed JDK on your machine before executing an applet. You can download it from Oracle's official website or use OpenJDK, which is open source and free.
You'll have the applet code as a .java file, inheriting java.applet.Applet or extending javax.swing.JApplet. Below is the example code for creating an applet in java:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, Applet World!", 50, 60);
}
}
You must compile the Java code into bytecode (a.class file) by using the javac command.
javac HelloWorldApplet.java
This will generate a HelloWorldApplet.class file.
Since browsers no longer natively support applets, you can use an HTML file containing the applet tag to load and execute the applet. You can do this by simply following these steps:
<html>
<body>
<applet code="HelloWorldApplet.class" width="300" height="300">
</applet>
</body>
</html>
Java ships with the appletviewer utility, which you can use to execute an applet independent of a browser.
appletviewer applet.html
This will execute the applet in the appletviewer environment, a window miming a browser's applet behavior.
Suppose you want an alternative to running an applet and you want to execute the applet's functionality on a newer system. In that case, you can re-encode the applet as a regular Java application using JFrame or other GUI objects. Applets are usually no longer needed because Swing and JavaFX are better for GUI programming.
Java applets support user interaction through a robust set of event-handling mechanisms. By leveraging interfaces like ActionListener and using GUI components such as Button and Label, applets can respond to user actions like clicks, key presses, and more. Event handling is essential for building interactive applets that react to user input within the sandbox environment provided by the browser or applet viewer.
In order to access event handling and GUI features, applets generally import java.applet and java.awt packages. To be able to respond to user actions (like pressing a button), applets implement listener interfaces like ActionListener and provide the implementation of their methods.
Below is a simplified version inspired by eventhandling.java and EventAppletDemo.java, demonstrating how to add a Button to an applet and update a Label when the button is clicked.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class EventAppletDemo extends Applet implements ActionListener {
Button myButton;
Label myLabel;
public void init() {
myButton = new Button("Click Me");
myButton.addActionListener(this); // Register event handler
add(myButton);
myLabel = new Label("Waiting for click...");
add(myLabel);
}
public void actionPerformed(ActionEvent e) {
myLabel.setText("Button clicked!");
}
public void stop() {
// Clean up resources if needed when the applet is stopped
}
}
Key Points:
You can also use parameters to configure event-driven behavior or display messages dynamically, further enhancing interactivity.
Java applets can effectively develop customer-friendly and attractive experiences by using the AWT (Abstract Window Toolkit) and other classes that are associated with it. Through the use of the graphics class and different drawing methods, developers are capable of generating figures, pictures, words, and also multimedia content such as sound and animation, all in the graphical user interface of the applet.
The core of applet graphics lies in overriding the paint() method. The paint(Graphics g) method receives a Graphics object, which provides powerful drawing capabilities.
Example: Drawing Shapes and Text
public void paint(Graphics g) {
g.drawString("Hello, Applet!", 20, 20); // Draw text
g.drawRect(50, 50, 100, 60); // Draw rectangle
}
To display images, use the getImage() method to load an image and drawImage() method to render it. The ImageObserver interface is often used to track image loading.
Image img;
public void init() {
img = getImage(getCodeBase(), "picture.jpg"); // Load image
}
public void paint(Graphics g) {
g.drawImage(img, 10, 10, this); // 'this' is the ImageObserver
}
Applets can display animation by repeatedly updating the graphics displayed in the paint() method, often using timers or separate threads to trigger repaints.
For interactive graphics, implement MouseMotionListener to respond to mouse movements and drags, allowing for features like drawing or painting within the applet window.
Applets can play audio clips by using the AudioClip interface and methods like getAudioClip() to load sounds and play() to trigger playback.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, World!", 20, 20);
}
}
Explanation:
Output:
// The output will be a simple window with the string
"Hello, World!"
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
String message;
public void init() {
message = getParameter("message");
}
public void paint(Graphics g) {
g.drawString(message, 20, 20);
}
}
Explanation:
HTML (for invoking the applet):
<applet code="HelloWorldApplet.class" width="300" height="300">
<param name="message" value="Hello from HTML!">
</applet>
Output:
// When the applet runs, it will display the message
"Hello from HTML!"
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;
public class HelloWorldApplet extends Applet implements MouseListener {
public void init() {
addMouseListener(this); // Registers the applet as a listener for mouse events
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
// Other mouse event methods can be implemented as needed
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Explanation:
Output:
// When you click inside the applet window, the message printed
"Mouse clicked!"
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class ImageApplet extends Applet {
Image img;
public void init() {
img = Toolkit.getDefaultToolkit().getImage("image.jpg"); // Load the image
}
public void paint(Graphics g) {
g.drawImage(img, 20, 20, this); // Draw the image at position (20, 20)
}
}
Explanation:
Output:
// The applet window will display the image
image.jpg
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class AudioApplet extends Applet {
AudioClip clip;
public void init() {
URL url = getClass().getResource("audio.wav"); // Load the audio file
clip = getAudioClip(url); // Get the audio clip
}
public void start() {
clip.play(); // Start playing the audio
}
public void stop() {
clip.stop(); // Stop the audio when the applet is stopped
}
}
Explanation:
Output:
// When it starts, the applet will play the audio file. If the audio file is not found, it won't play.
audio.wav
| Aspect | Applets | Applications |
|---|---|---|
| Execution Environment | Run in a web browser or applet viewer | Run independently of a web browser |
| System Access | Limited in access to the system (sandboxed for security) | Permitted to access system resources such as files, network, etc. |
| Primary Use | Primarily used for interactive web page content | Used for standalone programs, including those with a GUI |
| Main Method | No main() method; lifecycle managed by the browser | Contain a main() method as an entry point and are typically more capable |
QuickTime is an online multimedia platform upon which videos, audio, and other media types are played within an applet. QuickTime applets can be incorporated into web pages so users are able to watch movies or hear music inside their browser using nothing but their browser.
Applets which enable users to enter mathematical functions and view the graphs in real time. Such programs can enable students and scientists to understand advanced mathematical concepts using visual representation.
Like QuickTime, Windows Media Player streams audio and video media into a web browser. The application can embed websites to play or stream media files within the browser window.
A virtual whiteboard applet is an online space where users can draw, write, and exchange ideas. They are generally used for collaborative work during business meetings, education, or brainstorming.
The Game of Life is a cellular automaton designed by mathematician John Conway. Used as an applet, it shows how simple rules (cellular states) can create complicated patterns in the future. Using this applet, one can experiment with various initial configurations and see what becomes of the game.
Applets are interactive computer programs that visualise 3D in research or education. Through them, users can show and manipulate 3D models. Applets may be used to visualise data, molecules, topography, or other scientific or educational phenomena more intuitively and interactively.
This applet shows live weather data, including temperature, humidity, and wind speed, and enables users to see the data through interactive maps or graphs. It can be used in websites for weather prediction or educational purposes.
Java 3D is an API for developing three-dimensional graphics for web applications. Applets developed using this API enable users to visualize and interact with 3D worlds, which can be utilized in areas such as virtual reality, engineering, and entertainment.
Applets that enable users to view and interact with real-time stock market data. Users can monitor price changes, graph market trends, and make financial decisions based on real-time data.
These applets illustrate photographic methods, including lighting, exposure settings, and composition. They are interactive programs that enable students to view the effect of varying various photographic settings in real time.
In conclusion, an applet in java is a crucial element of web interactive development, allowing developers to offer rich multimedia material and interactivity to the web. Though their eventual demise was caused by security, performance, and compatibility issues, applets helped to drive web programming and Java popularity.
Applets are now almost a thing of the past, having given way to web technologies like HTML5, JavaScript, and CSS3. However, it is crucial in web development to consider how much the web has evolved in terms of functionality, security, and user-friendliness.
Understanding Java applets highlights the foundations of web interactivity and event-driven programming. Studying applet lifecycle, graphics, and event handling builds a strong base for modern GUI and web development. It also provides insight into legacy systems, helping learners appreciate security, compatibility, and evolution in web technologies.
A Java applet is a small interactive program that is meant to operate in a web browser. It is written in Java and executed by the Java Runtime Environment (JRE). Generally, it is embedded in HTML for user interaction.
The four major functions in an applet are:
There are two applet types:
Applets were created to deliver interactive visual content in web browsers. This content could involve animations, games, info graphics, and similar things. They allowed developers to write programs that could run on any platform inside web pages thereby enhancing users' experiences.
GUI (Graphical User Interface) in Java means designing interactive programs using graphical components such as buttons, text fields, and labels. Java provides libraries such as Swing and AWT for designing desktop programs with graphical objects for user interaction.
Source: NxtWave - https://www.ccbp.in/blog/articles/applet-in-java
Contact Information: