Class ShapeUtilities


  • public final class ShapeUtilities
    extends java.lang.Object
    Utility methods for shapes and the paths that delineate them.
    Author:
    Chris Jennings
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double DEFAULT_FLATNESS
      The default flatness for flattening path iterators; equal to 1.
      static int DEFAULT_LIMIT
      The default limit for flattening path iterators; equal to 10.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.geom.Area add​(java.awt.Shape lhs, java.awt.Shape rhs)
      Returns a Shape that is the union of the two shape parameters: all areas that are part of either shape will be in the returned shape.
      static java.awt.geom.Area exclusiveOr​(java.awt.Shape lhs, java.awt.Shape rhs)
      Returns a Shape that is the exclusive or of the two shape parameters: areas in one shape or the other will be included in the returned shape, excluding areas that are present in both.
      static java.awt.geom.Area intersect​(java.awt.Shape lhs, java.awt.Shape rhs)
      Returns a Shape that is the intersection of the two shape parameters: only areas that are part both shapes will be in the returned shape.
      static double pathLength​(java.awt.Shape shape)
      Measures the length of a shape's outline by flattening its path to a sequence of line segments.
      static double pathLength​(java.awt.Shape shape, double flatness)
      Measures the length of a shape's outline by flattening its path to a sequence of line segments.
      static double pathLength​(java.awt.Shape shape, double flatness, int limit)
      Measures the length of a shape's outline by flattening its path to a sequence of line segments.
      static java.awt.geom.Area subtract​(java.awt.Shape lhs, java.awt.Shape rhs)
      Returns a Shape that is the difference of the two shape parameters: the result is the lhs shape with any parts that overlap the rhs removed.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_FLATNESS

        public static final double DEFAULT_FLATNESS
        The default flatness for flattening path iterators; equal to 1.
        See Also:
        Constant Field Values
      • DEFAULT_LIMIT

        public static final int DEFAULT_LIMIT
        The default limit for flattening path iterators; equal to 10.
        See Also:
        Constant Field Values
    • Method Detail

      • pathLength

        public static double pathLength​(java.awt.Shape shape)
        Measures the length of a shape's outline by flattening its path to a sequence of line segments. Jumps caused by moving to another point without creating a line or curve are not counted.
        Parameters:
        shape - the shape whose outline is to be measured
        Returns:
        the sum of the lengths of the line segments of the flattened curve
        Throws:
        java.lang.NullPointerException - if shape is null
        java.lang.IllegalArgumentException - if flatness or limit is less than 0
      • pathLength

        public static double pathLength​(java.awt.Shape shape,
                                        double flatness)
        Measures the length of a shape's outline by flattening its path to a sequence of line segments. Jumps caused by moving to another point without creating a line or curve are not counted.
        Parameters:
        shape - the shape whose outline is to be measured
        flatness - the maximum allowable distance between the control points and the flattened curve
        Returns:
        the sum of the lengths of the line segments of the flattened curve
        Throws:
        java.lang.NullPointerException - if shape is null
        java.lang.IllegalArgumentException - if flatness or limit is less than 0
      • pathLength

        public static double pathLength​(java.awt.Shape shape,
                                        double flatness,
                                        int limit)
        Measures the length of a shape's outline by flattening its path to a sequence of line segments. Jumps caused by moving to another point without creating a line or curve are not counted.
        Parameters:
        shape - the shape whose outline is to be measured
        flatness - the maximum allowable distance between the control points and the flattened curve
        limit - log2 of the maximum number of line segments that will be generated for any curved segment of the path
        Returns:
        the sum of the lengths of the line segments of the flattened curve
        Throws:
        java.lang.NullPointerException - if shape is null
        java.lang.IllegalArgumentException - if flatness or limit is less than 0
      • add

        public static java.awt.geom.Area add​(java.awt.Shape lhs,
                                             java.awt.Shape rhs)
        Returns a Shape that is the union of the two shape parameters: all areas that are part of either shape will be in the returned shape. Neither of original shapes will be modified.
        Parameters:
        lhs - the primary shape
        rhs - the shape to add to the primary shape
        Returns:
        the union of the two shapes
      • subtract

        public static java.awt.geom.Area subtract​(java.awt.Shape lhs,
                                                  java.awt.Shape rhs)
        Returns a Shape that is the difference of the two shape parameters: the result is the lhs shape with any parts that overlap the rhs removed. Neither of original shapes will be modified.
        Parameters:
        lhs - the primary shape
        rhs - the shape to subtract from the primary shape
        Returns:
        the difference of the two shapes
      • intersect

        public static java.awt.geom.Area intersect​(java.awt.Shape lhs,
                                                   java.awt.Shape rhs)
        Returns a Shape that is the intersection of the two shape parameters: only areas that are part both shapes will be in the returned shape. Neither of original shapes will be modified.
        Parameters:
        lhs - the primary shape
        rhs - the shape to intersect with the primary shape
        Returns:
        the intersection of the two shapes
      • exclusiveOr

        public static java.awt.geom.Area exclusiveOr​(java.awt.Shape lhs,
                                                     java.awt.Shape rhs)
        Returns a Shape that is the exclusive or of the two shape parameters: areas in one shape or the other will be included in the returned shape, excluding areas that are present in both. This is equivalent to union(lhs,subtract(lhs,rhs)). Neither of original shapes will be modified.
        Parameters:
        lhs - the primary shape
        rhs - the shape to intersect with the primary shape
        Returns:
        the intersection of the two shapes