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:
- Java Graphics Library
- Graphics Programming in Java
- Draw Curves
- Polygons and Rectangles
- How to Draw Lines
- Draw Paths
- 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:
- Create an object of the Bitmap class.
- Initialize an object of the Graphics class from the bitmap.
- Define a Pen or SolidBrush class object with the desired parameters.
- Use the shape-specific method(s) of the Graphics class to draw the desired shape(s).
- 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:
- Create an instance of the Bitmap class.
- Create the Graphics class object using the fromImage() method from the newly created Bitmap object.
- Define a Pen class object with the specified color and size.
- After that, call the drawClosedCurve() method to draw a closed curve.
- Optionally, repeat the above steps to add an arc and ellipse.
- Finally, save the output image using the save() method.
The following code sample shows how to draw curved shapes in 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.
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.
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:
- Create an instance of the Bitmap class.
- Initialize the Graphics class object from the newly created Bitmap object using the fromImage() method.
- Define a Pen class object with the specified color and size.
- Create an instance of the GraphicsPath class.
- Add lines using the addLine() method.
- After that, call the drawPath() method with Pen and GraphicsPath objects.
- Finally, save the output image using the save() method.
The following code sample shows how to draw a path in Java.
Draw a Fill Region in Java
We can draw a fill region by following the steps below:
- Create an instance of the Bitmap class.
- Initialize the Graphics class object from the newly created Bitmap object using the fromImage() method.
- Create an instance of the GraphicsPath class.
- Add a polygon using the addPolygon() method.
- Create an instance of the Region class.
- Add another GraphicsPath object and add a rectangle using the addRectangle() method.
- Call the exclude() method to exclude the inner path from the region.
- Then, define a SolidBrush class object with the specified color.
- After that, call the fillRegion() method with the SolidBrush and GraphicsPath objects.
- Finally, save the output image using the save() method.
The following code sample shows how to draw a filled region in 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.