Class Rectangle

java.lang.Object
org.eclipse.swt.graphics.Rectangle
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
Rectangle.OfFloat

public sealed class Rectangle extends Object implements Serializable, Cloneable permits Rectangle.OfFloat
Instances of this class represent rectangular areas in an (x, y) coordinate system. The top left corner of the rectangle is specified by its x and y values, and the extent of the rectangle is specified by its width and height.

The coordinate space for rectangles and points is considered to have increasing values downward and to the right from its origin making this the normal, computer graphics oriented notion of (x, y) coordinates rather than the strict mathematical one.

The hashCode() method in this class uses the values of the public fields to compute the hash value. When storing instances of the class in hashed collections, do not modify these fields after the object has been inserted.

Application code does not need to explicitly release the resources managed by each instance when those instances are no longer required, and thus no dispose() method is provided.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Instances of this class represent Rectangle objects which supports values of Float type for it's fields
    static final class 
    Instances of this class represent Rectangle.OfFloat objects along with the context of the monitor in relation to which they are placed on the display.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    the height of the rectangle
    int
    the width of the rectangle
    int
    the x coordinate of the rectangle
    int
    the y coordinate of the rectangle
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rectangle(int x, int y, int width, int height)
    Construct a new instance of this class given the x, y, width and height values.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Rectangle rect)
    Destructively replaces the x, y, width and height values in the receiver with ones which represent the union of the rectangles specified by the receiver and the given rectangle.
    Creates and returns a copy of this Rectangle.
    boolean
    contains(int x, int y)
    Returns true if the point specified by the arguments is inside the area specified by the receiver, and false otherwise.
    boolean
    Returns true if the given point is inside the area specified by the receiver, and false otherwise.
    boolean
    equals(Object object)
    Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
    int
    Returns an integer hash code for the receiver.
    void
    Destructively replaces the x, y, width and height values in the receiver with ones which represent the intersection of the rectangles specified by the receiver and the given rectangle.
    Returns a new rectangle which represents the intersection of the receiver and the given rectangle.
    boolean
    intersects(int x, int y, int width, int height)
    Returns true if the rectangle described by the arguments intersects with the receiver and false otherwise.
    boolean
    Returns true if the given rectangle intersects with the receiver and false otherwise.
    boolean
    Returns true if the receiver does not cover any area in the (x, y) coordinate plane, and false if the receiver does cover some area in the plane.
    static Rectangle
    of(Point topLeft, int width, int height)
    Creates a new Rectangle using the specified top-left point and dimensions.
    Returns a string containing a concise, human-readable description of the receiver.
    Returns a new rectangle which represents the union of the receiver and the given rectangle.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      public int x
      the x coordinate of the rectangle
    • y

      public int y
      the y coordinate of the rectangle
    • width

      public int width
      the width of the rectangle
    • height

      public int height
      the height of the rectangle
  • Constructor Details

    • Rectangle

      public Rectangle(int x, int y, int width, int height)
      Construct a new instance of this class given the x, y, width and height values.
      Parameters:
      x - the x coordinate of the origin of the rectangle
      y - the y coordinate of the origin of the rectangle
      width - the width of the rectangle
      height - the height of the rectangle
  • Method Details

    • add

      public void add(Rectangle rect)
      Destructively replaces the x, y, width and height values in the receiver with ones which represent the union of the rectangles specified by the receiver and the given rectangle.

      The union of two rectangles is the smallest single rectangle that completely covers both of the areas covered by the two given rectangles.

      Parameters:
      rect - the rectangle to merge with the receiver
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
    • contains

      public boolean contains(int x, int y)
      Returns true if the point specified by the arguments is inside the area specified by the receiver, and false otherwise.
      Parameters:
      x - the x coordinate of the point to test for containment
      y - the y coordinate of the point to test for containment
      Returns:
      true if the rectangle contains the point and false otherwise
    • contains

      public boolean contains(Point pt)
      Returns true if the given point is inside the area specified by the receiver, and false otherwise.
      Parameters:
      pt - the point to test for containment
      Returns:
      true if the rectangle contains the point and false otherwise
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
    • equals

      public boolean equals(Object object)
      Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare with this object
      Returns:
      true if the object is the same as this object and false otherwise
      See Also:
    • hashCode

      public int hashCode()
      Returns an integer hash code for the receiver. Any two objects that return true when passed to equals must return the same value for this method.
      Overrides:
      hashCode in class Object
      Returns:
      the receiver's hash
      See Also:
    • intersect

      public void intersect(Rectangle rect)
      Destructively replaces the x, y, width and height values in the receiver with ones which represent the intersection of the rectangles specified by the receiver and the given rectangle.
      Parameters:
      rect - the rectangle to intersect with the receiver
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
      since 3.0
    • intersection

      public Rectangle intersection(Rectangle rect)
      Returns a new rectangle which represents the intersection of the receiver and the given rectangle.

      The intersection of two rectangles is the rectangle that covers the area which is contained within both rectangles.

      Parameters:
      rect - the rectangle to intersect with the receiver
      Returns:
      the intersection of the receiver and the argument
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
    • intersects

      public boolean intersects(int x, int y, int width, int height)
      Returns true if the rectangle described by the arguments intersects with the receiver and false otherwise.

      Two rectangles intersect if the area of the rectangle representing their intersection is not empty.

      Parameters:
      x - the x coordinate of the origin of the rectangle
      y - the y coordinate of the origin of the rectangle
      width - the width of the rectangle
      height - the height of the rectangle
      Returns:
      true if the rectangle intersects with the receiver, and false otherwise
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
      Since:
      3.0
      See Also:
    • intersects

      public boolean intersects(Rectangle rect)
      Returns true if the given rectangle intersects with the receiver and false otherwise.

      Two rectangles intersect if the area of the rectangle representing their intersection is not empty.

      Parameters:
      rect - the rectangle to test for intersection
      Returns:
      true if the rectangle intersects with the receiver, and false otherwise
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
      See Also:
    • isEmpty

      public boolean isEmpty()
      Returns true if the receiver does not cover any area in the (x, y) coordinate plane, and false if the receiver does cover some area in the plane.

      A rectangle is considered to cover area in the (x, y) coordinate plane if both its width and height are non-zero.

      Returns:
      true if the receiver is empty, and false otherwise
    • toString

      public String toString()
      Returns a string containing a concise, human-readable description of the receiver.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the rectangle
    • union

      public Rectangle union(Rectangle rect)
      Returns a new rectangle which represents the union of the receiver and the given rectangle.

      The union of two rectangles is the smallest single rectangle that completely covers both of the areas covered by the two given rectangles.

      Parameters:
      rect - the rectangle to perform union with
      Returns:
      the union of the receiver and the argument
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the argument is null
      See Also:
    • of

      public static Rectangle of(Point topLeft, int width, int height)
      Creates a new Rectangle using the specified top-left point and dimensions.

      If the provided Point instance carries additional contextual information, an extended Rectangle type may be returned to preserve that context. Otherwise, a standard Rectangle is returned.

      Parameters:
      topLeft - the top-left corner of the rectangle
      width - the width of the rectangle
      height - the height of the rectangle
      Returns:
      a new Rectangle instance appropriate for the given point and dimensions
      Since:
      3.131
    • clone

      public Rectangle clone()
      Creates and returns a copy of this Rectangle.

      This method performs a shallow copy of the rectangle's fields: x, y, width, and height. It does not copy any subclass-specific fields, so subclasses should override this method if additional fields exist.

      Overrides:
      clone in class Object
      Returns:
      a new Rectangle instance with the same position and size as this one
      Since:
      3.131