📄 rectangle.java
字号:
/*
* @(#)Rectangle.java 1.27 97/07/08
*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
* CopyrightVersion 1.1_beta
*
*/
package java.awt;
/**
* A rectangle specifies an area in a coordinate space that is
* defined by the rectangle's top-left point (<i>x</i>, <i>y</i>)
* in the coordinate space, its width, and its height.
* <p>
* A rectangle's <code>width</code> and <code>height</code> are
* public fields. The constructors that allow you to create a
* rectangle, and the methods that allow you to modify one, do not
* prevent you from setting a negative value for width or height.
* <p>
* A rectangle whose width or height is negative is considered
* empty, and all methods defined by the <code>Rectangle</code> class
* behave accordingly. If the rectangle is empty, then the method
* <code>isEmpty</code> returns <code>true</code>. No point can be
* contained by or inside an empty rectangle, however the values of
* <code>width</code> and <code>height</code> are still valid. An
* empty rectangle 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 rectangle is
* undefined if any of the participating rectangles has a negative
* <code>width</code> or <code>height</code>. These methods include
* <code>intersects</code>, <code>intersection</code>, and
* <code>union</code>.
*
* @version 1.27, 07/08/97
* @author Sami Shaio
* @since JDK1.0
*/
public class Rectangle implements Shape, java.io.Serializable {
/**
* The <i>x</i> coordinate of the rectangle.
* @since JDK1.0
*/
public int x;
/**
* The <i>y</i> coordinate of the rectangle.
* @since JDK1.0
*/
public int y;
/**
* The width of the rectangle.
* @since JDK1.0.
*/
public int width;
/**
* The height of the rectangle.
* @since JDK1.0
*/
public int height;
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = -4345857070255674764L;
/**
* Constructs a new rectangle whose top-left corner is at (0, 0)
* in the coordinate space, and whose width and height are zero.
* @since JDK1.0
*/
public Rectangle() {
this(0, 0, 0, 0);
}
/**
* Constructs a new rectangle, initialized to match the values of
* the specificed rectangle.
* @param r a rectangle from which to copy initial values.
* @since JDK1.1
*/
public Rectangle(Rectangle r) {
this(r.x, r.y, r.width, r.height);
}
/**
* Constructs a new rectangle 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 <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param width the width of the rectangle.
* @param height the height of the rectangle.
* @since JDK1.0
*/
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 rectangle 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 rectangle.
* @param height the height of the rectangle.
* @since JDK1.0
*/
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
/**
* Constructs a new rectangle whose top-left corner is specified
* by the <code>point</code> argument, and whose width and height
* are specified by the <code>dimension</code> argument.
* @param p a point, the top-left corner of the rectangle.
* @param d a dimension, representing the width and height.
* @since JDK1.0
*/
public Rectangle(Point p, Dimension d) {
this(p.x, p.y, d.width, d.height);
}
/**
* Constructs a new rectangle whose top-left corner is the
* specified point, and whose width and height are zero.
* @param p the top left corner of the rectangle.
* @since JDK1.0
*/
public Rectangle(Point p) {
this(p.x, p.y, 0, 0);
}
/**
* Constructs a new rectangle whose top left corner is
* (0, 0) and whose width and height are specified
* by the <code>dimension</code> argument.
* @param d a dimension, specifying width and height.
* @since JDK1.0
*/
public Rectangle(Dimension d) {
this(0, 0, d.width, d.height);
}
/**
* Gets the bounding rectangle of this rectangle.
* <p>
* This method is included for completeness, to parallel the
* <code>getBounds</code> method of <code>Component</code>.
* @return a new rectangle, equal to the bounding rectangle
* for this rectangle.
* @see java.awt.Component#getBounds
* @since JDK1.1
*/
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
/**
* Sets the bounding rectangle of this rectangle to match
* the specified rectangle.
* <p>
* This method is included for completeness, to parallel the
* <code>setBounds</code> method of <code>Component</code>.
* @param r a rectangle.
* @see java.awt.Component#setBounds(java.awt.Rectangle)
* @since JDK1.1
*/
public void setBounds(Rectangle r) {
setBounds(r.x, r.y, r.width, r.height);
}
/**
* Sets the bounding rectangle of this rectangle to the specified
* values for <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 <i>x</i> coordinate for the top-left
* corner of this rectangle.
* @param y the new <i>y</i> coordinate for the top-left
* corner of this rectangle.
* @param width the new width for this rectangle.
* @param height the new height for this rectangle.
* @see java.awt.Component#setBounds(int, int, int, int)
* @since JDK1.1
*/
public void setBounds(int x, int y, int width, int height) {
reshape(x, y, width, height);
}
/**
* @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 rectangle.
* <p>
* This method is included for completeness, to parallel the
* <code>getLocation</code> method of <code>Component</code>.
* @see java.awt.Component#getLocation
* @since JDK1.1
*/
public Point getLocation() {
return new Point(x, y);
}
/**
* Moves the rectangle 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 new location for the point.
* @see java.awt.Component#setLocation(java.awt.Point)
* @since JDK1.1
*/
public void setLocation(Point p) {
setLocation(p.x, p.y);
}
/**
* Moves the rectangle 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 <i>x</i> coordinate of the new location.
* @param y the <i>y</i> coordinate of the new location.
* @see java.awt.Component#setLocation(int, int)
* @since JDK1.1
*/
public void setLocation(int x, int y) {
move(x, y);
}
/**
* @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 the rectangle the indicated distance,
* to the right along the <i>x</i> coordinate axis, and
* downward along the <i>y</i> coordinate axis.
* @param dx the distance to move the rectangle
* along the <i>x</i> axis.
* @param dy the distance to move the rectangle
* along the <i>y</i> axis.
* @see java.awt.Rectangle#setLocation(int, int)
* @see java.awt.Rectangle#setLocation(java.awt.Point)
* @since JDK1.0
*/
public void translate(int x, int y) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -