Graphics and drawing in Java AWT

Java AWT (Abstract Window Toolkit) provides several classes and methods for performing graphics and drawing operations. In this blog post, we will explore how to use Java AWT to create graphics, draw shapes, and perform basic drawing operations.

Table of Contents

Getting Started with Java AWT

To start with Java AWT graphics, you need to create a Frame or Panel to display the graphics on the screen. Here’s a code snippet that demonstrates creating a basic Frame and setting its size:

import java.awt.*;

public class GraphicsDemo extends Frame {
   public static void main(String[] args) {
      GraphicsDemo graphicsDemo = new GraphicsDemo();
      graphicsDemo.setSize(500, 500);
      graphicsDemo.setVisible(true);
   }
}

Drawing Shapes

Java AWT provides various methods for drawing shapes such as lines, rectangles, ellipses, and arcs. Let’s see an example of drawing a red rectangle on the GraphicsDemo frame created earlier:

public void paint(Graphics g) {
   g.setColor(Color.RED);
   g.drawRect(50, 50, 200, 100);
}

In this example, the paint() method is overridden to perform custom painting. The setColor() method sets the color to be used for drawing, and drawRect() is used to draw a rectangle with the specified position and size.

Similarly, you can use methods like drawLine(), drawOval(), and drawArc() to draw other shapes.

Performing Basic Drawing Operations

Java AWT also provides additional methods for performing basic drawing operations such as filling shapes, drawing images, and adding text. Here’s an example of drawing a filled circle with an image and text inside it:

public void paint(Graphics g) {
   g.setColor(Color.GREEN);
   g.fillOval(50, 50, 100, 100);
   
   Image img = Toolkit.getDefaultToolkit().getImage("image.png");
   g.drawImage(img, 50, 50, this);
   
   g.setColor(Color.BLACK);
   g.drawString("Hello, Java!", 70, 110);
}

In this example, the fillOval() method is used to draw a filled circle, and the drawImage() method is used to draw an image inside the circle. Lastly, the drawString() method is used to add text to the circle.

Conclusion

Java AWT provides a powerful set of tools for performing graphics and drawing operations in Java applications. You can create various shapes, apply colors, draw images, and add text to enhance your graphical user interfaces.

In this blog post, we covered the basics of graphics and drawing in Java AWT. Feel free to explore the Java AWT documentation for more advanced topics and examples.

References