Class PathBuilder

java.lang.Object
org.vaadin.firitin.element.svg.PathBuilder

public class PathBuilder extends Object
A fluent builder for SVG path data strings.

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 Details

    • moveTo

      public PathBuilder moveTo(double x, double y)
      Move to the specified point (absolute coordinates).

      Equivalent to the "M" command.

      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      this builder for method chaining
    • moveToRelative

      public PathBuilder moveToRelative(double dx, double dy)
      Move to the specified point (relative coordinates).

      Equivalent to the "m" command.

      Parameters:
      dx - the relative x offset
      dy - the relative y offset
      Returns:
      this builder for method chaining
    • lineTo

      public PathBuilder lineTo(double x, double y)
      Draw a line to the specified point (absolute coordinates).

      Equivalent to the "L" command.

      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      this builder for method chaining
    • lineToRelative

      public PathBuilder lineToRelative(double dx, double dy)
      Draw a line to the specified point (relative coordinates).

      Equivalent to the "l" command.

      Parameters:
      dx - the relative x offset
      dy - the relative y offset
      Returns:
      this builder for method chaining
    • horizontalLineTo

      public PathBuilder horizontalLineTo(double x)
      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

      public PathBuilder horizontalLineToRelative(double dx)
      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

      public PathBuilder verticalLineTo(double y)
      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

      public PathBuilder verticalLineToRelative(double dy)
      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

      public PathBuilder 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

      public PathBuilder cubicBezierTo(double x1, double y1, double x2, double y2, double x, double y)
      Draw a cubic Bézier curve (absolute coordinates).

      Equivalent to the "C" command.

      Parameters:
      x1 - x coordinate of the first control point
      y1 - y coordinate of the first control point
      x2 - x coordinate of the second control point
      y2 - y coordinate of the second control point
      x - x coordinate of the end point
      y - 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 point
      dy1 - relative y offset of the first control point
      dx2 - relative x offset of the second control point
      dy2 - relative y offset of the second control point
      dx - relative x offset of the end point
      dy - relative y offset of the end point
      Returns:
      this builder for method chaining
    • smoothCubicBezierTo

      public PathBuilder smoothCubicBezierTo(double x2, double y2, double x, double y)
      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 point
      y2 - y coordinate of the second control point
      x - x coordinate of the end point
      y - y coordinate of the end point
      Returns:
      this builder for method chaining
    • smoothCubicBezierToRelative

      public PathBuilder smoothCubicBezierToRelative(double dx2, double dy2, double dx, double dy)
      Draw a smooth cubic Bézier curve (relative coordinates).

      Equivalent to the "s" command.

      Parameters:
      dx2 - relative x offset of the second control point
      dy2 - relative y offset of the second control point
      dx - relative x offset of the end point
      dy - relative y offset of the end point
      Returns:
      this builder for method chaining
    • quadraticBezierTo

      public PathBuilder quadraticBezierTo(double cx, double cy, double x, double y)
      Draw a quadratic Bézier curve (absolute coordinates).

      Equivalent to the "Q" command.

      Parameters:
      cx - x coordinate of the control point
      cy - y coordinate of the control point
      x - x coordinate of the end point
      y - y coordinate of the end point
      Returns:
      this builder for method chaining
    • quadraticBezierToRelative

      public PathBuilder quadraticBezierToRelative(double dcx, double dcy, double dx, double dy)
      Draw a quadratic Bézier curve (relative coordinates).

      Equivalent to the "q" command.

      Parameters:
      dcx - relative x offset of the control point
      dcy - relative y offset of the control point
      dx - relative x offset of the end point
      dy - relative y offset of the end point
      Returns:
      this builder for method chaining
    • smoothQuadraticBezierTo

      public PathBuilder smoothQuadraticBezierTo(double x, double y)
      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 point
      y - y coordinate of the end point
      Returns:
      this builder for method chaining
    • smoothQuadraticBezierToRelative

      public PathBuilder smoothQuadraticBezierToRelative(double dx, double dy)
      Draw a smooth quadratic Bézier curve (relative coordinates).

      Equivalent to the "t" command.

      Parameters:
      dx - relative x offset of the end point
      dy - 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 ellipse
      ry - y radius of the ellipse
      xAxisRotation - rotation of the ellipse in degrees
      largeArc - if true, draw the larger arc
      sweep - if true, draw the arc in positive angle direction
      x - x coordinate of the end point
      y - 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 ellipse
      ry - y radius of the ellipse
      xAxisRotation - rotation of the ellipse in degrees
      largeArc - if true, draw the larger arc
      sweep - if true, draw the arc in positive angle direction
      dx - relative x offset of the end point
      dy - relative y offset of the end point
      Returns:
      this builder for method chaining
    • build

      public String build()
      Builds and returns the path data string.
      Returns:
      the SVG path data string
    • toString

      public String toString()
      Returns the path data string.

      Equivalent to build().

      Overrides:
      toString in class Object
      Returns:
      the SVG path data string
    • rectangle

      public static PathBuilder rectangle(double x, double y, double width, double height)
      Creates a rectangular path.
      Parameters:
      x - x coordinate of the top-left corner
      y - y coordinate of the top-left corner
      width - width of the rectangle
      height - 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 corner
      y - y coordinate of the top-left corner
      width - width of the rectangle
      height - height of the rectangle
      radius - corner radius
      Returns:
      a new PathBuilder with the rounded rectangle path
    • circle

      public static PathBuilder circle(double cx, double cy, double r)
      Creates a circular path.
      Parameters:
      cx - x coordinate of the center
      cy - y coordinate of the center
      r - radius
      Returns:
      a new PathBuilder with the circle path
    • ellipse

      public static PathBuilder ellipse(double cx, double cy, double rx, double ry)
      Creates an elliptical path.
      Parameters:
      cx - x coordinate of the center
      cy - y coordinate of the center
      rx - horizontal radius
      ry - vertical radius
      Returns:
      a new PathBuilder with the ellipse path