Introduction to Java Graphics Programming
Java provides a powerful Abstract Window Toolkit
(AWT) and Swing libraries for developing Graphical User Interfaces (GUIs). The java.awt.Graphics class is fundamental for drawing 2D shapes,
handling colors, fonts, and rendering images.
Working with Frame
and Components
✳️ Frame:
A top-level window with a title and a border.
✳️ Component:
An object having a graphical representation that can
be displayed on the screen and can interact with the user.
🔹 Example:
Creating a Frame and Drawing a Line
Program:
import java.awt.*;
public class SimpleFrame extends Frame {
public SimpleFrame() {
setTitle("Basic Frame
Example");
setSize(400, 300);
setVisible(true);
}
public void paint(Graphics g) {
g.drawLine(50, 50, 250, 250);
}
public static void main(String[]
args) {
new SimpleFrame();
}
}
Output:
📝 Note: The paint(Graphics g) method is
overridden to draw on the frame.
Working with
2D Shapes
Java offers the Graphics2D class for enhanced 2D rendering using shapes like rectangle, ellipse, arc,
etc.
🔹 Example: Drawing
Basic Shapes
import java.awt.*;
import java.awt.geom.*;
public class ShapesDemo extends Frame {
public ShapesDemo() {
setTitle("2D Shapes
Example");
setSize(400, 300);
setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)
g;
g2.draw(new Rectangle2D.Double(50,
50, 100, 60));
g2.draw(new Ellipse2D.Double(200,
50, 100, 60));
g2.draw(new Arc2D.Double(50, 150,
100, 60, 45, 90, Arc2D.OPEN));
}
public static void main(String[]
args) {
new ShapesDemo();
}
}
Output:
Using Color in
Graphics
The setColor(Color c) method is used to set the current drawing color.
🔹 Example: Filling
Shapes with Color
Program:
import java.awt.*;
import java.awt.geom.*;
public class ColorExample extends Frame {
public ColorExample() {
setTitle("Using
Colors");
setSize(400, 300);
setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)
g;
g2.setColor(Color.RED);
g2.fill(new Rectangle2D.Double(50,
50, 100, 60));
g2.setColor(new Color(0, 128,
255)); // Custom color
g2.fill(new Ellipse2D.Double(200,
50, 100, 60));
}
public static void main(String[]
args) {
new ColorExample();
}
}
Output:
5. Using Fonts in
Graphics
Java allows font customization using Font class and drawing text using drawString() method.
Program:
import java.awt.*;
public class FontExample extends Frame {
public FontExample() {
setTitle("Font
Example");
setSize(400, 300);
setVisible(true);
}
public void paint(Graphics g) {
g.setFont(new Font("Serif",
Font.BOLD, 24));
g.setColor(Color.BLUE);
g.drawString("Graphics
with Font", 100, 100);
}
public static void main(String[]
args) {
new FontExample();
}
}
Output:
6. Working with Images
To display an image, we use the drawImage() method of the Graphics class. Images can be loaded using Toolkit.
🔹 Example:
Displaying an Image
Program:
import java.awt.*;
public class ImageExample extends Frame {
Image img;
public ImageExample() {
setTitle("Image
Display");
setSize(400, 300);
setVisible(true);
img =
Toolkit.getDefaultToolkit().getImage("your_image.jpg"); // Replace
with image path
}
public void paint(Graphics g) {
g.drawImage(img, 50, 50, this);
}
public static void main(String[]
args) {
new ImageExample();
}
}
Output:
🧾 Summary Notes
Topic |
Description |
Frame |
A top-level window in AWT; used for GUI apps. |
Component |
Elements like Button, Label, etc. |
Graphics |
Core class for drawing basic shapes. |
Graphics2D |
Advanced class to draw geometric primitives. |
Color |
Used to set drawing color; predefined and custom. |
Font |
Customizes text style and size in graphics. |
Image |
Used to load and render images onto the GUI. |
No comments:
Post a Comment