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.

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));

		JPanel p = new JPanel() {
			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);
		setTitle("My Shapes");

	public static void main(String arg[]) {

		SwingUtilities.invokeLater(new Runnable() {

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




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.


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