Java Applet Basics: Understanding Applets in Web Browsers
Learn the fundamentals of Java applets, how they run within web browsers, and their key differences from standalone Java applications. Discover how applets extend the java.applet.Applet
class, follow sandbox security rules, and are embedded in HTML pages. Understand the requirements for running applets, including JVM integration and JAR file support.
Java - Applet Basics
An applet is a Java program that runs within a web browser. It can function as a full-fledged Java application with access to the complete Java API. However, there are some key differences between an applet and a standalone Java application:
- An applet is a Java class that extends
java.applet.Applet
. - An applet does not have a
main()
method. - Applets are embedded in an HTML page.
- The code for an applet is downloaded to the user's machine upon loading the HTML page.
- A Java Virtual Machine (JVM) is needed to run an applet, which may be integrated into the browser or installed separately.
- Applets follow strict security rules enforced by the web browser, known as "sandbox security."
- Classes required by the applet can be included in a JAR file.
Life Cycle of an Applet in Java
The Applet class provides methods that help structure the life cycle of an applet:
- init: Called once for initialization when the applet is first loaded.
- start: Automatically called after
init()
, and each time the user returns to the page containing the applet. - stop: Automatically called when the user navigates away from the applet’s page.
- destroy: Called only when the browser shuts down.
- paint: Invoked whenever the applet needs to repaint itself on the page.
A Simple Java Applet Example: "Hello, World"
Here's a basic example of a Java applet:
Syntax
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 25, 50);
}
}
Output
Hello World
Invoking an Applet in HTML
An applet is embedded in HTML using the <applet>
tag. Below is an example:
Syntax
Hello World Applet
Life Cycle Flow of an Applet
The flow of an applet's life cycle methods looks like this:
Flow of Applet Life Cycle
init() -> start() -> paint() -> stop() -> destroy()
Event Handling in Applets
Applets inherit event-handling methods like processMouseEvent
and processKeyEvent
. Here's an example of handling mouse events:
Syntax
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;
import java.awt.Graphics;
public class ExampleEventHandling extends Applet implements MouseListener {
StringBuffer strBuffer;
public void init() {
addMouseListener(this);
strBuffer = new StringBuffer();
addItem("initializing the applet ");
}
public void start() {
addItem("starting the applet ");
}
public void stop() {
addItem("stopping the applet ");
}
public void destroy() {
addItem("unloading the applet");
}
void addItem(String word) {
strBuffer.append(word);
repaint();
}
public void paint(Graphics g) {
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g.drawString(strBuffer.toString(), 10, 20);
}
public void mouseClicked(MouseEvent event) {
addItem("mouse clicked! ");
}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
}
Output
initializing the applet
starting the applet
mouse clicked!
Displaying Images in Applets
Applets can display images in GIF, JPEG, BMP, and other formats. Here's an example:
Syntax
import java.applet.*;
import java.awt.*;
import java.net.*;
public class ImageDemo extends Applet {
private Image image;
public void init() {
String imageURL = getParameter("image");
try {
URL url = new URL(getDocumentBase(), imageURL);
image = getImage(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}
Playing Audio in Applets
Applets can play audio using the AudioClip
interface. Here's an example:
Syntax
import java.applet.*;
import java.net.*;
public class AudioDemo extends Applet {
private AudioClip clip;
public void init() {
try {
URL url = new URL(getDocumentBase(), "test.wav");
clip = getAudioClip(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void start() {
if (clip != null) {
clip.loop();
}
}
public void stop() {
if (clip != null) {
clip.stop();
}
}
}