FileDocCategorySizeDatePackage
Rectangle.javaAPI DocAndroid 1.5 API22457Wed May 06 22:41:54 BST 2009java.awt

Rectangle

public class Rectangle extends Rectangle2D implements Serializable, Shape
The 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.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
The Constant serialVersionUID.
public int
x
The X coordinate of the rectangle's left upper corner.
public int
y
The Y coordinate of the rectangle's left upper corner.
public int
width
The width of rectangle.
public int
height
The 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.

param
p the Point specifies the upper left corner coordinates of the rectangle.

        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).

param
p the point specifies the upper left corner coordinates of the rectangle.
param
d the dimension specifies the width and the height of the rectangle.

        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.

param
x the X upper left corner coordinate of the rectangle.
param
y the Y upper left corner coordinate of the rectangle.
param
width the width of rectangle.
param
height the height of rectangle.

        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.

param
width the width of rectangle.
param
height the height of rectangle.

        setBounds(0, 0, width, height);
    
public Rectangle(Rectangle r)
Instantiates a new rectangle with the same coordinates as the given source rectangle.

param
r the Rectangle object which parameters will be used for instantiating a new Rectangle.

        setBounds(r.x, r.y, r.width, r.height);
    
Methods Summary
public voidadd(int px, int py)
Enlarges the rectangle to cover the specified point.

param
px the X coordinate of the new point to be covered by the rectangle.
param
py the Y coordinate of the new point to be covered by the rectangle.

        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 voidadd(java.awt.Point p)
Enlarges the rectangle to cover the specified point with the new point given as a Point object.

param
p the Point object that specifies the new point to be covered by the rectangle.

        add(p.x, p.y);
    
public voidadd(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.

param
r the Rectangle which is added to the 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 booleancontains(int px, int py)
Determines whether or not the point with specified coordinates [px, py] is within the bounds of the rectangle.

param
px the X coordinate of point.
param
py the Y coordinate of point.
return
true, if the point with specified coordinates [px, py] is within the bounds of the rectangle, false otherwise.

        if (isEmpty()) {
            return false;
        }
        if (px < x || py < y) {
            return false;
        }
        px -= x;
        py -= y;
        return px < width && py < height;
    
public booleancontains(java.awt.Point p)
Determines whether or not the point given as a Point object is within the bounds of the rectangle.

param
p the Point object
return
true, if the point p is within the bounds of the rectangle, otherwise false.

        return contains(p.x, p.y);
    
public booleancontains(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.

param
rx the X coordinate of the rectangle to compare.
param
ry the Y coordinate of the rectangle to compare.
param
rw the width of the rectangle to compare.
param
rh the height of the rectangle to compare.
return
true, if a rectangle with [rx, ry, rw, rh] parameters is entirely contained in the original rectangle, false otherwise.

        return contains(rx, ry) && contains(rx + rw - 1, ry + rh - 1);
    
public booleancontains(java.awt.Rectangle r)
Compares whether or not the rectangle specified by the Rectangle object is located inside the original rectangle.

param
r the Rectangle object.
return
true, if the rectangle specified by Rectangle object is entirely contained in the original rectangle, false otherwise.

        return contains(r.x, r.y, r.width, r.height);
    
public java.awt.geom.Rectangle2DcreateIntersection(java.awt.geom.Rectangle2D r)
Returns the intersection of the original rectangle with the specified Rectangle2D.

param
r the Rectangle2D object.
return
the Rectangle2D object that is the result of intersecting the original rectangle with the specified Rectangle2D.
see
java.awt.geom.Rectangle2D#createIntersection(java.awt.geom.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.Rectangle2DcreateUnion(java.awt.geom.Rectangle2D r)
Enlarges the rectangle to cover the specified Rectangle2D.

param
r the Rectangle2D object.
return
the union of the original and the specified Rectangle2D.
see
java.awt.geom.Rectangle2D#createUnion(java.awt.geom.Rectangle2D)

        if (r instanceof Rectangle) {
            return union((Rectangle)r);
        }
        Rectangle2D dst = new Rectangle2D.Double();
        Rectangle2D.union(this, r, dst);
        return dst;
    
public booleanequals(java.lang.Object obj)
Compares the original Rectangle with the specified object.

param
obj the specified Object for comparison.
return
true, if the specified Object is a rectangle with the same dimensions as the original rectangle, false otherwise.
see
java.awt.geom.Rectangle2D#equals(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.RectanglegetBounds()
Gets bounds of the rectangle as a new Rectangle object.

return
the Rectangle object with the same bounds as the original rectangle.
see
java.awt.geom.RectangularShape#getBounds()

        return new Rectangle(x, y, width, height);
    
public java.awt.geom.Rectangle2DgetBounds2D()
Gets the bounds of the original rectangle as a Rectangle2D object.

return
the Rectangle2D object which represents the bounds of the original rectangle.
see
java.awt.geom.Rectangle2D#getBounds2D()

        return getBounds();
    
public doublegetHeight()
Gets the height of the rectangle as a double.

return
the height of the rectangle as a double.
see
java.awt.geom.RectangularShape#getHeight()

        return height;
    
public java.awt.PointgetLocation()
Gets the location of a rectangle's upper left corner as a Point object.

return
the Point object with coordinates equal to the upper left corner of the rectangle.

        return new Point(x, y);
    
public java.awt.DimensiongetSize()
Gets the size of a Rectangle as Dimension object.

return
a Dimension object which represents size of the rectangle.

        return new Dimension(width, height);
    
public doublegetWidth()
Gets the width of the rectangle as a double.

return
the width of the rectangle as a double.
see
java.awt.geom.RectangularShape#getWidth()

        return width;
    
public doublegetX()
Gets the X coordinate of bound as a double.

return
the X coordinate of bound as a double.
see
java.awt.geom.RectangularShape#getX()

        return x;
    
public doublegetY()
Gets the Y coordinate of bound as a double.

return
the Y coordinate of bound as a double.
see
java.awt.geom.RectangularShape#getY()

        return y;
    
public voidgrow(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.

param
dx the horizontal distance to move each corner coordinate.
param
dy the vertical distance to move each corner coordinate.

        x -= dx;
        y -= dy;
        width += dx + dx;
        height += dy + dy;
    
public booleaninside(int px, int py)
Compares whether or not a point with specified coordinates [px, py] belongs to a rectangle.

param
px the X coordinate of a point.
param
py the Y coordinate of a point.
return
true, if a point with specified coordinates [px, py] belongs to a rectangle, otherwise false.
deprecated
use contains(int, int) method.

        return contains(px, py);
    
public java.awt.Rectangleintersection(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.

param
r the Rectangle object.
return
the Rectangle object is result of the original rectangle with the specified rectangle.

        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 booleanintersects(java.awt.Rectangle r)
Determines whether or not the original rectangle intersects the specified rectangle.

param
r the Rectangle object.
return
true, if the two rectangles overlap, false otherwise.

        return !intersection(r).isEmpty();
    
public booleanisEmpty()
Determines whether or not the rectangle is empty. The rectangle is empty if its width or height is negative or zero.

return
true, if the rectangle is empty, otherwise false.
see
java.awt.geom.RectangularShape#isEmpty()

        return width <= 0 || height <= 0;
    
public voidmove(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.

param
x the new X coordinate of the rectangle's upper left corner.
param
y the new Y coordinate of the rectangle's upper left corner.
deprecated
Use setLocation(int, int) method.

        setLocation(x, y);
    
public intoutcode(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.

param
px the X coordinate of the specified point.
param
py the Y coordinate of the specified point.
return
the location of the Point relative to the rectangle as the result of logical OR operation with all out masks.
see
java.awt.geom.Rectangle2D#outcode(double, double)

        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 voidreshape(int x, int y, int width, int height)
Resets the bounds of a rectangle to the specified x, y, width and height parameters.

param
x the new X coordinate of the upper left corner.
param
y the new Y coordinate of the upper left corner.
param
width the new width of rectangle.
param
height the new height of rectangle.
deprecated
use setBounds(int, int, int, int) method

        setBounds(x, y, width, height);
    
public voidresize(int width, int height)
Sets a new size for the rectangle.

param
width the rectangle's new width.
param
height the rectangle's new height.
deprecated
use the setSize(int, int) method.

        setBounds(x, y, width, height);
    
public voidsetBounds(int x, int y, int width, int height)
Sets the bounds of a rectangle to the specified x, y, width, and height parameters.

param
x the X coordinate of the upper left corner.
param
y the Y coordinate of the upper left corner.
param
width the width of rectangle.
param
height the height of rectangle.

        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
    
public voidsetBounds(java.awt.Rectangle r)
Sets the bounds of the rectangle to match the bounds of the Rectangle object sent as a parameter.

param
r the Rectangle object which specifies the new bounds.

        setBounds(r.x, r.y, r.width, r.height);
    
public voidsetLocation(int x, int y)
Sets the location of the rectangle in terms of its upper left corner coordinates X and Y.

param
x the X coordinate of the rectangle's upper left corner.
param
y the Y coordinate of the rectangle's upper left corner.

        this.x = x;
        this.y = y;
    
public voidsetLocation(java.awt.Point p)
Sets the location of a rectangle using a Point object to give the coordinates of the upper left corner.

param
p the Point object which represents the new upper left corner coordinates of rectangle.

        setLocation(p.x, p.y);
    
public voidsetRect(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.

param
x the X coordinate of the upper left corner of the double-valued rectangle to be bounded.
param
y the Y coordinate of the upper left corner of the double-valued rectangle to be bounded.
param
width the width of the rectangle to be bounded.
param
height the height of the rectangle to be bounded.
see
java.awt.geom.Rectangle2D#setRect(double, double, double, double)

        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 voidsetSize(int width, int height)
Sets the size of the Rectangle.

param
width the new width of the rectangle.
param
height the new height of the rectangle.

        this.width = width;
        this.height = height;
    
public voidsetSize(java.awt.Dimension d)
Sets the size of a Rectangle specified as Dimension object.

param
d a Dimension object which represents new size of a rectangle.

        setSize(d.width, d.height);
    
public java.lang.StringtoString()
Returns a string representation of the rectangle; the string contains [x, y, width, height] parameters of the rectangle.

return
the string representation 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 voidtranslate(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.

param
mx the horizontal translation increment.
param
my the vertical translation increment.

        x += mx;
        y += my;
    
public java.awt.Rectangleunion(java.awt.Rectangle r)
Enlarges the rectangle to cover the specified rectangle.

param
r the Rectangle.
return
the union of the original and the specified rectangle.

        Rectangle dst = new Rectangle(this);
        dst.add(r);
        return dst;