Class MeasuredShape

  • All Implemented Interfaces:
    Serializable


    public class MeasuredShape
    extends Object
    implements Serializable
    This represents a single closed path.

    This object can trace arbitrary amounts of itself using the writeShape() methods.

    See Also:
    Serialized Form
    • Field Summary

      Fields

      Modifier and Type Field and Description
      static float DEFAULT_SPACING
      This is the increments t goes throw as each shape segment is traversed.
    • Method Summary

      Modifier and Type Method and Description
      float getClosedDistance()
      The distance of this shape, assuming that the path is closed.
      float getCommonDistance(MeasuredShape s)
      Returns the length that this shape has in common with the argument.
      float getMoveToX()
      Returns the x-value of where this path begins.
      float getMoveToY()
      Returns the y-value of where this path begins.
      float getOriginalDistance()
      The distance of the shape used to construct this MeasuredShape.
      Point2D getPoint(float distance, Point2D dest)
      Returns the point at a certain distance from the beginning of this shape.
      GeneralPath getShape(float position, float length)
      Trace the shape.
      static MeasuredShape[] getSubpaths(PathIterator i)
      Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
      static MeasuredShape[] getSubpaths(PathIterator i, float spacing)
      Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
      static MeasuredShape[] getSubpaths(Shape s)
      Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
      static MeasuredShape[] getSubpaths(Shape s, float spacing)
      Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
      float getTangentSlope(float distance)
      Returns the tangent slope at a certain distance from the beginning of this shape.
      void writeShape(float position, float length, PathWriter w)
      Trace the shape.
      void writeShape(float position, float length, PathWriter w, boolean includeMoveTo)
      Trace the shape.
      void writeShape(PathWriter w)
      Writes the entire shape
      void writeShapeBackwards(PathWriter w)
      Writes the entire shape backwards
    • Field Detail

      • DEFAULT_SPACING

        public static final float DEFAULT_SPACING
        This is the increments t goes throw as each shape segment is traversed. For quadratic and cubic curves, this affects how the shape distance is measured. The default value is .05, meaning quadratic and cubic curves are converted to linear segments connecting at t = 0, t = .05, t = .1, ... t = .95, t = 1.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MeasuredShape

        public MeasuredShape(Shape s)
        Construct a MeasuredShape from a Shape, using the default spacing.
        Parameters:
        s - the shape data
        Throws:
        IllegalArgumentException - if the shape has more than 1 path.
      • MeasuredShape

        public MeasuredShape(Shape s,
                             float spacing)
        Construct a MeasuredShape from a Shape.
        Parameters:
        s - the shape data to create
        spacing - the value to increment t as each segment is traversed. The default value is .05.
        Throws:
        IllegalArgumentException - if the shape has more than 1 path.
      • MeasuredShape

        public MeasuredShape(PathIterator i)
        Construct a MeasuredShape from a PathIterator using the default spacing.
        Parameters:
        i - the shape data to create
        Throws:
        IllegalArgumentException - if the shape has more than 1 path.
      • MeasuredShape

        public MeasuredShape(PathIterator i,
                             float spacing)
        Construct a MeasuredShape from a PathIterator.
        Parameters:
        i - the shape data to create
        spacing - the value to increment t as each segment is traversed. The default value is .05.
        Throws:
        IllegalArgumentException - if the shape has more than 1 path.
    • Method Detail

      • getSubpaths

        public static MeasuredShape[] getSubpaths(Shape s)
        Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
        Parameters:
        s - a path, possibly containing multiple subpaths
        Returns:
        a MeasuredShape object for each subpath in i
      • getSubpaths

        public static MeasuredShape[] getSubpaths(Shape s,
                                                  float spacing)
        Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
        Parameters:
        s - a path, possibly containing multiple subpaths
        spacing - the spacing to be used for each MeasuredShape
        Returns:
        a MeasuredShape object for each subpath in i
      • getSubpaths

        public static MeasuredShape[] getSubpaths(PathIterator i)
        Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
        Parameters:
        i - a path, possibly containing multiple subpaths
        Returns:
        a MeasuredShape object for each subpath in i
      • getSubpaths

        public static MeasuredShape[] getSubpaths(PathIterator i,
                                                  float spacing)
        Because a MeasuredShape must be exactly 1 subpath, this method will safely break up a path into separate subpaths and create one MeasuredShape for each.
        Parameters:
        i - a path, possibly containing multiple subpaths
        Returns:
        a MeasuredShape object for each subpath in i
      • writeShape

        public void writeShape(PathWriter w)
        Writes the entire shape
        Parameters:
        w - the destination to write to
      • getClosedDistance

        public float getClosedDistance()
        The distance of this shape, assuming that the path is closed. This will be greater than or equal to getOriginalDistance().
        See Also:
        getOriginalDistance()
      • getOriginalDistance

        public float getOriginalDistance()
        The distance of the shape used to construct this MeasuredShape.

        This will be less than or equal to getClosedDistance().

        Returns:
        The distance this path covered when the shape was constructed.
        See Also:
        getClosedDistance()
      • writeShapeBackwards

        public void writeShapeBackwards(PathWriter w)
        Writes the entire shape backwards
        Parameters:
        w - the destination to write to
      • getMoveToX

        public float getMoveToX()
        Returns the x-value of where this path begins.

        Because a MeasuredShape can only be one path, there is only possible moveTo().

        Returns:
        the x-value of where this path begins.
      • getMoveToY

        public float getMoveToY()
        Returns the y-value of where this path begins.

        Because a MeasuredShape can only be one path, there is only possible moveTo().

        Returns:
        the y-value of where this path begins.
      • writeShape

        public void writeShape(float position,
                               float length,
                               PathWriter w)
        Trace the shape.
        Parameters:
        position - a fraction from zero to one indicating where to start tracing
        length - a fraction from negative one to one indicating how much to trace. If this value is negative then the shape will be traced backwards.
        w - the destination to write to
      • writeShape

        public void writeShape(float position,
                               float length,
                               PathWriter w,
                               boolean includeMoveTo)
        Trace the shape.
        Parameters:
        position - a fraction from zero to one indicating where to start tracing
        length - a fraction from negative one to one indicating how much to trace. If this value is negative then the shape will be traced backwards.
        w - the destination to write to
        includeMoveTo - this controls whether a moveTo is the first thing written to the path. Note setting this to false means its the caller's responsibility to make sure the path is in the correct position.
      • getPoint

        public Point2D getPoint(float distance,
                                Point2D dest)
        Returns the point at a certain distance from the beginning of this shape.
        Parameters:
        distance - the distance from the beginning of this shape to measure
        dest - the destination to store the result in. (If this is null a new Point2D will be constructed.)
        Returns:
        the point at a certain distance from the beginning of this shape. Note this will be dest if dest is non-null.
      • getTangentSlope

        public float getTangentSlope(float distance)
        Returns the tangent slope at a certain distance from the beginning of this shape. The behavior of this method when the point you request falls exactly on an edge (that is, when two bordering segments don't have a continuous slope) is undefined.
        Parameters:
        distance - the distance from the beginning of this shape to measure
        Returns:
        the tangent slope (in radians) at a specific position
      • getCommonDistance

        public float getCommonDistance(MeasuredShape s)
        Returns the length that this shape has in common with the argument. This assumes the two shapes begin at the same point, and in the same direction.
        Parameters:
        s -
      • getShape

        public GeneralPath getShape(float position,
                                    float length)
        Trace the shape.
        Parameters:
        position - a fraction from zero to one indicating where to start tracing
        length - a fraction from negative one to one indicating how much to trace. If this value is negative then the shape will be traced backwards.
        Returns:
        a new path