Class PathBuilder
This class provides a typed API for building SVG path definitions
that can be used with both PathElement and AnimateMotionElement.
Example usage with PathElement:
PathElement path = new PathElement()
.d(p -> p
.moveTo(50, 50)
.lineTo(100, 50)
.quadraticBezierTo(150, 50, 150, 100)
.lineTo(150, 150)
.closePath())
.fill(HexColor.of("#3366cc"));
Example usage with AnimateMotionElement:
rect.animateMotion()
.path(p -> p
.moveTo(50, 80)
.horizontalLineTo(250)
.quadraticBezierTo(280, 80, 280, 60)
.closePath())
.dur(Duration.ofSeconds(3))
.rotateAuto()
.repeatIndefinitely();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionarcTo(double rx, double ry, double xAxisRotation, boolean largeArc, boolean sweep, double x, double y) Draw an elliptical arc (absolute coordinates).arcToRelative(double rx, double ry, double xAxisRotation, boolean largeArc, boolean sweep, double dx, double dy) Draw an elliptical arc (relative coordinates).build()Builds and returns the path data string.static PathBuildercircle(double cx, double cy, double r) Creates a circular path.Close the current path by drawing a straight line back to the start.cubicBezierTo(double x1, double y1, double x2, double y2, double x, double y) Draw a cubic Bézier curve (absolute coordinates).cubicBezierToRelative(double dx1, double dy1, double dx2, double dy2, double dx, double dy) Draw a cubic Bézier curve (relative coordinates).static PathBuilderellipse(double cx, double cy, double rx, double ry) Creates an elliptical path.horizontalLineTo(double x) Draw a horizontal line to the specified x coordinate (absolute).horizontalLineToRelative(double dx) Draw a horizontal line by the specified offset (relative).lineTo(double x, double y) Draw a line to the specified point (absolute coordinates).lineToRelative(double dx, double dy) Draw a line to the specified point (relative coordinates).moveTo(double x, double y) Move to the specified point (absolute coordinates).moveToRelative(double dx, double dy) Move to the specified point (relative coordinates).quadraticBezierTo(double cx, double cy, double x, double y) Draw a quadratic Bézier curve (absolute coordinates).quadraticBezierToRelative(double dcx, double dcy, double dx, double dy) Draw a quadratic Bézier curve (relative coordinates).static PathBuilderrectangle(double x, double y, double width, double height) Creates a rectangular path.static PathBuilderroundedRectangle(double x, double y, double width, double height, double radius) Creates a rounded rectangular path.smoothCubicBezierTo(double x2, double y2, double x, double y) Draw a smooth cubic Bézier curve (absolute coordinates).smoothCubicBezierToRelative(double dx2, double dy2, double dx, double dy) Draw a smooth cubic Bézier curve (relative coordinates).smoothQuadraticBezierTo(double x, double y) Draw a smooth quadratic Bézier curve (absolute coordinates).smoothQuadraticBezierToRelative(double dx, double dy) Draw a smooth quadratic Bézier curve (relative coordinates).toString()Returns the path data string.verticalLineTo(double y) Draw a vertical line to the specified y coordinate (absolute).verticalLineToRelative(double dy) Draw a vertical line by the specified offset (relative).
-
Method Details
-
moveTo
Move to the specified point (absolute coordinates).Equivalent to the "M" command.
- Parameters:
x- the x coordinatey- the y coordinate- Returns:
- this builder for method chaining
-
moveToRelative
Move to the specified point (relative coordinates).Equivalent to the "m" command.
- Parameters:
dx- the relative x offsetdy- the relative y offset- Returns:
- this builder for method chaining
-
lineTo
Draw a line to the specified point (absolute coordinates).Equivalent to the "L" command.
- Parameters:
x- the x coordinatey- the y coordinate- Returns:
- this builder for method chaining
-
lineToRelative
Draw a line to the specified point (relative coordinates).Equivalent to the "l" command.
- Parameters:
dx- the relative x offsetdy- the relative y offset- Returns:
- this builder for method chaining
-
horizontalLineTo
Draw a horizontal line to the specified x coordinate (absolute).Equivalent to the "H" command.
- Parameters:
x- the x coordinate- Returns:
- this builder for method chaining
-
horizontalLineToRelative
Draw a horizontal line by the specified offset (relative).Equivalent to the "h" command.
- Parameters:
dx- the relative x offset- Returns:
- this builder for method chaining
-
verticalLineTo
Draw a vertical line to the specified y coordinate (absolute).Equivalent to the "V" command.
- Parameters:
y- the y coordinate- Returns:
- this builder for method chaining
-
verticalLineToRelative
Draw a vertical line by the specified offset (relative).Equivalent to the "v" command.
- Parameters:
dy- the relative y offset- Returns:
- this builder for method chaining
-
closePath
Close the current path by drawing a straight line back to the start.Equivalent to the "Z" or "z" command.
- Returns:
- this builder for method chaining
-
cubicBezierTo
Draw a cubic Bézier curve (absolute coordinates).Equivalent to the "C" command.
- Parameters:
x1- x coordinate of the first control pointy1- y coordinate of the first control pointx2- x coordinate of the second control pointy2- y coordinate of the second control pointx- x coordinate of the end pointy- y coordinate of the end point- Returns:
- this builder for method chaining
-
cubicBezierToRelative
public PathBuilder cubicBezierToRelative(double dx1, double dy1, double dx2, double dy2, double dx, double dy) Draw a cubic Bézier curve (relative coordinates).Equivalent to the "c" command.
- Parameters:
dx1- relative x offset of the first control pointdy1- relative y offset of the first control pointdx2- relative x offset of the second control pointdy2- relative y offset of the second control pointdx- relative x offset of the end pointdy- relative y offset of the end point- Returns:
- this builder for method chaining
-
smoothCubicBezierTo
Draw a smooth cubic Bézier curve (absolute coordinates).Equivalent to the "S" command. The first control point is assumed to be the reflection of the second control point of the previous command.
- Parameters:
x2- x coordinate of the second control pointy2- y coordinate of the second control pointx- x coordinate of the end pointy- y coordinate of the end point- Returns:
- this builder for method chaining
-
smoothCubicBezierToRelative
Draw a smooth cubic Bézier curve (relative coordinates).Equivalent to the "s" command.
- Parameters:
dx2- relative x offset of the second control pointdy2- relative y offset of the second control pointdx- relative x offset of the end pointdy- relative y offset of the end point- Returns:
- this builder for method chaining
-
quadraticBezierTo
Draw a quadratic Bézier curve (absolute coordinates).Equivalent to the "Q" command.
- Parameters:
cx- x coordinate of the control pointcy- y coordinate of the control pointx- x coordinate of the end pointy- y coordinate of the end point- Returns:
- this builder for method chaining
-
quadraticBezierToRelative
Draw a quadratic Bézier curve (relative coordinates).Equivalent to the "q" command.
- Parameters:
dcx- relative x offset of the control pointdcy- relative y offset of the control pointdx- relative x offset of the end pointdy- relative y offset of the end point- Returns:
- this builder for method chaining
-
smoothQuadraticBezierTo
Draw a smooth quadratic Bézier curve (absolute coordinates).Equivalent to the "T" command. The control point is assumed to be the reflection of the control point of the previous command.
- Parameters:
x- x coordinate of the end pointy- y coordinate of the end point- Returns:
- this builder for method chaining
-
smoothQuadraticBezierToRelative
Draw a smooth quadratic Bézier curve (relative coordinates).Equivalent to the "t" command.
- Parameters:
dx- relative x offset of the end pointdy- relative y offset of the end point- Returns:
- this builder for method chaining
-
arcTo
public PathBuilder arcTo(double rx, double ry, double xAxisRotation, boolean largeArc, boolean sweep, double x, double y) Draw an elliptical arc (absolute coordinates).Equivalent to the "A" command.
- Parameters:
rx- x radius of the ellipsery- y radius of the ellipsexAxisRotation- rotation of the ellipse in degreeslargeArc- if true, draw the larger arcsweep- if true, draw the arc in positive angle directionx- x coordinate of the end pointy- y coordinate of the end point- Returns:
- this builder for method chaining
-
arcToRelative
public PathBuilder arcToRelative(double rx, double ry, double xAxisRotation, boolean largeArc, boolean sweep, double dx, double dy) Draw an elliptical arc (relative coordinates).Equivalent to the "a" command.
- Parameters:
rx- x radius of the ellipsery- y radius of the ellipsexAxisRotation- rotation of the ellipse in degreeslargeArc- if true, draw the larger arcsweep- if true, draw the arc in positive angle directiondx- relative x offset of the end pointdy- relative y offset of the end point- Returns:
- this builder for method chaining
-
build
-
toString
-
rectangle
Creates a rectangular path.- Parameters:
x- x coordinate of the top-left cornery- y coordinate of the top-left cornerwidth- width of the rectangleheight- height of the rectangle- Returns:
- a new PathBuilder with the rectangle path
-
roundedRectangle
public static PathBuilder roundedRectangle(double x, double y, double width, double height, double radius) Creates a rounded rectangular path.- Parameters:
x- x coordinate of the top-left cornery- y coordinate of the top-left cornerwidth- width of the rectangleheight- height of the rectangleradius- corner radius- Returns:
- a new PathBuilder with the rounded rectangle path
-
circle
Creates a circular path.- Parameters:
cx- x coordinate of the centercy- y coordinate of the centerr- radius- Returns:
- a new PathBuilder with the circle path
-
ellipse
Creates an elliptical path.- Parameters:
cx- x coordinate of the centercy- y coordinate of the centerrx- horizontal radiusry- vertical radius- Returns:
- a new PathBuilder with the ellipse path
-