rectangle.java

来自「gcc3.2.1源代码」· Java 代码 · 共 646 行 · 第 1/2 页

JAVA
646
字号
/* Copyright (C) 1999, 2000, 2001, 2002  Free Software FoundationThis file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.awt.geom.*;import java.io.Serializable;/* Status:  Mostly complete. Some of the Java2D stuff is commented out. *//** * This class represents a rectangle and all the interesting things you * might want to do with it.  Note that the coordinate system uses * the origin (0,0) as the top left of the screen, with the x and y * values increasing as they move to the right and down respectively. * * @author Warren Levy  <warrenl@cygnus.com> * @author Aaron M. Renn (arenn@urbanophile.com) */public class Rectangle extends Rectangle2D  implements Cloneable, Shape, Serializable{  /**  * The X coordinate of the top-left corner of the rectangle.  */  public int x;  /**  * The Y coordinate of the top-left corner of the rectangle;  */  public int y;  /**  * The width of the rectangle  */  public int width;  /**  * The height of the rectangle  */  public int height;  /**   * Initializes a new instance of <code>Rectangle</code> with a top   * left corner at (0,0) and a width and height of 0.   */  public Rectangle()  {    x = 0;    y = 0;    width = 0;    height = 0;  }  /**   * Initializes a new instance of <code>Rectangle</code> from the   * coordinates of the specified rectangle.   *   * @param rect The rectangle to copy from.   */  public Rectangle(Rectangle r)  {    x = r.x;    y = r.y;    width = r.width;    height = r.height;  }  /**   * Initializes a new instance of <code>Rectangle</code> from the specified   * inputs.   *   * @param x The X coordinate of the top left corner of the rectangle.   * @param y The Y coordinate of the top left corner of the rectangle.   * @param width The width of the rectangle.   * @param height The height of the rectangle.   */  public Rectangle(int x, int y, int width, int height)  {    this.x = x;    this.y = y;    this.width = width;    this.height = height;  }  /**   * Initializes a new instance of <code>Rectangle</code> with the specified   * width and height.  The upper left corner of the rectangle will be at   * the origin (0,0).   *   * @param width The width of the rectangle.   * @param height the height of the rectange.   */  public Rectangle(int width, int height)  {    x = 0;    y = 0;    this.width = width;    this.height = height;  }  /**   * Initializes a new instance of <code>Rectangle</code> with a top-left   * corner represented by the specified point and the width and height   * represented by the specified dimension.   *   * @param point The upper left corner of the rectangle.   * @param dim The width and height of the rectangle.   */  public Rectangle(Point p, Dimension d)  {    x = p.x;    y = p.y;    width = d.width;    height = d.height;  }  /**   * Initializes a new instance of <code>Rectangle</code> with a top left   * corner at the specified point and a width and height of zero.   *   * @param poin The upper left corner of the rectangle.   */  public Rectangle(Point p)  {    x = p.x;    y = p.y;    width = 0;    height = 0;  }  /**   * Initializes a new instance of <code>Rectangle</code> with an   * upper left corner at the origin (0,0) and a width and height represented   * by the specified dimension.   *   * @param dim The width and height of the rectangle.   */  public Rectangle(Dimension d)  {    x = 0;    y = 0;    width = d.width;    height = d.height;  }  /**   * Returns the bounding rectangle for this rectangle, which is simply   * this rectange itself.   *   * @return This rectangle.   */  public Rectangle getBounds ()  {    return (Rectangle) this.clone();  }  /**   * Modifies this rectangle so that it represents the smallest rectangle    * that contains both the existing rectangle and the specified point.   *   * @param x The X coordinate of the point to add to this rectangle.   * @param y The Y coordinate of the point to add to this rectangle.   */  public void add(int newx, int newy)  {    int x = this.x > newx ? newx : this.x;    int y = this.y > newy ? newy : this.y;    width = (this.x + width > newx ? this.x + width : newx) - x;    height = (this.y + height > newy ? this.y + height : newy) - y;    this.x = x;    this.y = y;  }  /**   * Modifies this rectangle so that it represents the smallest rectangle    * that contains both the existing rectangle and the specified point.   *   * @param point The point to add to this rectangle.   */  public void add(Point pt)  {    add (pt.x, pt.y);  }  /**   * Modifies this rectangle so that it represents the smallest rectangle    * that contains both the existing rectangle and the specified rectangle.   *   * @param rect The rectangle to add to this rectangle.   */  public void add(Rectangle r)  {    int x = this.x > r.x ? r.x : this.x;    int y = this.y > r.y ? r.y : this.y;    width = (this.x + width > r.x + r.width ?              this.x + width : r.x + r.width) - x;    height = (this.y + height > r.y + r.height ?              this.y + height : r.y + r.height) - y;    this.x = x;    this.y = y;  }  /**   * Tests whether or not the specified point is inside this rectangle.   *   * @param x The X coordinate of the point to test.   * @param y The Y coordinate of the point to test.   *   * @return <code>true</code> if the point is inside the rectangle,   * <code>false</code> otherwise.   */  public boolean contains(int x, int y)  {    return (x >= this.x && x <= this.x + this.width            && y >= this.y && y <= this.y + this.height);  }     public boolean contains(int x, int y, int w, int h)  {    return (x >= this.x && x + w <= this.x + this.width            && y >= this.y && y + h <= this.y + this.height);  }  /**   * Tests whether or not the specified point is inside this rectangle.   *   * @param point The point to test.   *   * @return <code>true</code> if the point is inside the rectangle,   * <code>false</code> otherwise.   */  public boolean contains(Point p)  {    return contains(p.x, p.y);  }  public boolean contains(Rectangle r)  {    return contains(r.x, r.y, r.width, r.height);  }  /**   * Tests this rectangle for equality against the specified object.  This   * will be true if an only if the specified object:   * <p>   * <ul>   * <li>Is not <code>null</code>.   * <li>Is an instance of <code>Rectangle</code>.   * <li>Has X and Y coordinates identical to this rectangle.   * <li>Has a width and height identical to this rectangle.   * </ul>   *   * @param obj The object to test against for equality.   *   * @return <code>true</code> if the specified object is equal to this one,   * <code>false</code> otherwise.   */  public boolean equals(Object obj)  {    if (obj instanceof Rectangle)      {	Rectangle r = (Rectangle) obj;	return (r.x == x 	        && r.y == y 		&& r.width == width 		&& r.height == height);      }    return false;  }  public double getHeight()  {    return (double) this.height;       }  /**   * Returns the location of this rectangle, which is the coordinates of   * its upper left corner.   *   * @return The point where this rectangle is located.   */  public Point getLocation()  {    return new Point(x,y);  }  /**   * Returns the size of this rectangle.

⌨️ 快捷键说明

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