wyvern.lib
Class Rectangle

java.lang.Object
  extended bywyvern.lib.Rectangle
All Implemented Interfaces:
java.io.Serializable

public class Rectangle
extends java.lang.Object
implements java.io.Serializable

Represents a rectangle in a map.

Version:
1.0, Mar 5, 2002
Author:
Steve Yegge
See Also:
Serialized Form

Field Summary
 int height
           
 int width
           
 int x
           
 int y
           
 
Constructor Summary
Rectangle()
          Constructs a new Rectangle
Rectangle(int width, int height)
          Constructs a new Rectangle
Rectangle(int x, int y, int width, int height)
          Constructs a new Rectangle
Rectangle(Point p, int width, int height)
          Constructs a new Rectangle
Rectangle(Rectangle r)
          Constructs a new Rectangle
Rectangle(java.awt.Rectangle r)
          Constructs a new Rectangle from an AWT rectangle.
 
Method Summary
 void add(int newx, int newy)
          Adds a point, specified by the integer arguments newx and newy, to this Rectangle.
 void add(Point pt)
          Adds the specified Point to this Rectangle.
 void add(Rectangle r)
          Adds a Rectangle to this Rectangle.
 boolean contains(int x, int y)
          Returns true if the rectangle contains the specified point.
 boolean contains(int X, int Y, int W, int H)
          Checks whether this Rectangle entirely contains the Rectangle at the specified location (XY) with the specified dimensions (WH).
 boolean contains(Point p)
          Returns true if the rectangle contains the specified point.
 boolean contains(Rectangle r)
          Checks whether this Rectangle entirely contains the Rectangle at the specified location (XY) with the specified dimensions (WH).
 java.awt.Rectangle createAWTRectangle()
          Returns an AWT version of this rectangle.
 boolean equals(java.lang.Object obj)
          Checks whether two rectangles are equal.
 int getHeight()
          Returns height
 Point getLocation()
          Returns the upper-left corner of the rectangle.
 int getWidth()
          Returns width.
 int getX()
          Returns X coordinate.
 int getY()
          Returns Y coordinate.
 int hashCode()
          Returns the hashcode for this rectangle
 Rectangle intersection(Rectangle r)
          Computes the intersection of this Rect with the specified Rect.
 boolean intersects(Rectangle r)
          Determines whether or not this Rectangle and the specified Rectangle intersect.
 void setBounds(int x, int y, int width, int height)
          Reshapes the rectangle.
 java.lang.String toString()
          Prints a formatted version of the rectangle.
 void translate(int dx, int dy)
          Translates this rectangle the indicated distance to the right (x) and down (y).
 Rectangle union(Rectangle r)
          Computes the union of this Rectangle with the specified Rectangle.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public int x

y

public int y

width

public int width

height

public int height
Constructor Detail

Rectangle

public Rectangle()
Constructs a new Rectangle


Rectangle

public Rectangle(Rectangle r)
Constructs a new Rectangle


Rectangle

public Rectangle(int x,
                 int y,
                 int width,
                 int height)
Constructs a new Rectangle

Parameters:
x - x coordinate
y - y coordinate
width - the width of the Rectangle
height - the height of the Rectangle

Rectangle

public Rectangle(Point p,
                 int width,
                 int height)
Constructs a new Rectangle

Parameters:
p - the (x, y) coordinates
width - the width of the Rectangle
height - the height of the Rectangle

Rectangle

public Rectangle(int width,
                 int height)
Constructs a new Rectangle


Rectangle

public Rectangle(java.awt.Rectangle r)
Constructs a new Rectangle from an AWT rectangle.

Method Detail

getY

public int getY()
Returns Y coordinate.


getX

public int getX()
Returns X coordinate.


getWidth

public int getWidth()
Returns width.


getHeight

public int getHeight()
Returns height


getLocation

public Point getLocation()
Returns the upper-left corner of the rectangle.


setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Reshapes the rectangle.


translate

public void translate(int dx,
                      int dy)
Translates this rectangle the indicated distance to the right (x) and down (y).

Parameters:
dx - the distance to move this Rectangle along the x axis
dy - the distance to move this Rectangle along the y axis

contains

public boolean contains(Point p)
Returns true if the rectangle contains the specified point.


contains

public boolean contains(int x,
                        int y)
Returns true if the rectangle contains the specified point.


contains

public boolean contains(int X,
                        int Y,
                        int W,
                        int H)
Checks whether this Rectangle entirely contains the Rectangle at the specified location (XY) with the specified dimensions (WH).

Parameters:
X - x coordinate
Y - y coordinate
W - the width of the Rectangle
H - the height of the Rectangle
Returns:
true if the Rectangle specified by (XYWH) is entirely enclosed inside this Rectangle; false otherwise. (taken from JDK 1.3)

contains

public boolean contains(Rectangle r)
Checks whether this Rectangle entirely contains the Rectangle at the specified location (XY) with the specified dimensions (WH).

Parameters:
r - the rectangle to check
Returns:
true if the Rectangle specified by (XYWH) is entirely enclosed inside this Rectangle; false otherwise.

intersects

public boolean intersects(Rectangle r)
Determines whether or not this Rectangle and the specified Rectangle intersect. Two rectangles intersect if their intersection is nonempty.

Parameters:
r - the specified Rectangle
Returns:
true if the specified Rectangle and this Rectangle insersect; false otherwise.

intersection

public Rectangle intersection(Rectangle r)
Computes the intersection of this Rect with the specified Rect. Returns a new Rect that represents the intersection of the two rectangles.

Parameters:
r - the specified Rect
Returns:
the largest Rect contained in both the specified Rect and in thisRect.

union

public Rectangle union(Rectangle r)
Computes the union of this Rectangle with the specified Rectangle. Returns a new Rectangle that represents the union of the two rectangles

Parameters:
r - the specified Rectangle
Returns:
the smallest Rectangle containing both the specified Rectangle and this Rectangle.

equals

public boolean equals(java.lang.Object obj)
Checks whether two rectangles are equal.

The result is true if and only if the argument is not null and is a Rectangle object that has the same top-left corner, width, and height as this Rectangle.

Parameters:
obj - the Object to compare with this Rectangle
Returns:
true if the objects are equal; false otherwise.

toString

public java.lang.String toString()
Prints a formatted version of the rectangle.


hashCode

public int hashCode()
Returns the hashcode for this rectangle

Returns:
the hashcode for this rectangle (took from JDK 1.3 Rectangle2D)

createAWTRectangle

public java.awt.Rectangle createAWTRectangle()
Returns an AWT version of this rectangle.


add

public void add(int newx,
                int newy)
Adds a point, specified by the integer arguments newx and newy, to this Rectangle. The resulting Rectangle is the smallest Rectangle that contains both the original Rectangle and the specified point.

After adding a point, a call to contains with the added point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore, if the added point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that point.

Parameters:
newx - new Point's x coordinate
newy - new Point's y coordinate

add

public void add(Point pt)
Adds the specified Point to this Rectangle. The resulting Rectangle is the smallest Rectangle that contains both the original Rectangle and the specified Point.

After adding a Point, a call to contains with the added Point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore if the added Point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that Point.

Parameters:
pt - the new Point to add to this Rectangle

add

public void add(Rectangle r)
Adds a Rectangle to this Rectangle. The resulting Rectangle is the union of the two rectangles.

Parameters:
r - the specified Rectangle