⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rectangle.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	this.x += x;
	this.y += y;
    }	

    /**
     * Gets the size (width and height) of this rectangle.
     * <p>
     * This method is included for completeness, to parallel the
     * <code>getSize</code> method of <code>Component</code>.
     * @return    a dimension, representing the size.
     * @see       java.awt.Component#getSize
     * @since     JDK1.1
     */
    public Dimension getSize() {
	return new Dimension(width, height);
    }	

    /**
     * Sets the size of this rectangle to match the specified dimension.
     * <p>
     * This method is included for completeness, to parallel the
     * <code>setSize</code> method of <code>Component</code>.
     * @param d  the new size for the Dimension object
     * @see       java.awt.Component#setSize(java.awt.Dimension)
     * @since     JDK1.1
     */
    public void setSize(Dimension d) {
	setSize(d.width, d.height);
    }	

    /**
     * Sets the size of this rectangle to the specified width and height.
     * <p>
     * This method is included for completeness, to parallel the
     * <code>setSize</code> method of <code>Component</code>.
     * @param     width    the new width for this rectangle object.
     * @param     height   the new height for this rectangle object.
     * @see       java.awt.Component#setSize(int, int)
     * @since     JDK1.1
     */
    public void setSize(int width, int height) {
    	resize(width, height);
    }	

    /**
     * @deprecated As of JDK version 1.1,
     * replaced by <code>setSize(int, int)</code>.
     */
    public void resize(int width, int height) {
	this.width = width;
	this.height = height;
    }	

    /**
     * Checks whether this rectangle contains the specified point.
     * @param p the point (location) to test.
     * @return    <code>true</code> if the point 
     *            (<i>x</i>,&nbsp;<i>y</i>) is inside this rectangle; 
     *            <code>false</code> otherwise.
     * @since     JDK1.1
     */
    public boolean contains(Point p) {
	return contains(p.x, p.y);
    }

    /**
     * Checks whether this rectangle contains the point
     * at the specified location (<i>x</i>,&nbsp;<i>y</i>).
     * @param     x   the <i>x</i> coordinate.
     * @param     y   the <i>y</i> coordinate.
     * @return    <code>true</code> if the point 
     *            (<i>x</i>,&nbsp;<i>y</i>) is inside this rectangle; 
     *            <code>false</code> otherwise.
     * @since     JDK1.1
     */
    public boolean contains(int x, int y) {
	return inside(x, y);
    }

    /**
     * @deprecated As of JDK version 1.1,
     * replaced by <code>contains(int, int)</code>.
     */
    public boolean inside(int x, int y) {
	return (x >= this.x) && ((x - this.x) < this.width) && (y >= this.y) && ((y-this.y) < this.height);
    }

    /**
     * Determines whether this rectangle and the specified rectangle  
     * intersect. Two rectangles intersect if their intersection is 
     * nonempty. 
     * @param     r   a rectangle.
     * @return    <code>true</code> if the specified rectangle 
     *            and this rectangle insersect; 
     *            <code>false</code> otherwise.
     * @since     JDK1.0
     */
    public boolean intersects(Rectangle r) {
	return !((r.x + r.width <= x) ||
		 (r.y + r.height <= y) ||
		 (r.x >= x + width) ||
		 (r.y >= y + height));
    }

    /**
     * Computes the intersection of this rectangle with the 
     * specified rectangle. Returns a new rectangle that 
     * represents the intersection of the two rectangles.
     * @param     r   a rectangle.
     * @return    the largest rectangle contained in both the 
     *            specified rectangle and in this rectangle.
     * @since   JDK1.0
     */
    public Rectangle intersection(Rectangle r) {
	int x1 = Math.max(x, r.x);
	int x2 = Math.min(x + width, r.x + r.width);
	int y1 = Math.max(y, r.y);
	int y2 = Math.min(y + height, r.y + r.height);
	return new Rectangle(x1, y1, x2 - x1, y2 - y1);
    }

    /**
     * Computes the union of this rectangle with the 
     * specified rectangle. Returns a new rectangle that 
     * represents the union of the two rectangles.
     * @param     r   a rectangle.
     * @return    the smallest rectangle containing both the specified 
     *            rectangle and this rectangle.
     * @since     JDK1.0
     */
    public Rectangle union(Rectangle r) {
	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);
	return new Rectangle(x1, y1, x2 - x1, y2 - y1);
    }

    /**
     * Adds a point, specified by the integer arguments <code>newx</code>
     * and <code>newy</code>, to this rectangle. The resulting rectangle is
     * the smallest rectangle that contains both the original rectangle 
     * and the specified point.
     * <p>
     * After adding a point, a call to <code>contains<code> with the 
     * added point as an argument will not necessarily return 
     * <code>true</code>. The <code>contains</code> method does not 
     * return <code>true</code> for points on the right or bottom 
     * edges of a rectangle. Therefore if the added point falls on 
     * the left or bottom edge of the enlarged rectangle, 
     * <code>contains</code> will return <code>false</code> for that point.
     * 
     * @param     newx   the <i>x</i> coordinate of the new point.
     * @param     newy   the <i>y</i> coordinate of the new point.
     * @since     JDK1.0
     */
    public void add(int newx, int newy) {
	int x1 = Math.min(x, newx);
	int x2 = Math.max(x + width, newx);
	int y1 = Math.min(y, newy);
	int y2 = Math.max(y + height, newy);
	x = x1;
	y = y1;
	width = x2 - x1;
	height = y2 - y1;
    }

    /**
     * Adds the point <code>pt</code> to this rectangle. The resulting 
     * rectangle is the smallest rectangle that contains both the 
     * original rectangle and the specified point.
     * <p>
     * After adding a point, a call to <code>contains<code> with the 
     * added point as an argument will not necessarily return 
     * <code>true</code>. The <code>contains</code> method does not 
     * return <code>true</code> for points on the right or bottom 
     * edges of a rectangle. Therefore if the added point falls on 
     * the left or bottom edge of the enlarged rectangle, 
     * <code>contains</code> will return <code>false</code> for that point.
     * 
     * @param     pt the new point to add to the rectangle.
     * @since     JDK1.0
     */
    public void add(Point pt) {
	add(pt.x, pt.y);
    }

    /**
     * Adds a rectangle to this rectangle. The resulting rectangle is
     * the union of the two rectangles. 
     * @param     a rectangle.
     * @since     JDK1.0
     */
    public void add(Rectangle r) {
	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);
	x = x1;
	y = y1;
	width = x2 - x1;
	height = y2 - y1;
    }

    /**
     * Grows the rectangle both horizontally and vertically.
     * <p>
     * This method modifies the rectangle so that it is 
     * <code>h</code> units larger on both the left and right side, 
     * and <code>v</code> units larger at both the top and bottom. 
     * <p>
     * The new rectangle has (<code>x&nbsp;-&nbsp;h</code>, 
     * <code>y&nbsp;-&nbsp;v</code>) as its top-left corner, a 
     * width of 
     * <code>width</code>&nbsp;<code>+</code>&nbsp;<code>2h</code>, 
     * and a height of 
     * <code>height</code>&nbsp;<code>+</code>&nbsp;<code>2v</code>. 
     * <p>
     * If negative values are supplied for <code>h</code> and 
     * <code>v</code>, the size of the rectangle decreases accordingly. 
     * The <code>grow</code> method does not check whether the resulting 
     * values of <code>width</code> and <code>height</code> are 
     * non-negative. 
     * @param     h   the horizontal expansion.
     * @param     v   the vertical expansion.
     * @since     JDK1.0
     */
    public void grow(int h, int v) {
	x -= h;
	y -= v;
	width += h * 2;
	height += v * 2;
    }

    /**
     * Determines whether this rectangle is empty. A rectangle is empty if 
     * its width or its height is less than or equal to zero. 
     * @return     <code>true</code> if this rectangle is empty; 
     *             <code>false</code> otherwise.
     * @since      JDK1.0
     */
    public boolean isEmpty() {
	return (width <= 0) || (height <= 0);
    }

    /**
     * Returns the hashcode for this rectangle.
     * @return     the hashcode for this rectangle.
     * @since      JDK1.0
     */
    public int hashCode() {
	return x ^ (y*37) ^ (width*43) ^ (height*47);
    }

    /**
     * Checks whether two rectangles are equal.
     * <p>
     * The result is <kbd>true</kbd> if and only if the argument is not 
     * <kbd>null</kbd> and is a <kbd>Rectangle</kbd> object that has the 
     * same top-left corner, width, and height as this rectangle. 
     * @param     obj   the object to compare with.
     * @return    <code>true</code> if the objects are equal; 
     *            <code>false</code> otherwise.
     * @since     JDK1.0
     */
    public boolean equals(Object obj) {
	if (obj instanceof Rectangle) {
	    Rectangle r = (Rectangle)obj;
	    return (x == r.x) && (y == r.y) && (width == r.width) && (height == r.height);
	}
	return false;
    }

    /**
     * Returns a string representation of this rectangle 
     * and its values.
     * @return     a string representation of this rectangle.
     * @since      JDK1.0
     */
    public String toString() {
	return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -