Class ShapeUtilities


  • public final class ShapeUtilities
    extends java.lang.Object
    A set of utilities on Java2D shapes.
    Version:
    $Id$
    Author:
    John Reekie, Michael Shilman
    • Constructor Summary

      Constructors 
      Constructor Description
      ShapeUtilities()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.Shape cloneShape​(java.awt.Shape s)
      Clone a shape.
      static java.awt.geom.Rectangle2D computeStrokedBounds​(java.awt.Shape shape, java.awt.Stroke stroke)
      Compute the bounds of a shape when stroked with the given stroke.
      static java.awt.Shape createCloudShape()
      Create the "cloud" shape.
      static java.awt.Shape createSwatchShape()
      Create the "swatch" shape.
      static java.awt.BasicStroke getStroke​(float floatwidth)
      Get a new stroke of the given width and with no dashing.
      static java.awt.BasicStroke getStroke​(int width)
      Get a stroke of the given width and with no dashing.
      static boolean intersectsOutline​(java.awt.geom.Rectangle2D r, java.awt.Shape s)
      Return true if the outline of the given shape intersects with the given rectangle.
      static boolean isOrthogonal​(java.awt.geom.AffineTransform at)
      Return true if the given transform maps a rectangle to a rectangle.
      static boolean jdk12beta4()
      Test if we are running in JDK1.2beta4
      static void main​(java.lang.String[] argv)
      Main function.
      static java.lang.String printShapeAsCode​(java.lang.String name, java.awt.Shape shape)
      Print a Shape to a String, as a code fragment that creates a new GeneralPath.
      static java.awt.geom.Rectangle2D transformBounds​(java.awt.geom.Rectangle2D rect, java.awt.geom.AffineTransform at)
      Given a bounding-box rectangle, return a new rectangle by transforming the argument rectangle and taking the bounding box of the result.
      static java.awt.Shape transformModify​(java.awt.Shape s, java.awt.geom.AffineTransform at)
      Transform a shape with the supplied transform.
      static void transformModifyRect​(java.awt.geom.RectangularShape s, java.awt.geom.AffineTransform at)
      In-place transform of a rectangular shape.
      static java.awt.Shape translateModify​(java.awt.Shape s, double x, double y)
      Translate a shape the given distance.
      • Methods inherited from class java.lang.Object

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

      • ShapeUtilities

        public ShapeUtilities()
    • Method Detail

      • jdk12beta4

        public static boolean jdk12beta4()
        Test if we are running in JDK1.2beta4
      • cloneShape

        public static java.awt.Shape cloneShape​(java.awt.Shape s)
        Clone a shape. This method is needed because Shape does not define clone(), although many shape classes do.
      • createCloudShape

        public static java.awt.Shape createCloudShape()
        Create the "cloud" shape. If running in JDK1.2 beta4, generate the shape on-the-fly using the Area class and return it. If running in JDK1.2 final, which has the screwed-up Area code, just generate a hardwired path (using code that was generated by running in beta4!!!) The shape is bounded by the rectangle (0,0,100,100).
      • createSwatchShape

        public static java.awt.Shape createSwatchShape()
        Create the "swatch" shape. If running in JDK1.2 beta4, generate the shape on-the-fly using the Area class and return it. If running in JDK1.2 final, which has the screwed-up Area code, just generate a hardwired path (using code that was generated by running in beta4!!!) The shape is bounded by the rectangle (0,0,100,100).
      • computeStrokedBounds

        public static java.awt.geom.Rectangle2D computeStrokedBounds​(java.awt.Shape shape,
                                                                     java.awt.Stroke stroke)
        Compute the bounds of a shape when stroked with the given stroke.
      • getStroke

        public static java.awt.BasicStroke getStroke​(int width)
        Get a stroke of the given width and with no dashing. This method will generally return an existing stroke object, and can be used to save creating zillions of Stroke objects.
      • getStroke

        public static java.awt.BasicStroke getStroke​(float floatwidth)
        Get a new stroke of the given width and with no dashing. This method will return an existing stroke object if the width is integer-valued and has a reasonably small width. This method can be used to save creating zillions of Stroke objects.
      • intersectsOutline

        public static boolean intersectsOutline​(java.awt.geom.Rectangle2D r,
                                                java.awt.Shape s)
        Return true if the outline of the given shape intersects with the given rectangle.
      • isOrthogonal

        public static boolean isOrthogonal​(java.awt.geom.AffineTransform at)
        Return true if the given transform maps a rectangle to a rectangle. If this is true, then the transform is optimized when using the transform methods of this class.
      • printShapeAsCode

        public static java.lang.String printShapeAsCode​(java.lang.String name,
                                                        java.awt.Shape shape)
        Print a Shape to a String, as a code fragment that creates a new GeneralPath.
      • transformBounds

        public static java.awt.geom.Rectangle2D transformBounds​(java.awt.geom.Rectangle2D rect,
                                                                java.awt.geom.AffineTransform at)
        Given a bounding-box rectangle, return a new rectangle by transforming the argument rectangle and taking the bounding box of the result. This method is essentially a faster version of AffineTransform.createTransformShape() if the shape is a Rectangle2D and the transform is identity, or orthogonal. Note that the argument rectangle is not modified, and the transform does not need to be orthogonal.
      • transformModifyRect

        public static void transformModifyRect​(java.awt.geom.RectangularShape s,
                                               java.awt.geom.AffineTransform at)
        In-place transform of a rectangular shape. The passed rectangular shape will be modified according to the given transform. This is essentially an in-place implementation of the ShapeUtilities.transformBounds() method.
      • transformModify

        public static java.awt.Shape transformModify​(java.awt.Shape s,
                                                     java.awt.geom.AffineTransform at)
        Transform a shape with the supplied transform. If possible, this method modifies the shape directly and returns a pointer to that same shape. In particular, instances of Rectangle2D, Polygon2D, Polyline2D, and GeneralPath will be modified directly. (In the case of RectangularShape, only if the transform is orthogonal.) Otherwise, a general transformation is used that creates and returns a new instance of GeneralPath.
      • translateModify

        public static java.awt.Shape translateModify​(java.awt.Shape s,
                                                     double x,
                                                     double y)
        Translate a shape the given distance. If possible, this method modifies the shape directly and returns a pointer to that same shape. In particular, instances of RectangularShape, Polygon2D, Polyline2D, and GeneralPath are modified directly. Otherwise, a general transformation is used that creates and returns a new instance of GeneralPath.
      • main

        public static void main​(java.lang.String[] argv)
        Main function. Depending on what version of Java we are running, print out a bunch of information to stdout.