📄 rectangle.java
字号:
/* * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com) * * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com * * J2ME version of java.awt.geom.Rectangle. *///#ifndef j2sepackage org.awt;import org.awt.geom.Rectangle2D;/** * A <code>Rectangle</code> specifies an area in a coordinate space that is * enclosed by the <code>Rectangle</code> object's top-left point * (<i>x</i>, <i>y</i>) * in the coordinate space, its width, and its height. * <p> * A <code>Rectangle</code> object's <code>width</code> and * <code>height</code> are <code>public</code> fields. The constructors * that create a <code>Rectangle</code>, and the methods that can modify * one, do not prevent setting a negative value for width or height. * <p> * A <code>Rectangle</code> whose width or height is negative is considered * empty. If the <code>Rectangle</code> is empty, then the * <code>isEmpty</code> method returns <code>true</code>. No point can be * contained by or inside an empty <code>Rectangle</code>. The * values of <code>width</code> and <code>height</code>, however, are still * valid. An empty <code>Rectangle</code> still has a location in the * coordinate space, and methods that change its size or location remain * valid. The behavior of methods that operate on more than one * <code>Rectangle</code> is undefined if any of the participating * <code>Rectangle</code> objects has a negative * <code>width</code> or <code>height</code>. These methods include * <code>intersects</code>, <code>intersection</code>, and * <code>union</code>. */public class Rectangle extends Rectangle2D /*implements Shape, java.io.Serializable*/ { /** * The <i>x</i> coordinate of the <code>Rectangle</code>. * * @serial * @see #setLocation(int, int) * @see #getLocation() */ public int x; /** * The <i>y</i> coordinate of the <code>Rectangle</code>. * * @serial * @see #setLocation(int, int) * @see #getLocation() */ public int y; /** * The width of the <code>Rectangle</code>. * @serial * @see #setSize(int, int) * @see #getSize() */ public int width; /** * The height of the <code>Rectangle</code>. * * @serial * @see #setSize(int, int) * @see #getSize() */ public int height; /** * Constructs a new <code>Rectangle</code> whose top-left corner * is at (0, 0) in the coordinate space, and whose width and * height are both zero. */ public Rectangle() { this(0, 0, 0, 0); } /** * Constructs a new <code>Rectangle</code>, initialized to match * the values of the specified <code>Rectangle</code>. * @param r the <code>Rectangle</code> from which to copy initial values * to a newly constructed <code>Rectangle</code> */ public Rectangle(Rectangle r) { this(r.x, r.y, r.width, r.height); } /** * Constructs a new <code>Rectangle</code> whose top-left corner is * specified as * (<code>x</code>, <code>y</code>) and whose width and height * are specified by the arguments of the same name. * @param x the specified x coordinate * @param y the specified y coordinate * @param width the width of the <code>Rectangle</code> * @param height the height of the <code>Rectangle</code> */ public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } /** * Constructs a new <code>Rectangle</code> whose top-left corner * is at (0, 0) in the coordinate space, and whose width and * height are specified by the arguments of the same name. * @param width the width of the <code>Rectangle</code> * @param height the height of the <code>Rectangle</code> */ public Rectangle(int width, int height) { this(0, 0, width, height); } /** * Constructs a new <code>Rectangle</code> whose top-left corner is * specified by the {@link Point} argument, and * whose width and height are specified by the * {@link Dimension} argument. * @param p a <code>Point</code> that is the top-left corner of * the <code>Rectangle</code> * @param d a <code>Dimension</code>, representing the * width and height of the <code>Rectangle</code> */ public Rectangle(Point p, Dimension d) { this(p.x, p.y, d.width, d.height); } /** * Constructs a new <code>Rectangle</code> whose top-left corner is the * specified <code>Point</code>, and whose width and height are both zero. * @param p a <code>Point</code> that is the top left corner * of the <code>Rectangle</code> */ public Rectangle(Point p) { this(p.x, p.y, 0, 0); } /** * Constructs a new <code>Rectangle</code> whose top left corner is * (0, 0) and whose width and height are specified * by the <code>Dimension</code> argument. * @param d a <code>Dimension</code>, specifying width and height */ public Rectangle(Dimension d) { this(0, 0, d.width, d.height); }//#ifdef notdef public Object clone() /*throws CloneNotSupportedException*/ { return new Rectangle(x,y,width,height); }//#endif /** * Returns the X coordinate of the bounding <code>Rectangle</code> in * <code>double</code> precision. * @return the x coordinate of the bounding <code>Rectangle</code>. */ public double getX() { return x; } /** * Returns the Y coordinate of the bounding <code>Rectangle</code> in * <code>double</code> precision. * @return the y coordinate of the bounding <code>Rectangle</code>. */ public double getY() { return y; } /** * Returns the width of the bounding <code>Rectangle</code> in * <code>double</code> precision. * @return the width of the bounding <code>Rectangle</code>. */ public double getWidth() { return width; } /** * Returns the height of the bounding <code>Rectangle</code> in * <code>double</code> precision. * @return the height of the bounding <code>Rectangle</code>. */ public double getHeight() { return height; } /** * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>. * <p> * This method is included for completeness, to parallel the * <code>getBounds</code> method of * {@link Component}. * @return a new <code>Rectangle</code>, equal to the * bounding <code>Rectangle</code> for this <code>Rectangle</code>. * @see java.awt.Component#getBounds * @see #setBounds(Rectangle) * @see #setBounds(int, int, int, int) */ public Rectangle getBounds() { return new Rectangle(x, y, width, height); } /** * Return the high precision bounding box of this rectangle. */ public Rectangle2D getBounds2D() { return new Rectangle(x, y, width, height); } /** * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code> * to match the specified <code>Rectangle</code>. * <p> * This method is included for completeness, to parallel the * <code>setBounds</code> method of <code>Component</code>. * @param r the specified <code>Rectangle</code> * @see #getBounds * @see java.awt.Component#setBounds(java.awt.Rectangle) */ public void setBounds(Rectangle r) { setBounds(r.x, r.y, r.width, r.height); } /** * Sets the bounding <code>Rectangle</code> of this * <code>Rectangle</code> to the specified * <code>x</code>, <code>y</code>, <code>width</code>, * and <code>height</code>. * <p> * This method is included for completeness, to parallel the * <code>setBounds</code> method of <code>Component</code>. * @param x the new x coordinate for the top-left * corner of this <code>Rectangle</code> * @param y the new y coordinate for the top-left * corner of this <code>Rectangle</code> * @param width the new width for this <code>Rectangle</code> * @param height the new height for this <code>Rectangle</code> * @see #getBounds * @see java.awt.Component#setBounds(int, int, int, int) */ public void setBounds(int x, int y, int width, int height) { reshape(x, y, width, height); } /** * Sets the bounds of this <code>Rectangle</code> to the specified * <code>x</code>, <code>y</code>, <code>width</code>, * and <code>height</code>. * This method is included for completeness, to parallel the * <code>setBounds</code> method of <code>Component</code>. * @param x the x coordinate of the upper-left corner of * the specified rectangle * @param y the y coordinate of the upper-left corner of * the specified rectangle * @param width the new width for the <code>Dimension</code> object * @param height the new height for the <code>Dimension</code> object */ public void setRect(double x, double y, double width, double height) { int x0 = (int) Math.floor(x); int y0 = (int) Math.floor(y); int x1 = (int) Math.ceil(x+width); int y1 = (int) Math.ceil(y+height); setBounds(x0, y0, x1-x0, y1-y0); } /** * Sets the bounding <code>Rectangle</code> of this * <code>Rectangle</code> to the specified * <code>x</code>, <code>y</code>, <code>width</code>, * and <code>height</code>. * <p> * @param x the new x coordinate for the top-left * corner of this <code>Rectangle</code> * @param y the new y coordinate for the top-left * corner of this <code>Rectangle</code> * @param width the new width for this <code>Rectangle</code> * @param height the new height for this <code>Rectangle</code> * @deprecated As of JDK version 1.1, * replaced by <code>setBounds(int, int, int, int)</code>. */ public void reshape(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } /** * Returns the location of this <code>Rectangle</code>. * <p> * This method is included for completeness, to parallel the * <code>getLocation</code> method of <code>Component</code>. * @return the <code>Point</code> that is the top-left corner of * this <code>Rectangle</code>. * @see java.awt.Component#getLocation * @see #setLocation(Point) * @see #setLocation(int, int) */ public Point getLocation() { return new Point(x, y); } /** * Moves this <code>Rectangle</code> to the specified location. * <p> * This method is included for completeness, to parallel the * <code>setLocation</code> method of <code>Component</code>. * @param p the <code>Point</code> specifying the new location * for this <code>Rectangle</code> * @see java.awt.Component#setLocation(java.awt.Point) * @see #getLocation */ public void setLocation(Point p) { setLocation(p.x, p.y); } /** * Moves this <code>Rectangle</code> to the specified location. * <p> * This method is included for completeness, to parallel the * <code>setLocation</code> method of <code>Component</code>. * @param x the x coordinate of the new location * @param y the y coordinate of the new location * @see #getLocation * @see java.awt.Component#setLocation(int, int) */ public void setLocation(int x, int y) { move(x, y); } /** * Moves this <code>Rectangle</code> to the specified location. * <p> * @param x the x coordinate of the new location * @param y the y coordinate of the new location * @deprecated As of JDK version 1.1, * replaced by <code>setLocation(int, int)</code>. */ public void move(int x, int y) { this.x = x; this.y = y; } /** * Translates this <code>Rectangle</code> the indicated distance, * to the right along the x coordinate axis, and * downward along the y coordinate axis. * @param x the distance to move this <code>Rectangle</code> * along the x axis * @param y the distance to move this <code>Rectangle</code> * along the y axis * @see java.awt.Rectangle#setLocation(int, int) * @see java.awt.Rectangle#setLocation(java.awt.Point) */ public void translate(int x, int y) { this.x += x; this.y += y; } /** * Gets the size of this <code>Rectangle</code>, represented by * the returned <code>Dimension</code>. * <p> * This method is included for completeness, to parallel the * <code>getSize</code> method of <code>Component</code>. * @return a <code>Dimension</code>, representing the size of * this <code>Rectangle</code>. * @see java.awt.Component#getSize * @see #setSize(Dimension) * @see #setSize(int, int) */ public Dimension getSize() { return new Dimension(width, height); } /** * Sets the size of this <code>Rectangle</code> to match the * specified <code>Dimension</code>. * <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 <code>Dimension</code> object * @see java.awt.Component#setSize(java.awt.Dimension) * @see #getSize */ public void setSize(Dimension d) { setSize(d.width, d.height); } /** * Sets the size of this <code>Rectangle</code> 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 <code>Rectangle</code> * @param height the new height for this <code>Rectangle</code> * @see java.awt.Component#setSize(int, int) * @see #getSize */ public void setSize(int width, int height) { resize(width, height); } /** * Sets the size of this <code>Rectangle</code> to the specified * width and height. * <p> * @param width the new width for this <code>Rectangle</code> * @param height the new height for this <code>Rectangle</code> * @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; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -