
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.
// 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"); |
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.
// 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 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.
// 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"); |
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.
// 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.

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.