Java Applets - Lecture Notes with Examples
1. Applet Basics
- An Applet is a Java program that runs inside a web browser or applet viewer. - Applets extend the
[Link] class and use the Abstract Window Toolkit (AWT) for GUI components. - Unlike
standalone programs, applets do not have a main() method; instead, they rely on lifecycle methods.
2. Applet Architecture
Applet Lifecycle is controlled by the browser or applet viewer. The key methods are: - init(): Called
once for initialization. - start(): Called after init() or when the applet becomes active again. - stop():
Called when the user leaves the page or applet becomes inactive. - destroy(): Called when the
applet is being removed from memory. - paint(Graphics g): Called to display output on the applet
window.
3. Applet Skeleton with Example
import [Link];
import [Link];
/* <applet code="[Link]" width=300 height=200></applet> */
public class MyApplet extends Applet {
public void init() {
// Initialization code
}
public void start() {
// Executed when applet starts
}
public void stop() {
// Executed when applet stops
}
public void destroy() {
// Executed when applet is destroyed
}
public void paint(Graphics g) {
[Link]("Hello Applet!", 50, 100);
}
}
Explanation: This applet displays the text 'Hello Applet!' at position (50,100).
4. Applet Display Method Example
import [Link];
import [Link];
/* <applet code="[Link]" width=300 height=200></applet> */
public class DisplayApplet extends Applet {
public void paint(Graphics g) {
[Link]("Displaying text on the applet window", 20, 50);
[Link](20, 70, 200, 70);
[Link](20, 90, 100, 50);
[Link](150, 90, 100, 50);
}
}
Explanation: This applet demonstrates drawing text, a line, a rectangle, and an oval.
5. Requesting Repainting
import [Link];
import [Link];
/* <applet code="[Link]" width=300 height=200></applet> */
public class RepaintApplet extends Applet {
int counter = 0;
public void paint(Graphics g) {
counter++;
[Link]("Repaint count: " + counter, 20, 50);
}
public void start() {
for (int i = 0; i < 5; i++) {
repaint(); // Requests redrawing of applet
}
}
}
Explanation: Each time repaint() is called, paint() executes and counter increases.
6. HTML Tag Example
<applet code="[Link]" width="300" height="200">
</applet>
7. Passing Parameters to Applet
import [Link];
import [Link];
/* <applet code="[Link]" width=300 height=200>
<param name="user" value="John">
</applet> */
public class ParamApplet extends Applet {
String user;
public void init() {
user = getParameter("user");
if (user == null) user = "Guest";
}
public void paint(Graphics g) {
[Link]("Hello, " + user, 20, 50);
}
}
Explanation: The applet reads parameter 'user' from HTML and displays greeting.
8. Layout Managers
import [Link];
import [Link];
import [Link];
/* <applet code="[Link]" width=300 height=200></applet> */
public class LayoutApplet extends Applet {
public void init() {
setLayout(new FlowLayout());
add(new Button("Button 1"));
add(new Button("Button 2"));
add(new Button("Button 3"));
}
}
Explanation: This applet uses FlowLayout to arrange buttons from left to right.