Class Bounds

java.lang.Object
swingtree.layout.Bounds

@Immutable public final class Bounds extends Object
An immutable value object that represents the position and size of a component in the form of an x and y coordinate modeled by a Location object and a width and height modeled by a Size object. Note the rectangular bounds object is positioned in a coordinate system where the y-axis is growing positively downwards and the x-axis is growing positively to the right.

Also note that the equals(Object) and hashCode() methods are implemented to compare the Location and Size objects for value based equality instead of reference based equality.

  • Method Summary

    Modifier and Type
    Method
    Description
    float
    The bounds object has a location and size which form a rectangular area exposed by this method as a float value defined by the width multiplied by the height.
    boolean
    equals(int x, int y, int width, int height)
    A convent method to check if the specified x and y coordinates and width and height are equal to the location and size of this bounds object.
    boolean
     
    int
     
    boolean
    Allows you to check weather the bounds object has a height that is greater than zero.
    boolean
    hasHeight(int height)
    Allows you to check weather the bounds object has the specified height, which is true if the height is equal to the height of the Size of this bounds object (see size()).
    boolean
    hasSize(int width, int height)
    Allows you to check weather the bounds object has the specified width and height, which is true if the width and height are equal to the width and height of the Size of this bounds object (see size()).
    boolean
    Allows you to check weather the bounds object has a width that is greater than zero.
    boolean
    hasWidth(int width)
    Allows you to check weather the bounds object has the specified width, which is true if the width is equal to the width of the Size of this bounds object (see size()).
    If you think of the bounds object as a rectangle, then the Location defines the top left corner and the Size defines the width and height of the rectangle.
    static Bounds
    Returns an empty bounds object, which is the null object for this class.
    static Bounds
    of(float x, float y, float width, float height)
     
    static Bounds
    of(int x, int y, int width, int height)
    Returns a bounds object with the specified location and size in the form of x and y coordinates, width and height.
    static Bounds
    of(Location location, Size size)
    Returns a bounds object with the specified location and size.
    The Size of define the width and height of the bounds starting from the x and y coordinates of the Location.
    The bounds object has a location and size which form a rectangular area which can easily be converted to a Rectangle object using this method.
     
    withHeight(int height)
    Allows you to create an updated version of this bounds object with the specified height and the same x and y coordinates as well as width as this bounds instance.
    withWidth(int width)
    Allows you to create an updated version of this bounds object with the specified width and the same x and y coordinates as well as height as this bounds instance.
    withX(int x)
    Allows you to create an updated version of this bounds object with the specified x-coordinate and the same y-coordinate and size as this bounds instance.
    withY(int y)
    Allows you to create an updated version of this bounds object with the specified y-coordinate and the same x-coordinate and size as this bounds instance.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • none

      public static Bounds none()
      Returns an empty bounds object, which is the null object for this class.

      The returned bounds object has a location of Location.origin() and a size of Size.unknown().

      Returns:
      an empty bounds object that is the null object for this class.
    • of

      public static Bounds of(Location location, Size size)
      Returns a bounds object with the specified location and size.

      If the location is Location.origin() and the size is Size.unknown() then the none() object is returned.

      Parameters:
      location - the location of the bounds object.
      size - the size of the bounds object.
      Returns:
      a bounds object with the specified location and size.
    • of

      public static Bounds of(int x, int y, int width, int height)
      Returns a bounds object with the specified location and size in the form of x and y coordinates, width and height.

      If the width or height is less than zero then the none() object is returned.

      Parameters:
      x - the x coordinate of the location of the bounds object.
      y - the y coordinate of the location of the bounds object.
      width - the width of the bounds object.
      height - the height of the bounds object.
      Returns:
      a bounds object with the specified location and size in the form of x and y coordinates, width and height.
    • of

      public static Bounds of(float x, float y, float width, float height)
    • location

      public Location location()
      If you think of the bounds object as a rectangle, then the Location defines the top left corner and the Size defines the width and height of the rectangle. Note that the y-axis is growing positively downwards and the x-axis is growing positively to the right.
      Returns:
      The Location of this bounds object, which contains the x and y coordinates.
    • hasWidth

      public boolean hasWidth()
      Allows you to check weather the bounds object has a width that is greater than zero.
      Returns:
      The truth value of whether this bounds object has a width, which is true if the width is greater than zero.
    • hasHeight

      public boolean hasHeight()
      Allows you to check weather the bounds object has a height that is greater than zero.
      Returns:
      The truth value of whether this bounds object has a height, which is true if the height is greater than zero.
    • size

      public Size size()
      The Size of define the width and height of the bounds starting from the x and y coordinates of the Location. Note that the Location is always the top left corner of the bounds object where the y-axis is growing positively downwards and the x-axis is growing positively to the right.
      Returns:
      The Size of this bounds object, which contains the width and height.
    • withX

      public Bounds withX(int x)
      Allows you to create an updated version of this bounds object with the specified x-coordinate and the same y-coordinate and size as this bounds instance. See also withY(int), withWidth(int), and withHeight(int).
      Parameters:
      x - A new x coordinate for the location of this bounds object.
      Returns:
      A new bounds object with a new location that has the specified x coordinate.
    • withY

      public Bounds withY(int y)
      Allows you to create an updated version of this bounds object with the specified y-coordinate and the same x-coordinate and size as this bounds instance. See also withX(int), withWidth(int), and withHeight(int).
      Parameters:
      y - A new y coordinate for the location of this bounds object.
      Returns:
      A new bounds object with a new location that has the specified y coordinate.
    • withWidth

      public Bounds withWidth(int width)
      Allows you to create an updated version of this bounds object with the specified width and the same x and y coordinates as well as height as this bounds instance. See also withX(int), withY(int), and withHeight(int).
      Parameters:
      width - A new width for the size of this bounds object.
      Returns:
      A new bounds object with a new size that has the specified width.
    • withHeight

      public Bounds withHeight(int height)
      Allows you to create an updated version of this bounds object with the specified height and the same x and y coordinates as well as width as this bounds instance. See also withX(int), withY(int), and withWidth(int).
      Parameters:
      height - A new height for the size of this bounds object.
      Returns:
      A new bounds object with a new size that has the specified height.
    • hasSize

      public boolean hasSize(int width, int height)
      Allows you to check weather the bounds object has the specified width and height, which is true if the width and height are equal to the width and height of the Size of this bounds object (see size()).
      Parameters:
      width - A new width for the size of this bounds object.
      height - A new height for the size of this bounds object.
      Returns:
      A new bounds object with a new size that has the specified width and height.
    • hasWidth

      public boolean hasWidth(int width)
      Allows you to check weather the bounds object has the specified width, which is true if the width is equal to the width of the Size of this bounds object (see size()).
      Parameters:
      width - An integer value to compare to the width of this bounds object.
      Returns:
      The truth value of whether the specified width is equal to the width of this bounds object.
    • hasHeight

      public boolean hasHeight(int height)
      Allows you to check weather the bounds object has the specified height, which is true if the height is equal to the height of the Size of this bounds object (see size()).
      Parameters:
      height - An integer value to compare to the height of this bounds object.
      Returns:
      The truth value of whether the specified height is equal to the height of this bounds object.
    • area

      public float area()
      The bounds object has a location and size which form a rectangular area exposed by this method as a float value defined by the width multiplied by the height.
      Returns:
      The area of this bounds object, which is the width multiplied by the height.
    • toRectangle

      public Rectangle toRectangle()
      The bounds object has a location and size which form a rectangular area which can easily be converted to a Rectangle object using this method.
      Returns:
      A Rectangle object with the same location and size as this bounds object.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(int x, int y, int width, int height)
      A convent method to check if the specified x and y coordinates and width and height are equal to the location and size of this bounds object. This is equivalent to calling equals(Object) with a new bounds object created with the specified x and y coordinates and width and height like so: equals(Bounds.of(x, y, width, height)).
      Parameters:
      x - An integer value to compare to the x coordinate of the location of this bounds object.
      y - An integer value to compare to the y coordinate of the location of this bounds object.
      width - An integer value to compare to the width of this bounds object.
      height - An integer value to compare to the height of this bounds object.
      Returns:
      The truth value of whether the specified x and y coordinates and width and height are equal to the location and size of this bounds object.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object