Package de.vatrasoft.vatralib.model
Class Rectangle
- java.lang.Object
-
- de.vatrasoft.vatralib.model.Rectangle
-
public class Rectangle extends java.lang.ObjectThis class represents immutable two-dimensional rectangles.
-
-
Constructor Summary
Constructors Constructor Description Rectangle(int x, int y, int width, int height)Constructs a newRectanglewith the given coordinates and dimensions.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RectangleaddCoordinates(int xOffset, int yOffset)Returns a newRectanglethat has the same dimensions as this rectangle, but is translated by the specified offsets.booleancontains(Rectangle other)Returnstrueif the givenRectangleis fully within thisRectangle.booleanequals(java.lang.Object o)Compares thisRectangleto the specifiedRectangle.intgetHeight()Returns the height of the rectangle.intgetWidth()Returns the width of the rectangle.intgetX()Returns the horizontal coordinate of the rectangle.intgetY()Returns the vertical coordinate of the rectangle.inthashCode()Returns a hash code for thisRectangle.booleanoverlaps(Rectangle other)Returnstrueif thisRectangleand the givenRectangleoverlap.java.lang.StringtoString()Returns a string representation of thisRectangle.
-
-
-
Constructor Detail
-
Rectangle
public Rectangle(int x, int y, int width, int height)Constructs a newRectanglewith the given coordinates and dimensions.- Parameters:
x- the horizontal coordinate of the rectangley- the vertical coordinate of the rectanglewidth- the width of the rectangle (must be a positive integer)height- the height of the rectangle (must be a positive integer)- Throws:
java.lang.IllegalArgumentException- ifwidthand/orwidthare not positive integers
-
-
Method Detail
-
getX
public int getX()
Returns the horizontal coordinate of the rectangle.- Returns:
- the horizontal coordinate of the rectangle
-
getY
public int getY()
Returns the vertical coordinate of the rectangle.- Returns:
- the vertical coordinate of the rectangle
-
getWidth
public int getWidth()
Returns the width of the rectangle.- Returns:
- the width of the rectangle
-
getHeight
public int getHeight()
Returns the height of the rectangle.- Returns:
- the height of the rectangle
-
addCoordinates
public Rectangle addCoordinates(int xOffset, int yOffset)
Returns a newRectanglethat has the same dimensions as this rectangle, but is translated by the specified offsets.- Parameters:
xOffset- the offset to add to this rectangle's x coordinate (may be zero or negative)yOffset- the offset to add to this rectangle's y coordinate (may be zero or negative)- Returns:
- a new
Rectanglethat has the same dimensions as this rectangle, but is translated by the specified offsets
-
overlaps
public boolean overlaps(Rectangle other)
Returnstrueif thisRectangleand the givenRectangleoverlap.- Parameters:
other- the rectangle that should be checked for overlapping with this rectangle- Returns:
trueif thisRectangleand the givenRectangleoverlap- Throws:
java.lang.NullPointerException- ifotherisnull
-
contains
public boolean contains(Rectangle other)
Returnstrueif the givenRectangleis fully within thisRectangle.- Parameters:
other- the rectangle that should be checked for containment within this rectangle- Returns:
trueif the givenRectangleis fully within thisRectangle- Throws:
java.lang.NullPointerException- ifotherisnull
-
toString
public java.lang.String toString()
Returns a string representation of thisRectangle.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this
Rectangle
-
equals
public boolean equals(java.lang.Object o)
Compares thisRectangleto the specifiedRectangle.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- the object to compare with- Returns:
trueif the objects are the same;falseotherwise
-
hashCode
public int hashCode()
Returns a hash code for thisRectangle.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code for this
Rectangle
-
-