Class ShapeTransform


  • public final class ShapeTransform
    extends Object
    Utility class, which resizes or translates a Shape. The class contains special handlers for Rectangles and Lines.
    Author:
    Thomas Morgner
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ABOVE
      Flag for point lying "above" clipping area.
      static int BELOW
      Flag for point lying "below" clipping area.
      static int H_CENTER
      Flag for point lying between horizontal bounds of area.
      static int INSIDE
      Mask for points which are inside.
      static int LEFT
      Flag for point lying left of clipping area.
      static int OUTSIDE
      Mask for points which are outside.
      static int RIGHT
      Flag for point lying right of clipping area.
      static int V_CENTER
      Flag for point lying between vertical bounds of clipping area.
    • Field Detail

      • LEFT

        public static final int LEFT
        Flag for point lying left of clipping area.
        See Also:
        Constant Field Values
      • H_CENTER

        public static final int H_CENTER
        Flag for point lying between horizontal bounds of area.
        See Also:
        Constant Field Values
      • RIGHT

        public static final int RIGHT
        Flag for point lying right of clipping area.
        See Also:
        Constant Field Values
      • BELOW

        public static final int BELOW
        Flag for point lying "below" clipping area.
        See Also:
        Constant Field Values
      • V_CENTER

        public static final int V_CENTER
        Flag for point lying between vertical bounds of clipping area.
        See Also:
        Constant Field Values
      • ABOVE

        public static final int ABOVE
        Flag for point lying "above" clipping area.
        See Also:
        Constant Field Values
      • INSIDE

        public static final int INSIDE
        Mask for points which are inside.
        See Also:
        Constant Field Values
      • OUTSIDE

        public static final int OUTSIDE
        Mask for points which are outside.
        See Also:
        Constant Field Values
    • Method Detail

      • resizeShape

        public static Shape resizeShape​(Shape s,
                                        float width,
                                        float height)
        Resizes a shape, so that the shape has the given width and height, but the origin of the shape does not change.

        Unlike the AffineTransform, this method tries to preserve the Shape's Type.

        Parameters:
        s - the shape
        width - the new width
        height - the new height
        Returns:
        the resized shape.
      • resizeRect

        public static Shape resizeRect​(RectangularShape rectangularShape,
                                       double width,
                                       double height)
        Resizes a rectangle. This works for real rectangles and produces funny results for RoundRects etc ..
        Parameters:
        rectangularShape - the rectangle
        width - the new width of the rectangle
        height - the new height of the rectangle.
        Returns:
        the resized rectangle.
      • transformShape

        public static Shape transformShape​(Shape s,
                                           boolean scale,
                                           boolean keepAR,
                                           double width,
                                           double height)
        Translates the given shape. The shape is translated to the origin supplied in point. If scaling is requested, the shape will also be scaled using an AffineTransform.
        Parameters:
        s - the shape that should be transformed
        scale - true, if the shape should be scaled, false otherwise
        keepAR - true, if the scaled shape should keep the aspect ratio
        width - the target width.
        height - the target height.
        Returns:
        the transformed shape
      • transformShape

        public static Shape transformShape​(Shape s,
                                           boolean scale,
                                           boolean keepAR,
                                           Dimension2D dim)
        Translates the given shape. The shape is translated to the origin supplied in point. If scaling is requested, the shape will also be scaled using an AffineTransform.
        Parameters:
        s - the shape that should be transformed
        scale - true, if the shape should be scaled, false otherwise
        keepAR - true, if the scaled shape should keep the aspect ratio
        dim - the target dimension.
        Returns:
        the transformed shape
      • performCliping

        public static Shape performCliping​(Shape s,
                                           Rectangle2D bounds)
        Clips the given shape to the given bounds. If the shape is a Line2D, manual clipping is performed, as the built in Area does not handle lines.
        Parameters:
        s - the shape to be clipped
        bounds - the bounds to which the shape should be clipped
        Returns:
        the clipped shape.
      • translateShape

        public static Shape translateShape​(Shape s,
                                           double x,
                                           double y)
        Translates a se. Special care is taken to preserve the shape's original class, if the shape is a rectangle or a line.
        Parameters:
        s - the shape
        x - the x coordinate where the shape is translated to
        y - the y coordinate where the shape is translated to
        Returns:
        the translated shape
      • getClipped

        public static Point2D[] getClipped​(double x1,
                                           double y1,
                                           double x2,
                                           double y2,
                                           double xmin,
                                           double xmax,
                                           double ymin,
                                           double ymax)
        Calculate the clipping points of a line with a rectangle.
        Parameters:
        x1 - starting x of line
        y1 - starting y of line
        x2 - ending x of line
        y2 - ending y of line
        xmin - lower left x of rectangle
        xmax - upper right x of rectangle
        ymin - lower left y of rectangle
        ymax - upper right y of rectangle
        Returns:
        null (does not clip) or array of two points