Java - Applet Basics
Hello there, aspiring Java developers! I'm thrilled to embark on this exciting journey with you into the world of Java Applets. As someone who's been teaching programming for over a decade, I can assure you that while the road ahead might seem daunting, it's also incredibly rewarding. So, let's roll up our sleeves and dive right in!
What is a Java Applet?
Before we start coding, let's understand what an applet actually is. Think of an applet as a small Java program that can be embedded in a web page. It's like a miniature application that runs within your browser, bringing interactivity and dynamic content to otherwise static HTML pages.
Life Cycle of an Applet in Java
Every applet goes through a series of stages during its lifetime. Understanding this life cycle is crucial for developing effective applets. Let's break it down:
- Initialization
- Starting
- Running
- Stopping
- Destruction
Flow of Java Applet Life Cycle
To better visualize this flow, let's look at a table that outlines the methods called during each stage:
Stage | Method Called |
---|---|
Initialization | init() |
Starting | start() |
Running | paint() |
Stopping | stop() |
Destruction | destroy() |
Now, let's dive deeper into each of these stages with some code examples.
A Simple Java Applet "Hello, World"
Let's start with the classic "Hello, World" example. This will help us understand the basic structure of an applet.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, World!", 50, 25);
}
}
Let's break this down:
- We import necessary classes from
java.applet
andjava.awt
packages. - Our class
HelloWorldApplet
extends theApplet
class. - We override the
paint
method to draw our "Hello, World!" message.
The Applet Class
The Applet
class is the superclass of all applets. It provides a framework for applet execution, including methods for initialization, starting, stopping, and destroying the applet. Let's look at a more comprehensive example:
import java.applet.Applet;
import java.awt.Graphics;
public class LifeCycleApplet extends Applet {
String message = "";
public void init() {
message += "Initializing... ";
}
public void start() {
message += "Starting... ";
}
public void stop() {
message += "Stopping... ";
}
public void destroy() {
message += "Destroying... ";
}
public void paint(Graphics g) {
g.drawString(message, 20, 20);
}
}
This applet demonstrates all the life cycle methods. Each method adds to the message
string, which is then displayed in the paint
method.
Invoking an Applet
To run an applet, we need to embed it in an HTML page. Here's how you would do it:
<html>
<body>
<applet code="LifeCycleApplet.class" width="300" height="100">
Your browser does not support the <code>applet</code> tag.
</applet>
</body>
</html>
Getting Applet Parameters
Applets can receive parameters from the HTML page. This allows for greater flexibility in applet behavior. Let's see how:
import java.applet.Applet;
import java.awt.Graphics;
public class ParameterApplet extends Applet {
public void paint(Graphics g) {
String name = getParameter("name");
g.drawString("Hello, " + name + "!", 50, 25);
}
}
And in the HTML:
<applet code="ParameterApplet.class" width="300" height="100">
<param name="name" value="Alice">
</applet>
Event Handling
Applets can respond to user interactions. Let's create a simple button-click applet:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonApplet extends Applet implements ActionListener {
Button b;
String message = "";
public void init() {
b = new Button("Click me!");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
message = "Button clicked!";
repaint();
}
public void paint(Graphics g) {
g.drawString(message, 50, 50);
}
}
This applet creates a button and responds to clicks by updating the message.
Displaying Images
Applets can also display images. Here's a simple example:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class ImageApplet extends Applet {
Image img;
public void init() {
img = getImage(getDocumentBase(), "myimage.jpg");
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
This applet loads an image from the same directory as the HTML file and displays it.
Playing Audio
Finally, let's look at how applets can play audio:
import java.applet.Applet;
import java.applet.AudioClip;
public class AudioApplet extends Applet {
AudioClip clip;
public void init() {
clip = getAudioClip(getCodeBase(), "mysound.au");
}
public void start() {
clip.play();
}
public void stop() {
clip.stop();
}
}
This applet loads an audio file and plays it when the applet starts, stopping when the applet stops.
And there you have it, folks! We've covered the basics of Java Applets, from their life cycle to handling events, displaying images, and playing audio. Remember, practice makes perfect, so don't be afraid to experiment with these examples and create your own applets. Happy coding!
Credits: Image by storyset