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
- Draw a Line using Java
- Draw an Ellipse using Java
- Drawing an Arc using Java
- Draw a Rectangle using Java
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.
- First, create an object of BmpOptions class and set bits per pixel using setBitsPerPixel() method.
- Then, assign StreamSource using setSource() method.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.clear() method.
- Draw line using Graphics.drawLine(Pen, int, int, int, int) method.
- Generate and save image using Image.save() method.
The following code sample shows how to draw a line in Java.
The following is the output of the above code sample.
Draw an Ellipse using Java
The following are the steps to draw an ellipse in Java.
- First, create an object of BmpOptions class and set bits per pixel using setBitsPerPixel() method.
- Then, assign StreamSource using setSource() method.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.clear() method.
- Draw an ellipse using Graphics.drawEllipse(Pen, Rectangle) method.
- Generate and save image using Image.save() method.
The following code sample shows how to draw an ellipse on an image in Java.
The following is the output of the above code sample.
Draw an Arc using Java
The following are the steps to draw an arc in Java.
- First, create an object of BmpOptions class and set bits per pixel using setBitsPerPixel() method.
- Then, assign StreamSource using setSource() method.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.clear() method.
- Draw arc using Graphics.drawArc(Pen, float x, float y, float width, float height, float startAngle, float sweepAngle) method.
- Generate and save image using Image.save() method.
The following code sample shows how to draw an arc on an image in Java.
The following is the output of the above code sample.
Draw an Rectangle using Java
The following are the steps to draw a rectangle in Java.
- First, create an object of BmpOptions class and set bits per pixel using setBitsPerPixel() method.
- Then, assign StreamSource using setSource() method.
- Create a new image and initialize it with BmpOptions object and the image’s height and width.
- Create an object of Graphics class and initialize it with Image object.
- Clear the surface of the image with some color using Graphics.clear() method.
- Draw rectangle using Graphics.drawRectangle(Pen, Rectangle) method.
- Generate and save image using Image.save() method.
The following code sample shows how to draw a rectangle on an image in Java.
The following is the output of the above code sample.
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.