Draw Shapes in Java

In certain cases, you may need to create different objects by drawing the shapes such as circles, lines, rectangles, etc. Also, you may have to draw these shapes on images for annotation. In this article, you will learn how to draw shapes programmatically in Java. Particularly, you will learn how to draw lines, ellipses, arcs, and rectangles and generate their images.

Java API to Draw Shapes - Free Download

To draw the shapes and generate output images, we will use Aspose.Imaging for Java. It is a powerful image editing API that provides a wide range of features to manipulate images and create drawings. You can either download the API or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-imaging</artifactId>
    <version>22.9</version>
</dependency>

Draw a Line using Java

The following are the steps to draw a line in Java.

The following code sample shows how to draw a line in Java.

// Create BmpOptions
com.aspose.imaging.imageoptions.BmpOptions bmpCreateOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpCreateOptions.setBitsPerPixel(32);
// Define the source property for the instance of BmpOptions
bmpCreateOptions.setSource(new StreamSource());
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpCreateOptions, 500, 500);
// Create and initialize an instance of Graphics class
com.aspose.imaging.Graphics graphic = new com.aspose.imaging.Graphics(image);
// Clear the image surface with White color
graphic.clear(com.aspose.imaging.Color.getWhite());
// Draw a dotted line by specifying the Pen object having blue color and
// co-ordinate Points
graphic.drawLine(new Pen(com.aspose.imaging.Color.getBlue(), 3), 18, 18, 200, 200);
graphic.drawLine(new Pen(com.aspose.imaging.Color.getBlue(), 3), 18, 200, 200, 18);
// Draw a continuous line by specifying the Pen object having Solid
// Brush with red color and two point structures
graphic.drawLine(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed()), 3),
new com.aspose.imaging.Point(18, 18), new com.aspose.imaging.Point(18, 200));
// Draw a continuous line by specifying the Pen object having Solid
// Brush with white color and two point structures
graphic.drawLine(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getOrange()), 3),
new com.aspose.imaging.Point(200, 18), new com.aspose.imaging.Point(18, 18));
// Save all changes
image.save("draw_lines.bmp");
view raw draw-line.java hosted with ❤ by GitHub

The following is the output of the above code sample.

draw line in Java

Draw an Ellipse using Java

The following are the steps to draw an ellipse in Java.

The following code sample shows how to draw an ellipse on an image in Java.

// Create BmpOptions
com.aspose.imaging.imageoptions.BmpOptions bmpCreateOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpCreateOptions.setBitsPerPixel(32);
// Define the source property for the instance of BmpOptions
bmpCreateOptions.setSource(new StreamSource());
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpCreateOptions, 400, 400);
// Create and initialize an instance of Graphics class
com.aspose.imaging.Graphics graphic = new com.aspose.imaging.Graphics(image);
// Clear the image surface with White color
graphic.clear(com.aspose.imaging.Color.getWhite());
// Draw a dotted ellipse shape by specifying the Pen object having red
// color and a surrounding Rectangle
graphic.drawEllipse(new Pen(com.aspose.imaging.Color.getRed(), 3),
new com.aspose.imaging.Rectangle(60, 40, 70, 120));
// Draw a continuous ellipse shape by specifying the Pen object having
// solid brush with blue color and a surrounding Rectangle
graphic.drawEllipse(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getBlue()), 3),
new com.aspose.imaging.Rectangle(40, 60, 120, 70));
// Save all changes
image.save("draw_ellipse.bmp");

The following is the output of the above code sample.

draw ellipse in Java

Draw an Arc using Java

The following are the steps to draw an arc in Java.

The following code sample shows how to draw an arc on an image in Java.

// Create BmpOptions
com.aspose.imaging.imageoptions.BmpOptions bmpCreateOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpCreateOptions.setBitsPerPixel(32);
// Define the source property for the instance of BmpOptions
bmpCreateOptions.setSource(new StreamSource());
// Creates an instance of Image and call Create method by passing the
// BmpOptions object
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpCreateOptions, 400, 400);
// Create and initialize an instance of Graphics class
com.aspose.imaging.Graphics graphic = new com.aspose.imaging.Graphics(image);
// Clear the image surface with White color
graphic.clear(com.aspose.imaging.Color.getWhite());
// Draw a dotted arc shape by specifying the Pen object having red black
// color and coordinates, height, width, start & end angles
int width = 200;
int height = 300;
int startAngle = 45;
int sweepAngle = 270;
// Draw arc to screen
graphic.drawArc(new Pen(com.aspose.imaging.Color.getBlack(), 3), 0, 0, width, height, startAngle, sweepAngle);
// Save all changes
image.save("draw_arc.bmp");
view raw draw-arc.java hosted with ❤ by GitHub

The following is the output of the above code sample.

draw arc in Java

Draw an Rectangle using Java

The following are the steps to draw a rectangle in Java.

The following code sample shows how to draw a rectangle on an image in Java.

// Create BmpOptions
com.aspose.imaging.imageoptions.BmpOptions bmpCreateOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpCreateOptions.setBitsPerPixel(32);
// Define the source property for the instance of BmpOptions
bmpCreateOptions.setSource(new StreamSource());
// Creates an instance of Image and call Create method by passing the
// bmpCreateOptionsobject
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpCreateOptions, 400, 400);
// Create and initialize an instance of Graphics class
com.aspose.imaging.Graphics graphic = new com.aspose.imaging.Graphics(image);
// Clear the image surface with White color
graphic.clear(com.aspose.imaging.Color.getWhite());
// Draw a dotted rectangle shape by specifying the Pen object having red
// color and a rectangle structure
graphic.drawRectangle(new Pen(com.aspose.imaging.Color.getRed(), 3),
new com.aspose.imaging.Rectangle(60, 40, 70, 120));
// Draw a continuous rectangle shape by specifying the Pen object having
// solid brush with blue color and a rectangle structure
graphic.drawRectangle(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getBlue()), 3),
new com.aspose.imaging.Rectangle(40, 60, 120, 70));
// Save all changes
image.save("draw_rectangle.bmp");

The following is the output of the above code sample.

draw rectangle in Java

Java Image Drawing API - Get a Free License

You can get a free temporary license and draw shapes without evaluation limitations.

Conclusion

In this article, you have learned how to draw shapes in Java. We have covered how to draw lines, ellipses, arcs, and rectangles on images programmatically. You can easily integrate the provided code samples into your Java applications.

Read More

You can explore more about the Java image processing API using documentation. Also, you can share your queries with us via our forum.

See Also