Working with Vector Graphics in Java

Creating vector graphics in Java becomes a straightforward task with Aspose.Drawing library for Java. This API facilitates working with a diverse range of vector graphics, including arcs, Bezier splines, Cardinal splines, closed curves, ellipses, lines, and various other types. The following Java examples demonstrate how to draw different types of vector graphics using this API.

Draw Arc in Java

To draw an arc using Java:

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawArc() method of Graphics class object to draw an arc
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawArc(pen, 0, 0, 700, 700, 0, 180);
bitmap.save("DrawArc.png");
Draw Arc

Draw Bezier Spline in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawBezier() method of Graphics class object to draw Bezier Spline
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
PointF p1 = new PointF(0, 0); // start point
PointF c1 = new PointF(0, 800); // first control point
PointF c2 = new PointF(1000, 0); // second control point
PointF p2 = new PointF(1000, 800); // end point
graphics.drawBezier(pen, p1, c1, c2, p2);
bitmap.save("DrawBezierSpline.png");
Draw Bezier Spline

Draw Cardinal Spline in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawCurve() method of Graphics class object to draw Cardinal Spline
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawCurve(pen, new Point[] { new Point(10, 700), new Point(250, 500), new Point(500, 10), new Point(750, 500), new Point(990, 700) });
bitmap.save("DrawCardinalSpline.png");
Draw Cardinal Spline

Draw Closed Curve in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawClosedCurve() method of Graphics class object to draw a closed curve
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawClosedCurve(pen, new Point[] { new Point(100, 700), new Point(350, 600), new Point(500, 500), new Point(650, 600), new Point(900, 700) });
bitmap.save("DrawClosedCurve.png");
Draw Closed Curve

Draw Ellipse in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap.
  3. Define a Pen object with desired parameters
  4. Use the drawEllipse() method of Graphics class object to draw Ellipse
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawEllipse(pen, 10, 10, 900, 700);
bitmap.save("DrawEllipse.png");
Draw Ellipse

Draw Lines in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawLine() method of Graphics class object to draw lines
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawLine(pen, 10, 700, 500, 10);
graphics.drawLine(pen, 500, 10, 990, 700);
bitmap.save("DrawLines.png");
Draw Lines

Draw Path in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Initialize a new object of GraphicsPath class and add Lines to its path collection
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
import com.aspose.drawing.drawing2d;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
GraphicsPath path = new GraphicsPath();
path.addLine(100, 100, 1000, 400);
path.addLine(1000, 600, 300, 600);
path.addRectangle(new Rectangle(500, 350, 200, 400));
path.addEllipse(10, 250, 450, 300);
graphics.drawPath(pen, path);
bitmap.save("DrawPath.png");
Draw Path

Draw Polygon in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawPolygon() method of Graphics class object to draw Polygon
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawPolygon(pen, new Point[] { new Point(100, 100), new Point(500, 700), new Point(900, 100) });
bitmap.save("DrawPolygon.png");
Draw Polygon

Draw Rectangle in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the drawRectangle() method of Graphics class object to draw Rectangle
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
Pen pen = new Pen(Color.getBlue(), 2);
graphics.drawRectangle(pen, 10, 10, 900, 700);
bitmap.save("DrawRectangle.png");
Draw Rectangle

Fill Region in Java

  1. Create an object of Bitmap class.
  2. Initialize an object of Graphics class from this bitmap
  3. Create a GraphicsPath class class object
  4. Add polygon to the Path collection of GraphicsPath class object
  5. Use the fillRegion() method of Graphics class to fill defined regions
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA
import com.aspose.drawing.*;
import com.aspose.drawing.drawing2d;
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb);
Graphics graphics = Graphics.fromImage(bitmap);
GraphicsPath path = new GraphicsPath();
path.addPolygon(new Point[] { new Point(100, 400), new Point(500, 100), new Point(900, 400), new Point(500, 700) });
Region region = new Region(path);
GraphicsPath innerPath = new GraphicsPath();
innerPath.addRectangle(new Rectangle(300, 300, 400, 200));
region.exclude(innerPath);
Brush brush = new SolidBrush(Color.getBlue());
graphics.fillRegion(brush, region);
bitmap.save("FillRegion.png");
Fill Region