Rectanglepublic class Rectangle extends Rectangle2D implements Serializable, ShapeThe Rectangle class defines the rectangular area in terms of its upper left
corner coordinates [x,y], its width, and its height. A Rectangle specified by
[x, y, width, height] parameters has an outline path with corners at [x, y],
[x + width,y], [x + width,y + height], and [x, y + height].
The rectangle is empty if the width or height is negative or zero. In this
case the isEmpty method returns true. |
Fields Summary |
---|
private static final long | serialVersionUIDThe Constant serialVersionUID. | public int | xThe X coordinate of the rectangle's left upper corner. | public int | yThe Y coordinate of the rectangle's left upper corner. | public int | widthThe width of rectangle. | public int | heightThe height of rectangle. |
Constructors Summary |
---|
public Rectangle()Instantiates a new rectangle with [0, 0] upper left corner coordinates,
the width and the height are zero.
setBounds(0, 0, 0, 0);
| public Rectangle(Point p)Instantiates a new rectangle whose upper left corner coordinates are
given by the Point object (p.X and p.Y), and the width and the height are
zero.
setBounds(p.x, p.y, 0, 0);
| public Rectangle(Point p, Dimension d)Instantiates a new rectangle whose upper left corner coordinates are
given by the Point object (p.X and p.Y), and the width and the height are
given by Dimension object (d.width and d.height).
setBounds(p.x, p.y, d.width, d.height);
| public Rectangle(int x, int y, int width, int height)Instantiates a new rectangle determined by the upper left corner
coordinates (x, y), width and height.
setBounds(x, y, width, height);
| public Rectangle(int width, int height)Instantiates a new rectangle with [0, 0] as its upper left corner
coordinates and the specified width and height.
setBounds(0, 0, width, height);
| public Rectangle(Rectangle r)Instantiates a new rectangle with the same coordinates as the given
source rectangle.
setBounds(r.x, r.y, r.width, r.height);
|
Methods Summary |
---|
public void | add(int px, int py)Enlarges the rectangle to cover the specified point.
int x1 = Math.min(x, px);
int x2 = Math.max(x + width, px);
int y1 = Math.min(y, py);
int y2 = Math.max(y + height, py);
setBounds(x1, y1, x2 - x1, y2 - y1);
| public void | add(java.awt.Point p)Enlarges the rectangle to cover the specified point with the new point
given as a Point object.
add(p.x, p.y);
| public void | add(java.awt.Rectangle r)Adds a new rectangle to the original rectangle, the result is an union of
the specified specified rectangle and original rectangle.
int x1 = Math.min(x, r.x);
int x2 = Math.max(x + width, r.x + r.width);
int y1 = Math.min(y, r.y);
int y2 = Math.max(y + height, r.y + r.height);
setBounds(x1, y1, x2 - x1, y2 - y1);
| public boolean | contains(int px, int py)Determines whether or not the point with specified coordinates [px, py]
is within the bounds of the rectangle.
if (isEmpty()) {
return false;
}
if (px < x || py < y) {
return false;
}
px -= x;
py -= y;
return px < width && py < height;
| public boolean | contains(java.awt.Point p)Determines whether or not the point given as a Point object is within the
bounds of the rectangle.
return contains(p.x, p.y);
| public boolean | contains(int rx, int ry, int rw, int rh)Determines whether or not the rectangle specified by [rx, ry, rw, rh]
parameters is located inside the original rectangle.
return contains(rx, ry) && contains(rx + rw - 1, ry + rh - 1);
| public boolean | contains(java.awt.Rectangle r)Compares whether or not the rectangle specified by the Rectangle object
is located inside the original rectangle.
return contains(r.x, r.y, r.width, r.height);
| public java.awt.geom.Rectangle2D | createIntersection(java.awt.geom.Rectangle2D r)Returns the intersection of the original rectangle with the specified
Rectangle2D.
if (r instanceof Rectangle) {
return intersection((Rectangle)r);
}
Rectangle2D dst = new Rectangle2D.Double();
Rectangle2D.intersect(this, r, dst);
return dst;
| public java.awt.geom.Rectangle2D | createUnion(java.awt.geom.Rectangle2D r)Enlarges the rectangle to cover the specified Rectangle2D.
if (r instanceof Rectangle) {
return union((Rectangle)r);
}
Rectangle2D dst = new Rectangle2D.Double();
Rectangle2D.union(this, r, dst);
return dst;
| public boolean | equals(java.lang.Object obj)Compares the original Rectangle with the specified object.
if (obj == this) {
return true;
}
if (obj instanceof Rectangle) {
Rectangle r = (Rectangle)obj;
return r.x == x && r.y == y && r.width == width && r.height == height;
}
return false;
| public java.awt.Rectangle | getBounds()Gets bounds of the rectangle as a new Rectangle object.
return new Rectangle(x, y, width, height);
| public java.awt.geom.Rectangle2D | getBounds2D()Gets the bounds of the original rectangle as a Rectangle2D object.
return getBounds();
| public double | getHeight()Gets the height of the rectangle as a double.
return height;
| public java.awt.Point | getLocation()Gets the location of a rectangle's upper left corner as a Point object.
return new Point(x, y);
| public java.awt.Dimension | getSize()Gets the size of a Rectangle as Dimension object.
return new Dimension(width, height);
| public double | getWidth()Gets the width of the rectangle as a double.
return width;
| public double | getX()Gets the X coordinate of bound as a double.
return x;
| public double | getY()Gets the Y coordinate of bound as a double.
return y;
| public void | grow(int dx, int dy)Enlarges the rectangle by moving each corner outward from the center by a
distance of dx horizonally and a distance of dy vertically. Specifically,
changes a rectangle with [x, y, width, height] parameters to a rectangle
with [x-dx, y-dy, width+2*dx, height+2*dy] parameters.
x -= dx;
y -= dy;
width += dx + dx;
height += dy + dy;
| public boolean | inside(int px, int py)Compares whether or not a point with specified coordinates [px, py]
belongs to a rectangle.
return contains(px, py);
| public java.awt.Rectangle | intersection(java.awt.Rectangle r)Returns the intersection of the original rectangle with the specified
rectangle. An empty rectangle is returned if there is no intersection.
int x1 = Math.max(x, r.x);
int y1 = Math.max(y, r.y);
int x2 = Math.min(x + width, r.x + r.width);
int y2 = Math.min(y + height, r.y + r.height);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
| public boolean | intersects(java.awt.Rectangle r)Determines whether or not the original rectangle intersects the specified
rectangle.
return !intersection(r).isEmpty();
| public boolean | isEmpty()Determines whether or not the rectangle is empty. The rectangle is empty
if its width or height is negative or zero.
return width <= 0 || height <= 0;
| public void | move(int x, int y)Moves a rectangle to the new location by moving its upper left corner to
the point with coordinates X and Y.
setLocation(x, y);
| public int | outcode(double px, double py)Determines where the specified Point is located with respect to the
rectangle. This method computes whether the point is to the right or to
the left of the rectangle and whether it is above or below the rectangle,
and packs the result into an integer by using a binary OR operation with
the following masks:
- Rectangle2D.OUT_LEFT
- Rectangle2D.OUT_TOP
- Rectangle2D.OUT_RIGHT
- Rectangle2D.OUT_BOTTOM
If the rectangle is empty, all masks are set, and if the point is inside
the rectangle, none are set.
int code = 0;
if (width <= 0) {
code |= OUT_LEFT | OUT_RIGHT;
} else if (px < x) {
code |= OUT_LEFT;
} else if (px > x + width) {
code |= OUT_RIGHT;
}
if (height <= 0) {
code |= OUT_TOP | OUT_BOTTOM;
} else if (py < y) {
code |= OUT_TOP;
} else if (py > y + height) {
code |= OUT_BOTTOM;
}
return code;
| public void | reshape(int x, int y, int width, int height)Resets the bounds of a rectangle to the specified x, y, width and height
parameters.
setBounds(x, y, width, height);
| public void | resize(int width, int height)Sets a new size for the rectangle.
setBounds(x, y, width, height);
| public void | setBounds(int x, int y, int width, int height)Sets the bounds of a rectangle to the specified x, y, width, and height
parameters.
this.x = x;
this.y = y;
this.height = height;
this.width = width;
| public void | setBounds(java.awt.Rectangle r)Sets the bounds of the rectangle to match the bounds of the Rectangle
object sent as a parameter.
setBounds(r.x, r.y, r.width, r.height);
| public void | setLocation(int x, int y)Sets the location of the rectangle in terms of its upper left corner
coordinates X and Y.
this.x = x;
this.y = y;
| public void | setLocation(java.awt.Point p)Sets the location of a rectangle using a Point object to give the
coordinates of the upper left corner.
setLocation(p.x, p.y);
| public void | setRect(double x, double y, double width, double height)Sets the rectangle to be the nearest rectangle with integer coordinates
bounding the rectangle defined by the double-valued parameters.
int x1 = (int)Math.floor(x);
int y1 = (int)Math.floor(y);
int x2 = (int)Math.ceil(x + width);
int y2 = (int)Math.ceil(y + height);
setBounds(x1, y1, x2 - x1, y2 - y1);
| public void | setSize(int width, int height)Sets the size of the Rectangle.
this.width = width;
this.height = height;
| public void | setSize(java.awt.Dimension d)Sets the size of a Rectangle specified as Dimension object.
setSize(d.width, d.height);
| public java.lang.String | toString()Returns a string representation of the rectangle; the string contains [x,
y, width, height] parameters of the rectangle.
// The output format based on 1.5 release behaviour. It could be
// obtained in the following way
// System.out.println(new Rectangle().toString())
return getClass().getName() + "[x=" + x + ",y=" + y + //$NON-NLS-1$ //$NON-NLS-2$
",width=" + width + ",height=" + height + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
| public void | translate(int mx, int my)Moves a rectangle a distance of mx along the x coordinate axis and a
distance of my along y coordinate axis.
x += mx;
y += my;
| public java.awt.Rectangle | union(java.awt.Rectangle r)Enlarges the rectangle to cover the specified rectangle.
Rectangle dst = new Rectangle(this);
dst.add(r);
return dst;
|
|