maclib
Class Rect

java.lang.Object
  extended by maclib.Rect

public class Rect
extends java.lang.Object

This class implements QuickDraw-like definitions for rectangles. Implements also object methods for these rectangles.

Version:
28 may 2001
Author:
Philippe Chassignet, Ecole Polytechnique
See Also:
MacLib, Rectangle

Field Summary
 short bottom
          The y coordinate of the bottom right corner.
 short left
          The x coordinate of the top left corner.
 short right
          The x coordinate of the bottom right corner.
 short top
          The y coordinate of the top left corner.
 
Constructor Summary
Rect()
          Constructs and initializes an empty rect with coordinates (0,0,0,0).
Rect(int left, int top, int right, int bottom)
          Constructs and initializes a rectangle with the given coordinates.
Rect(Point p1, Point p2)
          Constructs and initializes a rectangle as the smallest one which encloses the two given points.
Rect(Rect src1, Rect src2)
          Constructs and initializes a rectangle as the smallest one which encloses the two given rectangles.
 
Method Summary
 Point botRight()
          Returns the bottom right corner of this rectangle.
 boolean emptyRect()
          Determines whether this rectangle is empty or not.
 boolean equalRect(Rect r)
          Determines whether this rectangle and the given rectangle have the same coordinates.
 java.awt.Rectangle getRectangle()
          Converts this rectangle as a java.awt.Rectangle object.
 int hashCode()
          Returns a hash code for this rectangle.
 Rect insetRect(int dh, int dv)
          Shrinks or expands this rectangle by the specified amounts.
 Rect offsetRect(int dh, int dv)
          Translates this rectangle by the specified displacement.
 Rect pt2Rect(Point p1, Point p2)
          Sets the coordinates of this rectangle to the smallest one which encloses the two given points.
 boolean ptInRect(Point pt)
          Determines whether the pixel below and to the right of the given point is enclosed in this rectangle.
 boolean sectRect(Rect src1, Rect src2)
          Sets the coordinates of this rectangle to the rectangle that is the intersection of the two given rectangles.
 Rect setRect(int left, int top, int right, int bottom)
          Sets the coordinates of this rectangle as specified.
 Rect setRect(Rect src)
          Sets the coordinates of this rectangle as specified by the given Rect object.
 Rect setRect(java.awt.Rectangle rec)
          Sets the coordinates of this rectangle as specified by the given java.awt.Rectangle object.
 Point topLeft()
          Returns the top left corner of this rectangle.
 java.lang.String toString()
          Returns a string representation for the location of this rectangle.
 Rect unionRect(Rect src1, Rect src2)
          Sets the coordinates of this rectangle to the smallest one which encloses the two given rectangles.
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

left

public short left
The x coordinate of the top left corner.


top

public short top
The y coordinate of the top left corner.


right

public short right
The x coordinate of the bottom right corner.


bottom

public short bottom
The y coordinate of the bottom right corner.

Constructor Detail

Rect

public Rect()
Constructs and initializes an empty rect with coordinates (0,0,0,0).


Rect

public Rect(int left,
            int top,
            int right,
            int bottom)
Constructs and initializes a rectangle with the given coordinates.

Parameters:
left - the left coordinate of the new rectangle.
top - the top coordinate of the new rectangle.
right - the right coordinate of the new rectangle.
bottom - the bottom coordinate of the new rectangle.

Rect

public Rect(Point p1,
            Point p2)
Constructs and initializes a rectangle as the smallest one which encloses the two given points.

Parameters:
p1 - the first Point object used to specify the coordinates.
p2 - the second Point object used to specify the coordinates.

Rect

public Rect(Rect src1,
            Rect src2)
Constructs and initializes a rectangle as the smallest one which encloses the two given rectangles.

Parameters:
src1 - the first Rect object used to specify the coordinates.
src2 - the second Rect object used to specify the coordinates.
Method Detail

topLeft

public Point topLeft()
Returns the top left corner of this rectangle.

Returns:
a new Point object located at the top left corner.

botRight

public Point botRight()
Returns the bottom right corner of this rectangle.

Returns:
a new Point object located at the bottom right corner.

getRectangle

public java.awt.Rectangle getRectangle()
Converts this rectangle as a java.awt.Rectangle object.

Returns:
a new Rectangle object.
See Also:
Rectangle

setRect

public Rect setRect(int left,
                    int top,
                    int right,
                    int bottom)
Sets the coordinates of this rectangle as specified.

Parameters:
left - the new left coordinate.
top - the new top coordinate.
right - the new right coordinate.
bottom - the new bottom coordinate.

setRect

public Rect setRect(Rect src)
Sets the coordinates of this rectangle as specified by the given Rect object.

Parameters:
src - the Rect object used to specify the coordinates.

setRect

public Rect setRect(java.awt.Rectangle rec)
Sets the coordinates of this rectangle as specified by the given java.awt.Rectangle object.

Parameters:
rec - the Rectangle object used to specify the coordinates.
See Also:
Rectangle

pt2Rect

public Rect pt2Rect(Point p1,
                    Point p2)
Sets the coordinates of this rectangle to the smallest one which encloses the two given points.

Parameters:
p1 - the first Point object used to specify the coordinates.
p2 - the second Point object used to specify the coordinates.

offsetRect

public Rect offsetRect(int dh,
                       int dv)
Translates this rectangle by the specified displacement. Adds dh to each horizontal coordinate and dv to each vertical coordinate.

Parameters:
dh - the horizontal displacement.
dv - the vertical displacement.

insetRect

public Rect insetRect(int dh,
                      int dv)
Shrinks or expands this rectangle by the specified amounts. Adds dh to the left coordinate and subtracts dh from the right coordinate. Adds dv to the top coordinate and subtracts dv from the bottom coordinate.

Parameters:
dh - the horizontal displacement.
dv - the vertical displacement.

unionRect

public Rect unionRect(Rect src1,
                      Rect src2)
Sets the coordinates of this rectangle to the smallest one which encloses the two given rectangles.

Parameters:
src1 - the first Rect object used to specify the coordinates.
src2 - the second Rect object used to specify the coordinates.

equalRect

public boolean equalRect(Rect r)
Determines whether this rectangle and the given rectangle have the same coordinates.

Parameters:
r - the Rect object to compare with.
Returns:
true if both rectangles have same coordinates; false otherwise.

sectRect

public boolean sectRect(Rect src1,
                        Rect src2)
Sets the coordinates of this rectangle to the rectangle that is the intersection of the two given rectangles. Determines also whether they indeed intersect. Rectangles that "touch" at a line or a point are not considered intersecting. If the rectangles do not intersect, this rectangle is set to (0,0,0,0).

Parameters:
src1 - the first Rect object used to specify the coordinates.
src2 - the second Rect object used to specify the coordinates.
Returns:
true if the two rectangles really intersect; false otherwise.

ptInRect

public boolean ptInRect(Point pt)
Determines whether the pixel below and to the right of the given point is enclosed in this rectangle.

Parameters:
pt - the Point object to check with.
Returns:
true if so; false otherwise.
See Also:
Point.inRect(Rect)

emptyRect

public boolean emptyRect()
Determines whether this rectangle is empty or not. A rectangle is considered empty if the bottom coordinate is equal to or less than the top or the right coordinate is equal to or less than the left.

Returns:
true if so; false otherwise.

toString

public java.lang.String toString()
Returns a string representation for the location of this rectangle.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of the coordinates.

hashCode

public int hashCode()
Returns a hash code for this rectangle.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code build up from the coordinates.