Java AWT Graphics Example: Master Drawing Shapes and Images with Code

Robert Jul 13, 2026

Java AWT, or Abstract Window Toolkit, is a graphics and GUI framework provided by Java. It's crucial for creating graphical user interfaces and handling event-driven programming. One of the primary aspects of Java AWT is its robust graphics capabilities, enabling developers to draw and manage graphics efficiently. Let's delve into Java AWT graphics with some practical examples.

What are Arrays in Java?
What are Arrays in Java?

Before we dive into examples, it's essential to understand the basic components of Java AWT graphics. These include `Graphics` and `Graphics2D` classes, which provide methods for drawing shapes, text, and images on the screen. Now, let's explore how to use these classes to create graphics in Java AWT.

Java Decision Making Cheat Sheet – Code Smarter Decisions! β˜•πŸš€
Java Decision Making Cheat Sheet – Code Smarter Decisions! β˜•πŸš€

Java AWT Graphics Using `Graphics` Class

The `Graphics` class is the basic drawing class in Java AWT. It provides methods to draw simple shapes and text on the screen.

Top 5 Applications of Java with Real-World Examples
Top 5 Applications of Java with Real-World Examples

Here's a simple example demonstrating how to draw a rectangle and a string using the `Graphics` class:

Drawing a Rectangle

Java infographic made for my IT finals. Java History Infographic, Java Programming Language Infographic, Java Programming Specs Guide, Java Programming Tutorial, Learn Java Basics, Computer Science Infographic, Go Vs Java Infographic, Java Final Meaning Infographic, Java Programming Design Tips
Java infographic made for my IT finals. Java History Infographic, Java Programming Language Infographic, Java Programming Specs Guide, Java Programming Tutorial, Learn Java Basics, Computer Science Infographic, Go Vs Java Infographic, Java Final Meaning Infographic, Java Programming Design Tips

To draw a rectangle, you can use the `drawRect(int x, int y, int width, int height)` method of the `Graphics` class. Here's a simple example:

```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RectangleExample extends JFrame { public void paint(Graphics g) { g.drawRect(50, 50, 100, 100); } } ```

In this code, a rectangle is drawn at position (50, 50) with width and height both set to 100.

Drawing a String

Java TreeMap Infographic for Beginners (2025) - Quipoin
Java TreeMap Infographic for Beginners (2025) - Quipoin

To draw a string, you can use the `drawString(String text, int x, int y)` method. Here's an example:

```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class StringExample extends JFrame { public void paint(Graphics g) { g.drawString("Hello, AWT!", 50, 50); } } ```

In this code, the string "Hello, AWT!" is drawn at position (50, 50).

Java AWT Graphics Using `Graphics2D` Class

Java Programming, Java
Java Programming, Java

The `Graphics2D` class extends the `Graphics` class, providing advanced functionalities for drawing graphics in Java AWT.

Here's an example demonstrating how to draw a rounded rectangle and a_stratified image using the `Graphics2D` class:

a diagram that shows the different types of web pages and what they are used for
a diagram that shows the different types of web pages and what they are used for
a red and blue logo with the word's name on it
a red and blue logo with the word's name on it
Java Thread | Multithreading Basics
Java Thread | Multithreading Basics
concept of java #java  #javanotes
concept of java #java #javanotes
a diagram showing how to use the same language as an object in a web application
a diagram showing how to use the same language as an object in a web application
a black and white graduation hat holding a red book with a cup of coffee in it
a black and white graduation hat holding a red book with a cup of coffee in it
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Java
Java
Java 3D Graphics Programming
Java 3D Graphics Programming
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
java interview questions
java interview questions
Java HashMap Explained πŸ—ΊοΈ | DSA Pattern Cheat Sheet for Coding Interviews
Java HashMap Explained πŸ—ΊοΈ | DSA Pattern Cheat Sheet for Coding Interviews
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
Set Interface in Java
Set Interface in Java
Java Cheat Sheet: Operators, Scanner & User Input β˜•
Java Cheat Sheet: Operators, Scanner & User Input β˜•
Digging Into Java from Pre-history Till Now and Why We Always Go For It - Graphic Design Junction
Digging Into Java from Pre-history Till Now and Why We Always Go For It - Graphic Design Junction
a diagram showing the different types of food
a diagram showing the different types of food
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
What is abstraction in java?
What is abstraction in java?
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast πŸš€ | ArrayList HashMap Set Queue Explained

Drawing a Rounded Rectangle

To draw a rounded rectangle, you can use the `fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)` method. Here's an example:

```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RoundedRectangleExample extends JFrame { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.fillRoundRect(50, 50, 100, 100, 20, 20); } } ```

In this code, a rounded rectangle is drawn at position (50, 50) with width and height both set to 100, and arc.width and arc.height both set to 20.

Drawing a Stratified Image

To draw an image, you can use the `drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)` method. To draw multiple images on top of each other, create a layered pane and add each image to a separate `JLabel` with the desired opacity. Here's an example:

```java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.imageio.ImageIO; public class StratifiedImageExample extends JFrame { public void paint(Graphics g) { JPanel panel = new JPanel(); JLabel label1 = new JLabel(new ImageIcon("image1.png")); JLabel label2 = new JLabel(new ImageIcon("image2.png")); label2.setOpacity(0.5f); // Set opacity to 50% panel.add(label1); panel.add(label2); getContentPane().add(panel); } } ```

In this code, two images ("image1.png" and "image2.png") are drawn on top of each other. The second image is given a reduced opacity to create a stratified effect.

That's it! You now have a solid understanding of Java AWT graphics and can create engaging and functional GUIs with graphics. Happy coding!