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.

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 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.

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

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

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

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:




















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!