Class Bounds

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Bounds
    extends java.lang.Object
    implements java.io.Serializable
    Class representing the bounds of an object (that is, the X- and Y-position plus the width and height).
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Bounds EMPTY
      An empty (zero position and size) area.
      int height
      The height of the area.
      static java.lang.String HEIGHT_KEY
      The map key to retrieve the height.
      int width
      The width of the area.
      static java.lang.String WIDTH_KEY
      The map key to retrieve the width.
      int x
      The X-position of the bounding area.
      static java.lang.String X_KEY
      The map key to retrieve the X position.
      int y
      The Y-position of the bounding area.
      static java.lang.String Y_KEY
      The map key to retrieve the Y position.
    • Constructor Summary

      Constructors 
      Constructor Description
      Bounds​(int xPos, int yPos, int widthValue, int heightValue)
      Construct a bounds object given all four values for it.
      Bounds​(java.awt.Rectangle rectangle)
      Convert a Rectangle to one of our bounds objects.
      Bounds​(Dictionary<java.lang.String,​?> bounds)
      Construct a new bounds object given a map/dictionary of the four needed values.
      Bounds​(Sequence<?> bounds)
      Construct a new bounds object given a sequence of the four needed values.
      Bounds​(Bounds bounds)
      Construct a new bounds object from an existing bounds.
      Bounds​(Dimensions size)
      Construct a new Bounds which has the given size and a (0, 0) origin.
      Bounds​(Point origin, Dimensions size)
      Construct a bounds object given the origin position and size.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(int xValue, int yValue)
      Does this bounded area contain the point defined by the given arguments?
      boolean contains​(int xValue, int yValue, int widthValue, int heightValue)  
      boolean contains​(Bounds bounds)  
      boolean contains​(Point point)  
      boolean containsX​(int xValue)
      Does this bounded area contain the X point defined by the given value?
      boolean containsY​(int yValue)
      Does this bounded area contain the Y point defined by the given value?
      static Bounds decode​(java.lang.String boundsValue)
      Decode a JSON-encoded string (map or list) that contains the values for a new bounded area.
      Bounds enlarge​(int amt)
      Create a new bounds object that is both offset and expanded by a single amount in both directions.
      Bounds enlarge​(int dx, int dy)
      Create a new bounds object that is enlarged by different amounts in the X- and Y-directions.
      boolean equals​(java.lang.Object object)  
      Bounds expand​(int dw, int dh)
      Create a new bounds object that represents this bounds expanded/contracted by the given width/height values (negative represent contraction).
      Point getLocation()  
      Dimensions getSize()  
      int hashCode()  
      Bounds intersect​(int xValue, int yValue, int widthValue, int heightValue)
      Create a new bounds that is the intersection of this one and the bounded area specified by the given arguments.
      Bounds intersect​(java.awt.Rectangle rect)  
      Bounds intersect​(Bounds bounds)  
      Bounds intersect​(Dimensions size)  
      boolean intersects​(int xValue, int yValue, int widthValue, int heightValue)  
      boolean intersects​(Bounds bounds)  
      boolean isEmpty()
      Does this bounds represent an empty area?
      java.awt.Rectangle toRectangle()  
      java.lang.String toString()  
      Bounds translate​(int dx, int dy)
      Create a new bounds object that represents this bounds offset by the given values.
      Bounds translate​(Point offset)  
      Bounds union​(int xValue, int yValue, int widthValue, int heightValue)
      Create a new bounds that is the union of this one and the given arguments.
      Bounds union​(Bounds bounds)  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • x

        public final int x
        The X-position of the bounding area.
      • y

        public final int y
        The Y-position of the bounding area.
      • width

        public final int width
        The width of the area.
      • height

        public final int height
        The height of the area.
      • X_KEY

        public static final java.lang.String X_KEY
        The map key to retrieve the X position.
        See Also:
        Constant Field Values
      • Y_KEY

        public static final java.lang.String Y_KEY
        The map key to retrieve the Y position.
        See Also:
        Constant Field Values
      • WIDTH_KEY

        public static final java.lang.String WIDTH_KEY
        The map key to retrieve the width.
        See Also:
        Constant Field Values
      • HEIGHT_KEY

        public static final java.lang.String HEIGHT_KEY
        The map key to retrieve the height.
        See Also:
        Constant Field Values
      • EMPTY

        public static final Bounds EMPTY
        An empty (zero position and size) area.
    • Constructor Detail

      • Bounds

        public Bounds​(int xPos,
                      int yPos,
                      int widthValue,
                      int heightValue)
        Construct a bounds object given all four values for it.
        Parameters:
        xPos - The starting X-position of the area.
        yPos - The starting Y-position.
        widthValue - The width of the bounded area.
        heightValue - The height of the area.
      • Bounds

        public Bounds​(Point origin,
                      Dimensions size)
        Construct a bounds object given the origin position and size.
        Parameters:
        origin - The origin point of the area (must not be null).
        size - The size of the bounded area (must not be null).
        Throws:
        java.lang.IllegalArgumentException - if either argument is null.
      • Bounds

        public Bounds​(Dimensions size)
        Construct a new Bounds which has the given size and a (0, 0) origin.
        Parameters:
        size - The size of the bounded area (must not be null).
        Throws:
        java.lang.IllegalArgumentException - if the size is null.
      • Bounds

        public Bounds​(Bounds bounds)
        Construct a new bounds object from an existing bounds.
        Parameters:
        bounds - The existing bounds to copy (cannot be null).
        Throws:
        java.lang.IllegalArgumentException - if the argument is null.
      • Bounds

        public Bounds​(Dictionary<java.lang.String,​?> bounds)
        Construct a new bounds object given a map/dictionary of the four needed values. If any of the values is missing from the dictionary, the corresponding value in the bounds will be set to zero.
        Parameters:
        bounds - The dictionary containing the bounds values, which should contain an entry for X_KEY, Y_KEY, WIDTH_KEY and HEIGHT_KEY.
        Throws:
        java.lang.IllegalArgumentException - if the bounds argument is null.
      • Bounds

        public Bounds​(Sequence<?> bounds)
        Construct a new bounds object given a sequence of the four needed values.
        Parameters:
        bounds - The sequence containing the bounds values, in the order of [ x, y, width, height ]
        Throws:
        java.lang.IllegalArgumentException - if the bounds argument is null.
      • Bounds

        public Bounds​(java.awt.Rectangle rectangle)
        Convert a Rectangle to one of our bounds objects.
        Parameters:
        rectangle - The existing rectangle to convert (cannot be null).
        Throws:
        java.lang.IllegalArgumentException - if the rectangle is null.
    • Method Detail

      • getLocation

        public Point getLocation()
        Returns:
        The X- and Y-location of this bounded area in Point form.
      • getSize

        public Dimensions getSize()
        Returns:
        The width and height of this bounded area in Dimensions form.
      • union

        public Bounds union​(int xValue,
                            int yValue,
                            int widthValue,
                            int heightValue)
        Create a new bounds that is the union of this one and the given arguments.

        "Union" means the new bounds will include all of this bounds and the bounds specified by the arguments (X- and Y-position will be the minimum of either and width and height will be the maximum).

        Parameters:
        xValue - The X-position of the bounded area to union with this one.
        yValue - The Y-position of the other bounded area.
        widthValue - The width of the other area to union with this one.
        heightValue - The other area's height.
        Returns:
        A new bounds that is the union of this one with the bounds specified by the given arguments.
      • union

        public Bounds union​(Bounds bounds)
        Parameters:
        bounds - The other bounds to union with this one.
        Returns:
        A new bounds object that is the union of this bounds with the given one.
        Throws:
        java.lang.IllegalArgumentException - if the given bounds is null.
        See Also:
        union(int, int, int, int)
      • intersect

        public Bounds intersect​(int xValue,
                                int yValue,
                                int widthValue,
                                int heightValue)
        Create a new bounds that is the intersection of this one and the bounded area specified by the given arguments.

        "Intersection" means the new bounds will include only the area that is common to both areas (X- and Y-position will be the maximum of either, while width and height will be the minimum of the two).

        Parameters:
        xValue - The X-position of the other area to intersect with.
        yValue - The Y-position of the other area.
        widthValue - The width of the other bounded area.
        heightValue - The height of the other area.
        Returns:
        The new bounds that is the intersection of this one and the given area.
      • intersect

        public Bounds intersect​(Bounds bounds)
        Parameters:
        bounds - The other area to intersect with this one (cannot be null).
        Returns:
        A new bounds that is the intersection of this one and the given one.
        Throws:
        java.lang.IllegalArgumentException - if the given bounds is null.
        See Also:
        intersect(int, int, int, int)
      • intersect

        public Bounds intersect​(java.awt.Rectangle rect)
        Parameters:
        rect - The other rectangle to intersect with (must not be null).
        Returns:
        A new bounds object that is the intersection of this one with the given rectangle.
        Throws:
        java.lang.IllegalArgumentException - if the rectangle is null.
        See Also:
        intersect(int, int, int, int)
      • intersect

        public Bounds intersect​(Dimensions size)
        Parameters:
        size - The dimensions to intersect with (must not be null).
        Returns:
        A new bounds object that is the intersection of this one with the given area defined by (0,0) to (width, height) of the size value.
        Throws:
        java.lang.IllegalArgumentException - if the dimension is null.
        See Also:
        intersect(int, int, int, int)
      • translate

        public Bounds translate​(int dx,
                                int dy)
        Create a new bounds object that represents this bounds offset by the given values.

        The new bounds has the same width and height, but the X- and Y-positions have been offset by the given values (which can be either positive or negative).

        Parameters:
        dx - The amount of translation in the X-direction.
        dy - The amount of translation in the Y-direction.
        Returns:
        A new bounds offset by these amounts.
      • expand

        public Bounds expand​(int dw,
                             int dh)
        Create a new bounds object that represents this bounds expanded/contracted by the given width/height values (negative represent contraction).

        The new bounds have the same x- and y-origin values as the original.

        Parameters:
        dw - The amount of expansion(contraction) in the width.
        dh - The amount of expansion(contraction) in the height.
        Returns:
        A new bounds expanded by this amount.
      • translate

        public Bounds translate​(Point offset)
        Parameters:
        offset - X- and Y-values which are used to offset this bounds to a new position (must not be null).
        Returns:
        A new bounds offset by the amounts given by the point.
        Throws:
        java.lang.IllegalArgumentException - if the offset value is null.
        See Also:
        translate(int, int)
      • enlarge

        public Bounds enlarge​(int amt)
        Create a new bounds object that is both offset and expanded by a single amount in both directions. The X- and Y-offset values are moved up/left and the width and height are expanded by twice the amount. The center point of the bounds remains the same.

        This has the same effect as translate(amt, amt).expand(2*amt, 2*amt).

        Parameters:
        amt - The amount to expand both the X- and Y- directions.
        Returns:
        A new enlarged bounds.
      • enlarge

        public Bounds enlarge​(int dx,
                              int dy)
        Create a new bounds object that is enlarged by different amounts in the X- and Y-directions. The center point of the bounds remains the same.

        This has the same effect as translate(dx, dy).expand(2*dx, 2*dy).

        Parameters:
        dx - The amount to enlarge in the X-direction.
        dy - The amount to enlarge in the Y-direction.
        Returns:
        A new enlarged bounds.
      • contains

        public boolean contains​(Point point)
        Parameters:
        point - The other point to test (must not be null).
        Returns:
        Whether this bounded area contains the given point.
        Throws:
        java.lang.IllegalArgumentException - if the point argument is null.
        See Also:
        contains(int, int)
      • contains

        public boolean contains​(int xValue,
                                int yValue)
        Does this bounded area contain the point defined by the given arguments?
        Parameters:
        xValue - The X-position of the other point to test.
        yValue - The Y-position of the other point to test.
        Returns:
        Whether this bounds contains the given point.
      • contains

        public boolean contains​(Bounds bounds)
        Parameters:
        bounds - The other bounded area to check (must not be null).
        Returns:
        Does this bounded area completely contain (could be coincident with) the bounded area specified by the given argument?
        Throws:
        java.lang.IllegalArgumentException - if the given bounds is null.
        See Also:
        contains(int, int, int, int)
      • contains

        public boolean contains​(int xValue,
                                int yValue,
                                int widthValue,
                                int heightValue)
        Parameters:
        xValue - The X-position of the area to test.
        yValue - The Y-position of the other area.
        widthValue - The width of the other area.
        heightValue - The height of the area to test.
        Returns:
        Does this bounded area completely contain (could be coincident with) the bounded area specified by the given arguments?
      • containsX

        public boolean containsX​(int xValue)
        Does this bounded area contain the X point defined by the given value?
        Parameters:
        xValue - The X-position to test.
        Returns:
        Whether this bounds contains the given X point.
      • containsY

        public boolean containsY​(int yValue)
        Does this bounded area contain the Y point defined by the given value?
        Parameters:
        yValue - The Y-position to test.
        Returns:
        Whether this bounds contains the given Y point.
      • intersects

        public boolean intersects​(Bounds bounds)
        Parameters:
        bounds - The other area to test (must not be null).
        Returns:
        Does this bounded area intersect with the bounded area given by the argument?
        Throws:
        java.lang.IllegalArgumentException - if the given bounds is null.
        See Also:
        intersects(int, int, int, int)
      • intersects

        public boolean intersects​(int xValue,
                                  int yValue,
                                  int widthValue,
                                  int heightValue)
        Parameters:
        xValue - The X-position of the other area to check.
        yValue - The Y-position of the other area.
        widthValue - The width of the other bounded area.
        heightValue - The height of the other area.
        Returns:
        Does this bounded area intersect with the bounded area given by the arguments?
      • isEmpty

        public boolean isEmpty()
        Does this bounds represent an empty area?
        Returns:
        true if the width OR height of this bounded area is less than or equal to zero (in other words if it has EITHER no width or no height).
      • equals

        public boolean equals​(java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toRectangle

        public java.awt.Rectangle toRectangle()
        Returns:
        This bounded area as a Rectangle.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        A more-or-less human-readable representation of this object, which looks like:
        Bounds [X,Y;WxH]
      • decode

        public static Bounds decode​(java.lang.String boundsValue)
        Decode a JSON-encoded string (map or list) that contains the values for a new bounded area.

        The format of a JSON map format will be:

        { "x": nnn, "y": nnn, "width": nnn, "height": nnn }

        The format of a JSON list format will be:

        [ x, y, width, height ]

        Also accepted is a simple list (comma- or semicolon-separated) of four integer values.

        Parameters:
        boundsValue - The JSON string containing the map or list of bounds values (must not be null).
        Returns:
        The new bounds object if the string can be successfully decoded.
        Throws:
        java.lang.IllegalArgumentException - if the given string is null or empty or the string could not be parsed as a JSON map or list.
        See Also:
        Bounds(Dictionary), Bounds(int, int, int, int)