0% found this document useful (0 votes)
9 views11 pages

Java Applets, AWT, Events, Threads, Collections

The document covers key Java concepts including Applets, AWT, Event Handling, Multithreading, and the Collection Framework. It explains the life cycle of applets, the components of AWT, the event handling model, thread creation methods, and core interfaces of the Collection Framework. Additionally, it provides example programs for each concept to illustrate their usage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Java Applets, AWT, Events, Threads, Collections

The document covers key Java concepts including Applets, AWT, Event Handling, Multithreading, and the Collection Framework. It explains the life cycle of applets, the components of AWT, the event handling model, thread creation methods, and core interfaces of the Collection Framework. Additionally, it provides example programs for each concept to illustrate their usage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lecture Notes: Applets, AWT, Event

Handling, Multithreading, and Collection


Framework

1. Applets in Java
1.1 What is an Applet?

 An applet is a small Java program that runs inside a web browser or applet viewer.
 It is a subclass of [Link] (or [Link] for Swing-based
GUI).
 Applets were popular in the early days of Java for embedding interactive programs in
web pages.
 They are now deprecated, but still important academically to understand GUI +
AWT.

1.2 Life Cycle of an Applet

Applets have four main methods (called automatically by browser/JVM):

1. init() – Initialization (called once, like a constructor).


2. start() – Called every time the applet is started or revisited.
3. stop() – Called when the user leaves the page.
4. destroy() – Called before applet is removed from memory.

Additionally:

 paint(Graphics g) – Used to draw graphics or text.

1.3 Example: Simple Applet


import [Link];
import [Link];

/* <applet code="[Link]" width=300 height=300></applet> */


public class MyApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello, Applet World!", 50, 50);
}
}

Run using appletviewer:

appletviewer [Link]
2. AWT (Abstract Window Toolkit)
2.1 What is AWT?

 AWT provides classes for creating GUI components: buttons, labels, text fields,
checkboxes, etc.
 Belongs to [Link] package.
 Works with native system resources (heavyweight components).

2.2 Common AWT Components

 Label → Displays text.


 Button → Creates clickable button.
 TextField → Single-line input box.
 TextArea → Multi-line input box.
 Checkbox / CheckboxGroup → For options.
 Choice → Drop-down list.
 List → Multi-selection list.
 Frame → Top-level window.

2.3 Example: AWT Components


import [Link].*;
import [Link].*;

public class AWTExample extends Applet {


public void init() {
Label l = new Label("Enter Name:");
TextField tf = new TextField(20);
Button b = new Button("Submit");

add(l);
add(tf);
add(b);
}
}
/* <applet code="[Link]" width=400 height=200></applet> */

3. Event Handling in Java


3.1 Event Handling Model

 Java uses Delegation Event Model:


1. Event Source → Component generating event (Button, TextField, etc.).
2. Event Object → Encapsulates event details (e.g., ActionEvent,
MouseEvent).
3. Event Listener → Interface defining methods to handle events (e.g.,
ActionListener, MouseListener).

3.2 Common Listener Interfaces


 ActionListener → Handles button clicks, menu selections.
 MouseListener → Handles mouse clicks, movement.
 KeyListener → Handles keyboard events.
 WindowListener → Handles window closing, opening.

3.3 Example: Button Event Handling


import [Link].*;
import [Link].*;
import [Link].*;

public class EventExample extends Applet implements ActionListener {


TextField tf;
Button b;

public void init() {


tf = new TextField(20);
b = new Button("Click Me");
add(tf);
add(b);

[Link](this); // Register listener


}

public void actionPerformed(ActionEvent e) {


[Link]("Button Clicked!");
}
}
/* <applet code="[Link]" width=300 height=200></applet> */

4. Multithreaded Programming in Java


4.1 What is a Thread?

 A thread is a lightweight subprocess, smallest unit of CPU execution.


 Multithreading allows multiple tasks to run concurrently.

4.2 Creating Threads

Two ways:

1. Extending Thread class


2. Implementing Runnable interface

4.3 Example 1: Extending Thread


class MyThread extends Thread {
public void run() {
for(int i=1; i<=5; i++) {
[Link]("Thread running: " + i);
try { [Link](1000); } catch(Exception e) {}
}
}
}

public class ThreadExample {


public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]();
}
}

4.4 Example 2: Implementing Runnable


class MyRunnable implements Runnable {
public void run() {
for(int i=1; i<=5; i++) {
[Link]("Runnable Thread: " + i);
try { [Link](1000); } catch(Exception e) {}
}
}

public class RunnableExample {


public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
[Link]();
}
}

5. Collection Framework
5.1 What is Collection Framework?

 Introduced in Java 2.
 Provides data structures + algorithms to store, retrieve, and manipulate data.
 Part of [Link] package.
 Supports dynamic arrays, linked lists, sets, maps, queues.

5.2 Core Interfaces

 Collection → Base interface.


 List → Ordered, allows duplicates (ArrayList, LinkedList, Vector).
 Set → No duplicates (HashSet, TreeSet, LinkedHashSet).
 Map → Key-value pairs (HashMap, TreeMap, Hashtable).
 Queue → FIFO (PriorityQueue, ArrayDeque).

5.3 Example: List


import [Link].*;

public class ListExample {


public static void main(String[] args) {
List<String> list = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Mango");

for(String fruit : list) {


[Link](fruit);
}
}
}

5.4 Example: Map


import [Link].*;

public class MapExample {


public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
[Link](1, "One");
[Link](2, "Two");
[Link](3, "Three");

for([Link]<Integer, String> entry : [Link]()) {


[Link]([Link]() + " : " + [Link]());
}
}
}

✅ Summary
 Applet → Deprecated small programs, run inside browsers.
 AWT → GUI toolkit with components (Button, TextField, etc.).
 Event Handling → Delegation model with Event Sources, Event Objects, and
Listeners.
 Multithreading → Achieved via Thread and Runnable, used for concurrent
execution.
 Collection Framework → Provides ready-made data structures (List, Set, Map,
Queue).

🔹 Extra Programs

1. Applet Programs
Program 1: Display Shapes in an Applet
import [Link];
import [Link].*;

/* <applet code="[Link]" width=400 height=400></applet> */


public class ShapeApplet extends Applet {
public void paint(Graphics g) {
[Link](50, 50, 100, 50); // Rectangle
[Link](200, 50, 100, 100); // Circle
[Link](50, 150, 200, 200); // Line
[Link]([Link]);
[Link](100, 250, 80, 80); // Filled circle
}
}

Program 2: Simple Calculator Applet


import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code="[Link]" width=400 height=200></applet> */


public class CalcApplet extends Applet implements ActionListener {
TextField t1, t2, result;
Button add;

public void init() {


t1 = new TextField(5);
t2 = new TextField(5);
result = new TextField(10);
add = new Button("Add");

add(t1);
add(t2);
add(add);
add(result);

[Link](this);
}

public void actionPerformed(ActionEvent e) {


int a = [Link]([Link]());
int b = [Link]([Link]());
[Link]([Link](a + b));
}
}

2. AWT Programs
Program 1: Simple AWT Form
import [Link].*;
import [Link].*;

/* <applet code="[Link]" width=300 height=200></applet> */


public class FormApplet extends Applet {
public void init() {
Label name = new Label("Name:");
TextField tf = new TextField(20);
Label pass = new Label("Password:");
TextField pf = new TextField(20);
[Link]('*');
Button submit = new Button("Submit");

add(name); add(tf);
add(pass); add(pf);
add(submit);
}
}

Program 2: Checkbox and Choice Example


import [Link].*;
import [Link].*;

/* <applet code="[Link]" width=300 height=200></applet> */


public class CheckChoiceApplet extends Applet {
public void init() {
Checkbox c1 = new Checkbox("Java");
Checkbox c2 = new Checkbox("Python");
Checkbox c3 = new Checkbox("C++");

Choice ch = new Choice();


[Link]("Red");
[Link]("Green");
[Link]("Blue");

add(c1);
add(c2);
add(c3);
add(ch);
}
}

3. Event Handling Programs


Program 1: Mouse Events
import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code="[Link]" width=400 height=300></applet> */


public class MouseEventApplet extends Applet implements MouseListener {
String msg = "";

public void init() {


addMouseListener(this);
}

public void mouseClicked(MouseEvent e) {


msg = "Mouse Clicked at (" + [Link]() + "," + [Link]() + ")";
repaint();
}

public void mousePressed(MouseEvent e) { msg = "Mouse Pressed";


repaint(); }
public void mouseReleased(MouseEvent e) { msg = "Mouse Released";
repaint(); }
public void mouseEntered(MouseEvent e) { msg = "Mouse Entered";
repaint(); }
public void mouseExited(MouseEvent e) { msg = "Mouse Exited";
repaint(); }

public void paint(Graphics g) {


[Link](msg, 20, 20);
}
}

Program 2: Key Event Handling


import [Link].*;
import [Link].*;
import [Link].*;

/* <applet code="[Link]" width=400 height=200></applet> */


public class KeyEventApplet extends Applet implements KeyListener {
String msg = "";

public void init() {


addKeyListener(this);
setFocusable(true);
}

public void keyPressed(KeyEvent e) { msg = "Key Pressed: " +


[Link](); repaint(); }
public void keyReleased(KeyEvent e) { msg = "Key Released: " +
[Link](); repaint(); }
public void keyTyped(KeyEvent e) { msg = "Key Typed: " +
[Link](); repaint(); }

public void paint(Graphics g) {


[Link](msg, 50, 100);
}
}

4. Multithreading Programs
Program 1: Two Threads Running Together
class ThreadA extends Thread {
public void run() {
for(int i=1; i<=5; i++) {
[Link]("Thread A: " + i);
try { [Link](500); } catch(Exception e) {}
}
}
}

class ThreadB extends Thread {


public void run() {
for(int i=1; i<=5; i++) {
[Link]("Thread B: " + i);
try { [Link](700); } catch(Exception e) {}
}
}
}

public class TwoThreadExample {


public static void main(String[] args) {
new ThreadA().start();
new ThreadB().start();
}
}

Program 2: Thread Synchronization (Bank Account Example)


class Bank {
int balance = 1000;

synchronized void withdraw(int amount) {


if(balance >= amount) {
[Link]([Link]().getName() + " is
withdrawing " + amount);
balance -= amount;
[Link]("Balance after withdrawal: " + balance);
} else {
[Link]("Insufficient Balance for " +
[Link]().getName());
}
}
}

class Customer extends Thread {


Bank b;
int amount;

Customer(Bank b, int amount) {


this.b = b;
[Link] = amount;
}

public void run() {


[Link](amount);
}
}

public class BankExample {


public static void main(String[] args) {
Bank bank = new Bank();
Customer c1 = new Customer(bank, 700);
Customer c2 = new Customer(bank, 500);
[Link]("Customer1");
[Link]("Customer2");
[Link]();
[Link]();
}
}

5. Collection Framework Programs


Program 1: HashSet Example
import [Link].*;

public class HashSetExample {


public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Mango");
[Link]("Apple"); // Duplicate ignored

for(String s : set) {
[Link](s);
}
}
}

Program 2: PriorityQueue Example


import [Link].*;

public class PriorityQueueExample {


public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
[Link](30);
[Link](10);
[Link](20);

while(![Link]()) {
[Link]([Link]()); // Prints in ascending order
}
}
}

Program 3: TreeMap Example


import [Link].*;

public class TreeMapExample {


public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<>();
[Link](3, "Three");
[Link](1, "One");
[Link](2, "Two");

for([Link]<Integer, String> entry : [Link]()) {


[Link]([Link]() + " => " + [Link]());
}
}
}

✅ Final Recap
 You now have multiple programs for each concept:
o Applet → Shapes, Calculator
o AWT → Form, Checkbox/Choice
o Event Handling → Mouse, Keyboard
o Multithreading → Multiple Threads, Synchronization
o Collections → HashSet, PriorityQueue, TreeMap

You might also like