Java Applet Library
Java applets are small programs written in Java that can run inside a web browser or an
applet viewer. They were primarily used to provide interactive features on web pages, though
modern browsers have mostly phased out applets in favor of JavaScript and other technologies.
1. Applet Basics
Applet: A Java program that extends [Link] class.
Key Feature: Runs in a browser, not as a standalone application.
Life Cycle: init() → start() → paint() → stop() → destroy().
2. Applet Classes & Packages
2.1 [Link] Package
Contains classes and interfaces for creating applets:
Class /
Description
Interface
Applet
Base class for all applets. Provides methods for initialization, starting,
stopping, and destruction.
AudioClip Used to play audio clips in an applet.
AppletContext
Represents the environment in which the applet runs (e.g., for communication
between applets).
AppletStub
Interface that defines the methods that the browser or applet viewer uses to
communicate with the applet.
2.2 [Link] Package
Used for GUI components in applets (buttons, labels, text fields).
Classes include: Button, Label, TextField, Graphics, Color, Font.
3. Life Cycle of an Applet
Method Purpose
Method Purpose
init() Initializes the applet. Called once when the applet is loaded.
start()
Starts or resumes execution. Called after init() and whenever the applet
becomes active.
paint(Graphics Used to draw graphics on the applet. Called whenever the applet needs to
g) redraw.
stop() Pauses the applet when the user navigates away from the page.
destroy() Called when the applet is unloaded. Used for cleanup.
Note: Graphics g object in paint() is used to draw shapes, text, or images on the applet.
4. Example of a Simple Applet
import [Link];
import [Link];
public class HelloApplet extends Applet {
public void init() {
[Link]("Applet initialized");
}
public void paint(Graphics g) {
[Link]("Hello, Applet!", 50, 50);
}
}
init() → called when applet is loaded.
paint(Graphics g) → draws the string on the applet screen.
5. Advantages of Applets
1. Interactive web applications.
2. Runs on multiple platforms (write once, run anywhere).
3. Can handle graphics, multimedia, and simple games.
6. Disadvantages
1. Security restrictions limit access to the local system.
2. Requires Java plugin in the browser (mostly obsolete now).
3. Slow startup compared to modern web technologies.
Using Images in Applet
Use [Link] and getImage() to load images.
Draw using [Link]().
Example:
import [Link];
import [Link];
import [Link];
public class ImageApplet extends Applet {
Image img;
public void init() {
img = getImage(getDocumentBase(), "[Link]"); // Load image from
HTML page location
}
public void paint(Graphics g) {
[Link](img, 20, 20, this); // Draw image at (20,20)
}
}
Key Points:
getImage(URL, filename) → loads image from given path.
Image can be drawn repeatedly using paint().
Using Sound in Applet
Use [Link] class.
Load sound using getAudioClip() and play using play(), loop(), or stop().
Example:
import [Link];
import [Link];
import [Link];
public class SoundApplet extends Applet {
AudioClip clip;
public void init() {
try {
URL url = new URL(getDocumentBase(), "[Link]");
clip = getAudioClip(url);
} catch (Exception e) {
[Link]();
}
}
public void start() {
[Link](); // Play sound once
}
public void stop() {
[Link](); // Stop sound when applet is inactive
}
}
Java AWT Library
AWT (Abstract Window Toolkit) is a Java library for building GUI (Graphical User
Interface) applications. It provides classes and components to create windows, buttons, text
fields, labels, and handle user interactions.
1. Key Features of AWT
1. GUI Components: Buttons, Labels, TextFields, Checkboxes, etc.
2. Event Handling: Responds to user actions (clicks, typing, mouse movements).
3. Platform Independence: Works across operating systems.
4. Lightweight & Heavyweight Components: Some components depend on OS
(heavyweight).
5. Graphics Support: Draw shapes, lines, images on components using Graphics class.
2. AWT Package
Main Package: [Link]
Other Related Packages: [Link] (for event handling), [Link] (for images)
3. Important Classes in AWT
Class Description
Frame Top-level window with a title bar.
Button A clickable button.
Label Displays text on the GUI.
TextField Single-line input field for text.
TextArea Multi-line input field.
Checkbox Single checkbox option.
CheckboxGroup Group of mutually exclusive checkboxes (radio buttons).
Choice Drop-down list for selecting options.
List List of items; user can select one or more.
Panel Container to group components.
MenuBar, Menu, MenuItem Create menus and submenus.
Canvas Custom drawing surface for graphics.
Graphics Provides methods to draw shapes, lines, text, images.
Color Represents color for components and graphics.
Font Specifies font type, style, and size for text.
4. Event Handling in AWT
AWT uses event-driven programming:
1. Event Source: Component generating an event (e.g., Button).
2. Event Listener: Interface that handles the event (e.g., ActionListener).
3. Event Object: Contains event details (e.g., ActionEvent).
Common Event Listeners:
Event Listener Interface Method
Button click ActionListener actionPerformed(ActionEvent e)
Mouse click MouseListener mouseClicked(MouseEvent e)
Key press KeyListener keyPressed(KeyEvent e)
Window close WindowListener windowClosing(WindowEvent e)
Example: Button Click Event
import [Link].*;
import [Link].*;
public class ButtonExample extends Frame implements ActionListener {
Button b;
public ButtonExample() {
b = new Button("Click Me");
[Link](50, 100, 80, 30);
add(b);
[Link](this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
public static void main(String[] args) {
new ButtonExample();
}
}
5. Graphics in AWT
Use Graphics class to draw shapes, text, and images.
Common methods:
Method Description
drawLine(x1, y1, x2, y2) Draw a line
drawRect(x, y, width, height) Draw a rectangle
fillRect(x, y, width, height) Draw a filled rectangle
drawOval(x, y, width, height) Draw an oval
fillOval(x, y, width, height) Draw a filled oval
drawString(String, x, y) Draw text
setColor(Color c) Set drawing color
setFont(Font f) Set font for text
drawImage(Image img, x, y, observer) Draw image
Example: Drawing Shapes
import [Link].*;
public class GraphicsExample extends Frame {
public void paint(Graphics g) {
[Link]([Link]);
[Link](50, 50, 100, 50);
[Link]([Link]);
[Link]("Hello AWT", 60, 80);
}
public static void main(String[] args) {
GraphicsExample f = new GraphicsExample();
[Link](400, 400);
[Link](true);
}
}
6. Advantages of AWT
Provides cross-platform GUI support.
Supports event-driven programming.
Can create interactive applications with buttons, menus, and graphics.
Integrates with applet programming for web-based GUIs.
7. Disadvantages of AWT
Limited customization of GUI components.
Heavyweight components depend on native OS (less consistent look across platforms).
Lacks modern GUI features (Java Swing is preferred now).
Java AWT Components & Examples
1. Frame
Description: Top-level window with a title bar. Can contain other components.
Example:
import [Link].*;
public class FrameExample {
public static void main(String[] args) {
Frame f = new Frame("My Frame");
[Link](400, 300);
[Link](true);
}
}
2. Button
Description: Clickable button.
Example:
import [Link].*;
import [Link].*;
public class ButtonExample extends Frame implements ActionListener {
Button b;
ButtonExample() {
b = new Button("Click Me");
[Link](50, 100, 80, 30);
add(b);
[Link](this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked!");
}
public static void main(String[] args) {
new ButtonExample();
}
}
3. Label
Description: Displays text or information.
Example:
import [Link].*;
public class LabelExample {
public static void main(String[] args) {
Frame f = new Frame("Label Example");
Label l = new Label("Hello, AWT");
[Link](50, 50, 100, 30);
[Link](l);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
4. TextField
Description: Single-line input field.
Example:
import [Link].*;
public class TextFieldExample {
public static void main(String[] args) {
Frame f = new Frame("TextField Example");
TextField t = new TextField();
[Link](50, 50, 150, 20);
[Link](t);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
5. TextArea
Description: Multi-line input field.
Example:
import [Link].*;
public class TextAreaExample {
public static void main(String[] args) {
Frame f = new Frame("TextArea Example");
TextArea ta = new TextArea("Type here...");
[Link](50, 50, 200, 100);
[Link](ta);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
6. Checkbox
Description: Toggle option; can be checked or unchecked.
Example:
import [Link].*;
public class CheckboxExample {
public static void main(String[] args) {
Frame f = new Frame("Checkbox Example");
Checkbox cb1 = new Checkbox("Option 1");
[Link](50, 50, 100, 30);
[Link](cb1);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
7. CheckboxGroup (Radio Buttons)
Description: Group of mutually exclusive checkboxes.
Example:
import [Link].*;
public class RadioExample {
public static void main(String[] args) {
Frame f = new Frame("Radio Button Example");
CheckboxGroup group = new CheckboxGroup();
Checkbox r1 = new Checkbox("Male", group, false);
Checkbox r2 = new Checkbox("Female", group, false);
[Link](50, 50, 100, 30);
[Link](50, 80, 100, 30);
[Link](r1); [Link](r2);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
8. Choice (Dropdown List)
Description: Drop-down list for selecting one option.
Example:
import [Link].*;
public class ChoiceExample {
public static void main(String[] args) {
Frame f = new Frame("Choice Example");
Choice c = new Choice();
[Link]("Red"); [Link]("Green"); [Link]("Blue");
[Link](50, 50, 100, 50);
[Link](c);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
9. List
Description: Shows multiple items; allows single or multiple selection.
Example:
import [Link].*;
public class ListExample {
public static void main(String[] args) {
Frame f = new Frame("List Example");
List l = new List(4, true); // 4 visible rows, multiple select
[Link]("Apple"); [Link]("Banana"); [Link]("Cherry"); [Link]("Mango");
[Link](50, 50, 100, 80);
[Link](l);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
10. Panel
Description: Container to group multiple components.
Example:
import [Link].*;
public class PanelExample {
public static void main(String[] args) {
Frame f = new Frame("Panel Example");
Panel p = new Panel();
[Link](50, 50, 200, 100);
[Link]([Link]);
Button b1 = new Button("OK");
Button b2 = new Button("Cancel");
[Link](b1); [Link](b2);
[Link](p);
[Link](300, 300);
[Link](null);
[Link](true);
}
}
11. MenuBar, Menu, MenuItem
Description: Create menus and submenus.
Example:
import [Link].*;
import [Link].*;
public class MenuExample {
public static void main(String[] args) {
Frame f = new Frame("Menu Example");
MenuBar mb = new MenuBar();
Menu file = new Menu("File");
MenuItem open = new MenuItem("Open");
MenuItem exit = new MenuItem("Exit");
[Link](open); [Link](exit);
[Link](file);
[Link](mb);
[Link](300, 300);
[Link](true);
}
}
12. Canvas
Description: Custom drawing surface.
Example:
import [Link].*;
public class CanvasExample {
public static void main(String[] args) {
Frame f = new Frame("Canvas Example");
Canvas c = new Canvas() {
public void paint(Graphics g) {
[Link]([Link]);
[Link](20, 20, 100, 50);
}
};
[Link](50, 50, 200, 100);
[Link](c);
[Link](300, 300);
[Link](null);
[Link](true);
}
}