Drawing Shapes in Java

Textual data without any graphics makes a business document boring and less interesting. In fact, graphical illustrations are used to grab the reader’s attention throughout the document and the reader perceives the gist of the data easily. This article is for Java programmers who create and manipulate documents/images programmatically. So, Aspose.PSD for Java is a powerful Java graphics library for drawing shapes in Java such as lines, eclipses, and rectangles. Therefore, in this guide, we will implement how to draw lines using this Java drawing API. In addition, we will also go through how to draw an Eclipse in Java by writing a few lines of source code.

The following points will be covered in this guide:

  1. Java Graphics Library Installation
  2. Draw a Line in Java - Code Sample
  3. Draw an Eclipse Programmatically

Java Graphics Library Installation

This installation guide demonstrates all the necessary details to install this Java drawing API. Moreover, you may download the JAR file here.

Java Graphics Library Installation

Draw a Line in Java - Code Sample

The following steps and the code snippet show how to draw lines in Javs using Aspose.PSD for Java:

  • Create an instance of the BmpOptions class.
  • Invoke the setBitsPerPixel method to set the bits per pixel.
  • Initialize an object of the Image class with an instance of the PsdImage class.
  • Create and initialize an object of the Graphics class and set the image background color.
  • Call the drawLine method to draw two dotted diagonal lines by specifying the Pen object having blue color and coordinate Points.
  • The save method will save the file on disk.

The following code sample shows how to draw lines in Java programmatically:

The output of the above code example is shown in the image below:

draw a line in java

Draw an Eclipse Programmatically

To draw an eclipse in Java, please follow the steps and the code sample given below:

  • Invoke the drawEllipse method to draw an ellipse shape by specifying the Pen object having a red color and a surrounding Rectangle.

You can see the output in the image below:

draw an eclipse

Java Drawing API - Get a Free License

You can get a free temporary license to try this Java graphics API without evaluation limitations.

Summing up

Thanks for reading! Aspose.PSD for Java is not only confined to drawing shapes but it also offers a lot more. So, in this blog post, we covered how to draw lines and eclipses in Java programmatically. However, you can go through the documentation, and the API references to know this Java drawing API further.

If you are feeling the blog post vibe then head over to this Getting Started guide. Finally, aspose.com will publish other useful blog posts, so stay connected.

Ask a question

You can let us know about your questions or queries on our forum.

Frequently Asked Questions – FAQs

How to draw shapes in Java code?

Install Aspose.PSD for Java and automate the process of drawing shapes in Java programmatically.

How to draw a line in Java?

You can draw a line in Java by making a few method calls offered by Aspose.PSD for Java. Moreover, please visit this link to see the code example.

See Also