Class Rectangle


  • public class Rectangle
    extends java.lang.Object
    This class represents immutable two-dimensional rectangles.
    • Constructor Summary

      Constructors 
      Constructor Description
      Rectangle​(int x, int y, int width, int height)
      Constructs a new Rectangle with the given coordinates and dimensions.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Rectangle addCoordinates​(int xOffset, int yOffset)
      Returns a new Rectangle that has the same dimensions as this rectangle, but is translated by the specified offsets.
      boolean contains​(Rectangle other)
      Returns true if the given Rectangle is fully within this Rectangle.
      boolean equals​(java.lang.Object o)
      Compares this Rectangle to the specified Rectangle.
      int getHeight()
      Returns the height of the rectangle.
      int getWidth()
      Returns the width of the rectangle.
      int getX()
      Returns the horizontal coordinate of the rectangle.
      int getY()
      Returns the vertical coordinate of the rectangle.
      int hashCode()
      Returns a hash code for this Rectangle.
      boolean overlaps​(Rectangle other)
      Returns true if this Rectangle and the given Rectangle overlap.
      java.lang.String toString()
      Returns a string representation of this Rectangle.
      • Methods inherited from class java.lang.Object

        clone, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Rectangle

        public Rectangle​(int x,
                         int y,
                         int width,
                         int height)
        Constructs a new Rectangle with the given coordinates and dimensions.
        Parameters:
        x - the horizontal coordinate of the rectangle
        y - the vertical coordinate of the rectangle
        width - 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 - if width and/or width are 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 new Rectangle that 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 Rectangle that has the same dimensions as this rectangle, but is translated by the specified offsets
      • overlaps

        public boolean overlaps​(Rectangle other)
        Returns true if this Rectangle and the given Rectangle overlap.
        Parameters:
        other - the rectangle that should be checked for overlapping with this rectangle
        Returns:
        true if this Rectangle and the given Rectangle overlap
        Throws:
        java.lang.NullPointerException - if other is null
      • contains

        public boolean contains​(Rectangle other)
        Returns true if the given Rectangle is fully within this Rectangle.
        Parameters:
        other - the rectangle that should be checked for containment within this rectangle
        Returns:
        true if the given Rectangle is fully within this Rectangle
        Throws:
        java.lang.NullPointerException - if other is null
      • toString

        public java.lang.String toString()
        Returns a string representation of this Rectangle.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this Rectangle
      • equals

        public boolean equals​(java.lang.Object o)
        Compares this Rectangle to the specified Rectangle.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the object to compare with
        Returns:
        true if the objects are the same; false otherwise
      • hashCode

        public int hashCode()
        Returns a hash code for this Rectangle.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a hash code for this Rectangle