Unit 5: Event Driven Programming
Event Driven Programming
Event Driven Programming is a programming style where the flow of the program depends on user actions (events).
Simple meaning: The program waits for an event, and when the event happens, it responds.
Examples of Events:
- Clicking a button
- Moving the mouse
- Pressing a key
- Closing a window
Real-life example:
- Pressing a doorbell → Bell rings
- Clicking “Submit” → Form gets submitted
Java supports event-driven programming mainly through AWT and Swing.
Graphics Programming in Java
Graphics programming is used to draw shapes, text, images, and colors on the screen.
Java provides graphics support using:
- AWT (Abstract Window Toolkit)
- Swing
- Graphics and Graphics2D classes
Frame
A Frame is a main window that appears on the screen.
Simple meaning: A frame is like an empty window where we place buttons, labels, drawings, etc.
Example:
Key Points:
Framebelongs to java.awt- It has title bar, borders, and close button
- Acts as a container for components
Components
Components are GUI elements placed inside a frame.
Examples of Components:
- Button
- Label
- TextField
- Checkbox
- Choice
Example:
Common AWT Components:
| Component | Use |
|---|---|
| Button | Click action |
| Label | Display text |
| TextField | Input text |
| Checkbox | Select option |
| Choice | Drop-down list |
Working with 2D Shapes
Java uses the Graphics and Graphics2D classes to draw shapes.
Drawing Shapes
Shapes are drawn inside the paint() method.
Example:
Common Shape Methods:
| Method | Shape |
|---|---|
| drawLine() | Line |
| drawRect() | Rectangle |
| fillRect() | Filled rectangle |
| drawOval() | Oval |
| drawArc() | Arc |
Using Graphics2D (Advanced 2D Graphics)
Additional Features:
- Better quality
- Rotation
- Scaling
- Stroke thickness
Using Colors
Setting Colors
Java provides the Color class.
Example:
Custom Colors:
Common Colors:
- Color.RED
- Color.BLUE
- Color.GREEN
- Color.BLACK
Using Fonts
A font controls:
- Text style
- Text size
- Text appearance
Example:
Font Styles:
- Font.PLAIN
- Font.BOLD
- Font.ITALIC
Using Images
Loading and Displaying Images
Java uses the Image and Toolkit classes.
Example:
Uses:
- Logos
- Icons
- Background images
Event Handling (Basic Overview)
Event Handling Steps:
- Generate event (button click)
- Listener listens to event
- Listener handles event
Example:
Common Event Listeners:
- ActionListener
- MouseListener
- KeyListener
- WindowListener
Complete Simple Example
Conclusion
Event-driven and graphics programming in Java allow developers to:
- Create interactive applications
- Build graphical user interfaces
- Draw shapes, text, and images
- Respond to user actions
Understanding frames, components, 2D shapes, colors, fonts, and images is essential for GUI-based Java applications and exams.
Basics of Event Handling in Java
Event handling is the mechanism that allows a Java program to respond to user actions.
Simple meaning: When a user performs an action, Java detects it as an event and handles it using code.
Examples of Events:
- Clicking a button
- Pressing a key
- Moving the mouse
- Closing a window
Event Handling Mechanism in Java
Java follows the Delegation Event Model.
Components of Event Handling:
- Event Source – Object that generates the event (Button, TextField, Frame)
- Event Object – Contains details of the event (ActionEvent, MouseEvent)
- Event Listener (Handler) – Object that handles the event
Event Handlers (Event Listeners)
An event handler is a method that executes when an event occurs.
Example: ActionListener
Key Points:
- Listener interface must be implemented
- Event-handling method must be overridden
What are Adapter Classes?
Adapter classes provide empty implementations of listener interfaces.
Problem Without Adapter: Some listeners have many methods, even if you need only one.
Solution: Adapter classes allow overriding only required methods.
Example: MouseAdapter
Common Adapter Classes:
| Adapter Class | Listener |
|---|---|
| MouseAdapter | MouseListener |
| KeyAdapter | KeyListener |
| WindowAdapter | WindowListener |
Action Events
An ActionEvent occurs when:
- Button is clicked
- Menu item selected
- Enter key pressed in TextField
Example:
Class Used:
java.awt.event.ActionEvent
Mouse Events
Mouse events occur when the user interacts with the mouse.
Types of Mouse Events:
| Event Method | Description |
|---|---|
| mouseClicked() | Mouse clicked |
| mousePressed() | Button pressed |
| mouseReleased() | Button released |
| mouseEntered() | Cursor enters |
| mouseExited() | Cursor exits |
Example:
AWT Event Hierarchy
Java events follow a hierarchical structure.
Explanation:
EventObject→ Base class for all eventsAWTEvent→ Base class for AWT events- Subclasses represent specific events
Introduction to Swing
Swing is a GUI toolkit built on top of AWT.
Key Features of Swing:
- Platform independent
- Lightweight components
- More advanced and flexible than AWT
Difference Between AWT and Swing
| Feature | AWT | Swing |
|---|---|---|
| Components | Heavyweight | Lightweight |
| Look & Feel | OS dependent | Platform independent |
| Component names | Button | JButton |
Layout Management in Swing
What is Layout Manager?
A layout manager controls how components are arranged inside a container.
Simple meaning: It decides where buttons, labels, and text fields appear.
Common Layout Managers
1. FlowLayout - Components arranged in a row
2. BorderLayout - Divides container into five areas
| Area |
|---|
| NORTH |
| SOUTH |
| EAST |
| WEST |
| CENTER |
3. GridLayout - Components arranged in rows and columns
4. BoxLayout - Components arranged vertically or horizontally
Example: Swing Layout
Conclusion
Event handling and Swing form the foundation of Java GUI programming.
They help to:
- Build interactive applications
- Handle user input efficiently
- Design well-structured interfaces
Understanding event handlers, adapter classes, action and mouse events, AWT event hierarchy, and Swing layout management is essential for Java exams and practical applications.