Graphics Programming in Java

Today, we are going to learn the basics of graphics programming in Java. We’ll delve into the fundamentals of drawing basic shapes, manipulating colors, and working with pens. Whether you’re creating graphics as a part of your current project or as the basis of your own game or app, let’s explore how to create beautiful visuals with ease programmatically. 

This article covers the following topics:

  1. Java Graphics Library
  2. Graphics Programming in Java
  3. Draw Curves
  4. Polygons and Rectangles
  5. How to Draw Lines
  6. Draw Paths
  7. Draw a Fill Region

Java Graphics Library - Free Download

Aspose.Drawing is one of the most widely used vector graphics libraries for 2D programming. We will use Aspose.Drawing for Java API for graphics programming in Java. Programmers can leverage this cross-platform library to draw text, shapes, and images in their 2D applications. It allows loading, saving, and manipulating the supported file formats.

Please download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.com/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-drawing</artifactId>
        <version>24.1</version>
    </dependency>
</dependencies>

Graphics Programming in Java

In Java, we can easily draw various types of shapes such as curves, lines, rectangles, filled regions, ellipses, and polygons. The API provides various methods to draw these shapes. We can create a variety of different vector graphics programmatically by following the steps below:

  1. Create an object of the Bitmap class.
  2. Initialize an object of the Graphics class from the bitmap.
  3. Define a Pen or SolidBrush class object with the desired parameters.
  4. Use the shape-specific method(s) of the Graphics class to draw the desired shape(s).
  5. In the end, save the image.

Now, let’s see how to perform these steps in Java.

Draw Curves in Java

We can draw arcs, circles, ellipses, or closed curves by using the following methods:

  • drawArc() method to draw an arc.
  • drawEllipse() method to draw an ellipse.
  • drawClosedCurve() method to draw a closed curve.
  • drawCurve() method to draw a curve.

Please follow the steps given below to draw the curved shapes:

  1. Create an instance of the Bitmap class.
  2. Create the Graphics class object using the fromImage() method from the newly created Bitmap object.
  3. Define a Pen class object with the specified color and size.
  4. After that, call the drawClosedCurve() method to draw a closed curve.
  5. Optionally, repeat the above steps to add an arc and ellipse.
  6. Finally, save the output image using the save() method.

The following code sample shows how to draw curved shapes in Java.

Curves in Graphics Programming using Java

Curves in Graphics Programming using Java

Draw Polygons and Rectangles in Java

We can draw polygons and rectangles by following the steps mentioned earlier. However, we need to use the following methods in step 4:

  • drawPolygon() method for drawing a polygon.
  • drawRectangle() method to draw a rectangle.

The following code sample shows how to draw polygons and rectangles in Java.

Load-an-Image-in-Bitmap-using-java

Load an Image in Bitmap using Java

Draw Lines in Java

Similarly, we can draw lines by following the steps mentioned earlier. However, we need to use the drawLine() method in step # 4 to draw a line.

The following code sample shows how to draw lines in Java.

Lines in Graphics Programming using Java

Lines in Graphics Programming using Java

Draw Paths in Java

Paths in vector graphics represent a series of smooth straight lines defined by a start and end point, along with other points. We can draw a path by following the steps below:

  1. Create an instance of the Bitmap class.
  2. Initialize the Graphics class object from the newly created Bitmap object using the fromImage() method.
  3. Define a Pen class object with the specified color and size.
  4. Create an instance of the GraphicsPath class.
  5. Add lines using the addLine() method.
  6. After that, call the drawPath() method with Pen and GraphicsPath objects.
  7. Finally, save the output image using the save() method.

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

Paths in Graphics Programming using Java

Paths in Graphics Programming using Java

Draw a Fill Region in Java

We can draw a fill region by following the steps below:

  1. Create an instance of the Bitmap class.
  2. Initialize the Graphics class object from the newly created Bitmap object using the fromImage() method.
  3. Create an instance of the GraphicsPath class.
  4. Add a polygon using the addPolygon() method.
  5. Create an instance of the Region class.
  6. Add another GraphicsPath object and add a rectangle using the addRectangle() method.
  7. Call the exclude() method to exclude the inner path from the region.
  8. Then, define a SolidBrush class object with the specified color.
  9. After that, call the fillRegion() method with the SolidBrush and GraphicsPath objects.
  10. Finally, save the output image using the save() method.

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

Fill Region in Graphics Programming using Java

Fill Region in Graphics Programming using Java

Get a Free License

You can get a free temporary license to try Aspose.Drawing for Java without evaluation limitations.

Graphics Programming in Java – Free Resources

Besides drawing lines, paths, and rectangles, you can explore various other features of the library using the resources below:

Conclusion

In this article, we have learned the basics of graphics programming in Java using Aspose.Drawing for Java. We have seen how to draw rectangles, ellipses, lines, and paths programmatically in Java. We have also learned how to create and save a bitmap image as a PNG image. By following the steps outlined in this article and the provided code examples, you can create stunning vector graphics in your Java applications. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also