Main Tutorials

Java AWT – Drawing rectangle, line and circle

The java.awt libraries are set of classes provided by Java in order to draw shapes on a window. The abbreviation AWT stands for Abstract Windowing Toolkit. Today, the library has been converted into a huge set of classes which allows the user to create an entire GUI based application. The visual appearance of these classes depends on the platform on which the application runs.

The most basic of all the classes are the java.awt.geom library which is used to draw simple geometric shapes. This article discusses the code to draw simple shapes like Rectangle, Line, Circle, etc.

JFrame & JPanel
JFrame and JPanel are classes belonging to javax.swing libraries. Swing library is created on top of the AWT library stack. JFrame is a window like container which holds the various components together in a window. JPanel is a container which fits inside a JFrame and helps in grouping of components.

Here in the code below we have used JFrame and JPanel for holding our shape objects.

AWT Drawing Example

Let us now jump onto the main subject of drawing shapes using the AWT libraries. This page shows the various classes available to draw shapes. The name of classes clearly explains which the possible shapes that can be draw are.

DrawShapes.java

package com.mkyong;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawShapes extends JFrame {

	private static final long serialVersionUID = 1L;

	public DrawShapes() {

		setSize(new Dimension(320, 320));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

		JPanel p = new JPanel() {
			@Override
			public void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D) g;
				Shape line = new Line2D.Double(3, 3, 303, 303);
				Shape rect = new Rectangle(3, 3, 303, 303);
				Shape circle = new Ellipse2D.Double(100, 100, 100, 100);
				Shape roundRect = new RoundRectangle2D.Double(20, 20, 250, 250, 5, 25);
				g2.draw(line);
				g2.draw(rect);
				g2.draw(circle);
				g2.draw(roundRect);
			}
		};
		setTitle("My Shapes");
		this.getContentPane().add(p);
	}

	public static void main(String arg[]) {

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new DrawShapes();
			}
		});
	}

}

Output:

awt-shapes

The above code draws a line, rectangle, circle and rounded rectangle. The arguments usually passed to the constructors include coordinates, width and height in case of linear objects. In case of rounded shapes like Ellipse or Arc, the arguments passed include co-ordinates of the origin and width and height of the shape. By executing the code, the output is obtained in the form of a window as shown below.

You can obtain further details about the different types of constructors and shapes available in the AWT libraries using below references.

References

  1. Available Shape Classes
  2. More details on drawing shapes
  3. Shape Class java docs

About Author

author image
A technically sound person, oriented towards learning things logically rather than technically. It is my logical approach that has helped me learn and take up any programming language and start with coding. With a responsibility of Java based Web development professionally, I highly believe in imparting knowledge online for any technical subject that I can handle.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
anurag
6 years ago

great post, nice explanation.
thanks you