Package swingtree.layout
Class Bounds
java.lang.Object
swingtree.layout.Bounds
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 TypeMethodDescriptionfloat
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.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
hashCode()
boolean
Allows you to check weather the bounds object has a height that is greater than zero.boolean
hasHeight
(int height) boolean
hasSize
(int width, int height) boolean
hasWidth()
Allows you to check weather the bounds object has a width that is greater than zero.boolean
hasWidth
(int width) location()
static Bounds
none()
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
Returns a bounds object with the specified location and size.size()
The bounds object has a location and size which form a rectangular area which can easily be converted to aRectangle
object using this method.toString()
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.
-
Method Details
-
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 ofSize.unknown()
.- Returns:
- an empty bounds object that is the null object for this class.
-
of
Returns a bounds object with the specified location and size.If the location is
Location.origin()
and the size isSize.unknown()
then thenone()
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
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
-
location
If you think of the bounds object as a rectangle, then theLocation
defines the top left corner and theSize
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
TheSize
of define the width and height of the bounds starting from the x and y coordinates of theLocation
. Note that theLocation
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
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 alsowithY(int)
,withWidth(int)
, andwithHeight(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
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 alsowithX(int)
,withWidth(int)
, andwithHeight(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
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 alsowithX(int)
,withY(int)
, andwithHeight(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
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 alsowithX(int)
,withY(int)
, andwithWidth(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 theSize
of this bounds object (seesize()
).- 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 theSize
of this bounds object (seesize()
).- 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 theSize
of this bounds object (seesize()
).- 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
The bounds object has a location and size which form a rectangular area which can easily be converted to aRectangle
object using this method.- Returns:
- A
Rectangle
object with the same location and size as this bounds object.
-
toString
-
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 callingequals(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
-
hashCode
public int hashCode()
-