JavaHow to Use Geometric Shapes In Java

How to Use Geometric Shapes In Java

Java provides the Graphics2D class for rendering two-dimensional (2D) text, shapes, and images in Java applications. This class is part of the java.awt package. In addition, the Shape interface is used to define objects that represent geometric shapes. These geometries can be found in the java.awt.geom package.

In this programming tutorial, developers will learn how to use the Graphics2D class and the Shape interface to render geometric 2D graphics in Java.

Before we begin, however: are you looking to learn how to develop software in Java in an online course? If so, we have a list of the Best Online Courses to Learn Java to help get you started.

How to Create a Shape in Java

The first step in creating a 2D geometric shape in Java is to provide a Graphics parameter to the paint() function. Inside the paint(Graphics g) function, you can then call the class for the particular 2D object.

The following sections describe how programmers can draw common geometric shapes using Java.

How to Draw a Line in Java

You can create a simple line by using the drawLine(int x1, int y1, int x2, int y2) method. This draws a line from the cartesian coordinates (x1,y1) to (x2,y2).

For example:

g.drawLine(42, 55, 130, 25)

Would draw a line with the starting coordinates of x42 and y55 and ending at coordinates x130 and y25 on a grid.

How to Draw a Rectangle in Java

To draw a rectangle in a Java application, you need to use the drawRect(int x, int y, int width, int height) method. This method draws a rectangle of a given width value and height value from coordinates (x,y), relative to your container object:

g.drawRect(100, 100, 40, 120); // example of rectangle

The above method creates a normal rectangle (i.e rectangle with pointed edges). However, in some instances a programmer might want to create a rectangle with rounded edges. In that case, you need to use the drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) method.

Here are a few examples of how to use this method:

g.drawRoundRect(100, 100, 40, 120, 35, 35);
g.drawRoundRect(100, 100, 40, 120, 0, 0);

Take particular note of the second example. When you provide archWidth and arcHeight of (0,0), you get a normal rectangle.

You can also use the setColor() method to set your rectangle’s border color. If you instead wanted to fill the rectangle with a given color, then you need to use the fillRect(int x, int y, int width, int height) method, in addition to setColor(). For a rounded rectangle, you can use fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight).

Here is some example code showing how to use drawRect() to create a shape in Java:

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class GeometricShape extends JPanel{
   public static void main(String args[]){
       JFrame frame = new JFrame();
       frame.getContentPane().add(new GeometricShape());
   public void paint(Graphics g) {
       g.drawRect(100, 100, 40, 120);
       g.fillRect(120, 120, 40, 60); 

If you run this in your code editor or integrated development environment (IDE), you would get the following output:

Java Shapes tutroial

Read: The Best Project Management Tools for Developers

How to Draw a Circle or Curve in Java

The drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) method in Java allows developers to draw a curve or a circle. This method lets programmers draw a curve (or circle) bounded by a rectangle of a given width and height.

Here is an example of an arc of 60 degrees:

drawArc(45, 75, 90, 150, 0, 60)

If you are to draw a circle, then the arcAngle needs to be 360 degrees and the width value should be equal to height. The startAngle can be any angle. To avoid confusion, you can just use a startAngle of 0. For example:

drawArc(45, 75, 150, 150, 0, 360)

How to Draw an Oval in Java

The drawOval(int x, int y, int width, int height) method allows you to draw an oval, circle, or ellipse that is bound in a rectangle of a given width and height.

You can play around with various integer values to see what shape forms you get with drawOval(). It is worth mentioning that, in case you wish to create a circle, the width value should be equal to height.

Final Thoughts on Drawing Shapes in Java

Java provides methods in the Graphics class for creating different geometries. This class provides a simple way to create 2D objects. However, if you want more control over how you create these shapes, then you can consider using the Graphics2D class, a subclass of Graphics</b.

Read more Java programming tutorials and software development tips.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories