Package codedraw

Class Path

java.lang.Object
codedraw.Path

public class Path extends Object
Represents a Path that is used to create custom shapes. Use Image.drawPathStartingAt(double, double) and Image.fillPathStartingAt(double, double) to start creating a path. Here is an example on how to draw a section of a dart board.

  double angle = Math.PI / 4; // 45°
  cd.fillPathStartingAt(200, 300)
      .arcTo(300, 300, angle)
      .lineTo(300 - Math.cos(angle) * 200, 300 - Math.sin(angle) * 200)
      .arcTo(300, 300, -angle)
      .complete();
 
  • Method Summary

    Modifier and Type
    Method
    Description
    arcTo(double centerX, double centerY, double sweepRadians)
    Draws an arc with the center being the (centerX, centerY) coordinates.
    bezierTo(double control1X, double control1Y, double control2X, double control2Y, double endX, double endY)
    Draws a cubic Bézier curve.
    void
    Completes the shapes by connecting the start point with the last position of this path.
    curveTo(double controlX, double controlY, double endX, double endY)
    Draws a quadratic Bézier curve.
    double
    Gets the current position of the path.
    double
    Gets the current position of the path.
    lineTo(double endX, double endY)
    Draws a straight line from the current position to the end position.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • getCurrentX

      public double getCurrentX()
      Gets the current position of the path. This position changes with each section added to the path.
      Returns:
      the x position.
    • getCurrentY

      public double getCurrentY()
      Gets the current position of the path. This position changes with each section added to the path.
      Returns:
      the y position.
    • lineTo

      public Path lineTo(double endX, double endY)
      Draws a straight line from the current position to the end position. The underlying image class can be used to change the Image.setLineWidth(double).
      Parameters:
      endX - The distance in pixel from the left side of the canvas to the end of the line.
      endY - The distance in pixel from the top side of the canvas to the end of the line.
      Returns:
      An instance of this path which can be used to complete the shape.
    • curveTo

      public Path curveTo(double controlX, double controlY, double endX, double endY)
      Draws a quadratic Bézier curve. See: Wikipedia Bezier Curve. The start and end of the curve will be precisely where startX/Y and endX/Y are specified. The controlX/Y parameter specifies in what way the curve will be bent. The underlying image class can be used to change the Image.setLineWidth(double).
      Parameters:
      controlX - Defines the way the curve bends in the x direction.
      controlY - Defines the way the curve bends in the y direction.
      endX - The distance in pixel from the left side of the canvas to the end of the curve.
      endY - The distance in pixel from the top side of the canvas to the end of the curve.
      Returns:
      An instance of this path which can be used to complete the shape.
    • bezierTo

      public Path bezierTo(double control1X, double control1Y, double control2X, double control2Y, double endX, double endY)
      Draws a cubic Bézier curve. See Wikipedia Bezier Curve The start and end of the curve will be precisely where startX/Y and endX/Y are specified. The control1X/Y and control2X/Y parameter specify in what way the curve will be bent. The underlying image class can be used to change the Image.setLineWidth(double).
      Parameters:
      control1X - Defines the way the curve bends in the x direction.
      control1Y - Defines the way the curve bends in the y direction.
      control2X - Defines the way the curve bends in the x direction.
      control2Y - Defines the way the curve bends in the y direction.
      endX - The distance in pixel from the left side of the canvas to the end of the curve.
      endY - The distance in pixel from the top side of the canvas to the end of the curve.
      Returns:
      An instance of this path which can be used to complete the shape.
    • arcTo

      public Path arcTo(double centerX, double centerY, double sweepRadians)
      Draws an arc with the center being the (centerX, centerY) coordinates. The total length of the arc is defined by the sweepRadians parameter. The underlying image class can be used to change the Image.setLineWidth(double).
      Parameters:
      centerX - The distance in pixel from the left side of the canvas to the center of the arc.
      centerY - The distance in pixel from the top side of the canvas to the center of the arc.
      sweepRadians - The length of the arc in radians from the current Position in a clockwise direction.
      Returns:
      An instance of this path which can be used to complete the shape.
    • complete

      public void complete()
      Completes the shapes by connecting the start point with the last position of this path. This method has to be called to draw the path to the image.
    • toString

      public String toString()
      Overrides:
      toString in class Object