JAVA_-_UNIT_4
JAVA_-_UNIT_4
JAVA - UNIT 4
1. AWT (Abstract Window Toolkit)
What is AWT?
AWT is a part of Java's java.awt package and is used to create Graphical User
Interfaces (GUIs) for Java applications.
It provides a set of classes for creating windows, buttons, text fields, and
other UI components.
AWT Classes
AWT components are divided into container classes and non-container classes.
1. Container Classes:
Containers are used to hold and organize other components (like buttons,
labels).
Examples:
2. Non-Container Classes:
Components that cannot contain other elements but provide specific
functionalities.
Examples:
JAVA - UNIT 4 1
Checkbox : A box that can be selected or deselected.
AWT Frame
The Frame class is the top-level window in AWT used to display other components.
Example:
import java.awt.*;
// Setting properties
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());
frame.setBackground(Color.LIGHT_GRAY);
// Adding components
Button button = new Button("Click Me");
frame.add(button);
AWT Panel
The Panel class is a simple container that groups components inside a
container like a Frame .
JAVA - UNIT 4 2
Panels are used to organize the layout within a frame.
Example:
import java.awt.*;
frame.setSize(300, 200);
frame.setVisible(true);
}
}
1. FlowLayout:
Example:
frame.setLayout(new FlowLayout());
2. BorderLayout:
Divides the container into 5 regions: NORTH , SOUTH , EAST , WEST , CENTER .
Example:
frame.setLayout(new BorderLayout());
3. GridLayout:
Example:
JAVA - UNIT 4 3
frame.setLayout(new GridLayout(2, 2)); // 2 rows, 2 co
lumns
4. CardLayout:
Example:
5. GridBagLayout:
In AWT, events are generated by user interactions or system actions and are
processed using event listeners.
Key Concepts
1. Event:
2. Event Source:
3. Event Listener:
4. Event Object:
JAVA - UNIT 4 4
Steps for Event Handling
1. Registering the Event Source:
Example: button.addActionListener(listener);
import java.awt.*;
import java.awt.event.*;
public ButtonEventExample() {
// Create a button and set its properties
button = new Button("Click Me");
JAVA - UNIT 4 5
button.setBounds(50, 100, 80, 30);
setSize(300, 200);
setLayout(null);
setVisible(true);
}
Explanation:
1. ActionListener Interface:
import java.awt.*;
import java.awt.event.*;
JAVA - UNIT 4 6
// Register WindowListener for the frame
addWindowListener(this);
setVisible(true);
}
Explanation:
1. WindowListener Interface:
3. Graphics Class
JAVA - UNIT 4 7
Draws a line from (x1, y1) to (x2,
drawLine(x1, y1, x2, y2)
y2) .
import java.awt.*;
import javax.swing.*;
// Draw a rectangle
g.drawRect(50, 100, 150, 50);
// Draw an oval
g.drawOval(250, 100, 100, 50);
Color Class
JAVA - UNIT 4 8
Setting Colors:
The Color class is used to define the color of shapes and text in the Graphics
class.
Color.GREEN
Color.BLUE
Color.BLACK
Color.YELLOW
Font Class
Creating a Font:
JAVA - UNIT 4 9
g.setColor(Color.MAGENTA); // Set text color
g.drawString("Hello, Graphics!", 100, 100);
}
Drawing Polygons
g.setColor(Color.CYAN);
g.drawPolygon(xPoints, yPoints, nPoints); // Draw outlin
ed polygon
g.fillPolygon(xPoints, yPoints, nPoints); // Draw filled
polygon
}
Drawing Arcs
4. Menus
AWT offers classes like MenuBar , Menu , and MenuItem to build menus.
JAVA - UNIT 4 10
2. Menu:
3. MenuItem:
4. CheckboxMenuItem:
5. PopupMenu:
import java.awt.*;
import java.awt.event.*;
// Create menus
Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
JAVA - UNIT 4 11
MenuItem pasteItem = new MenuItem("Paste");
Explanation:
1. MenuBar: A MenuBar object ( mb ) is created and added to the frame using
setMenuBar() .
2. Menus: Two menus, "File" and "Edit," are created using the Menu class.
3. Menu Items: Items like "New," "Open," and "Exit" are created using the
MenuItem class and added to the menus.
4. Action Listener: The "Exit" menu item has an ActionListener to close the
program when clicked.
Adding Submenus
import java.awt.*;
JAVA - UNIT 4 12
Menu fileMenu = new Menu("File");
setTitle("Submenu Example");
setSize(400, 300);
setVisible(true);
}
Explanation:
1. A Menu object named "Save" is created and added to the "File" menu as a
submenu.
2. The submenu contains items like "Save As Text" and "Save As PDF."
File
├── New
├── Open
├── Save
JAVA - UNIT 4 13
├── Save As Text
└── Save As PDF
Popup Menus
import java.awt.*;
import java.awt.event.*;
popup.add(cut);
popup.add(copy);
popup.add(paste);
JAVA - UNIT 4 14
Explanation:
1. PopupMenu:
A PopupMenu object is created and populated with menu items like "Cut,"
"Copy," and "Paste."
2. MouseListener:
Summary:
MenuBar: Holds multiple menus.
5. Images in AWT
Images can be loaded from files, URLs, or other sources, and displayed using
components like Canvas or overridden paint() methods in Frame .
Types of Images
1. Raster Images:
2. Vector Images:
JAVA - UNIT 4 15
Use the Graphics.drawImage() method inside the paint() method to display
the image.
import java.awt.*;
public ImageExample() {
// Load the image
img = Toolkit.getDefaultToolkit().getImage("example.j
pg");
Toolkit.getDefaultToolkit().getImage(String filename) :
Toolkit.getDefaultToolkit().getImage(URL url) :
2. Drawing an Image:
3. Scaling an Image:
JAVA - UNIT 4 16
Scales the image to the specified dimensions.
Example:
import java.awt.*;
public ScaledImageExample() {
// Load and scale the image
img = Toolkit.getDefaultToolkit().getImage("example.j
pg");
img = img.getScaledInstance(300, 200, Image.SCALE_SMO
OTH);
Using ImageObserver
The ImageObserver interface is used to monitor the loading and rendering of
images.
The Frame class already implements ImageObserver , so we can pass this as the
observer while drawing the image.
Summary:
JAVA - UNIT 4 17
AWT makes it easy to load and display raster images using the Toolkit and
Graphics classes.
Images can be resized and drawn at specific positions using drawImage() and
getScaledInstance() .
Advanced features like ImageObserver provide control over image loading and
rendering.
Types of Streams
1. Byte Streams:
Used to handle binary data, such as reading or writing image or video files.
Classes:
2. Character Streams:
Classes:
Byte Streams
import java.io.*;
JAVA - UNIT 4 18
public class ByteStreamExample {
public static void main(String[] args) {
try (FileInputStream in = new FileInputStream("input.
txt");
FileOutputStream out = new FileOutputStream("out
put.txt")) {
int data;
while ((data = in.read()) != -1) {
out.write(data); // Writes data to output.txt
}
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Character Streams
import java.io.*;
JAVA - UNIT 4 19
}
}
Buffered Streams
import java.io.*;
Character Streams: Handle textual data; use Reader and Writer classes.
Buffered Streams: Enhance performance for both byte and character streams.
JAVA - UNIT 4 20