rectangle.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 672 行 · 第 1/2 页
JAVA
672 行
/* * @(#)Rectangle.java 1.49 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.awt;/** * 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>. * * @version 1.52, 02/02/00 * @author Sami Shaio * @since JDK1.0 */public class Rectangle implements Shape, java.io.Serializable, Cloneable { /** * 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() * @since JDK1.0. */ public int width; /** * The height of the <code>Rectangle</code>. * * @serial * @see #setSize(int, int) * @see #getSize() */ public int height; /* * JDK 1.1 serialVersionUID */ private static final long serialVersionUID = -4345857070255674764L; /** * 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 specificed <code>Rectangle</code>. * @param r the <code>Rectangle</code> from which to copy initial values * to a newly constructed <code>Rectangle</code> * @since JDK1.1 */ 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, y the specified coordinates * @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); } /** * 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 * @since JDK1.1 */ public Rectangle getBounds() { 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 java.awt.Component#setBounds(java.awt.Rectangle) * @since JDK1.1 */ public void setBounds(Rectangle r) { setBounds(r.x, r.y, r.width, r.height); } private double getX() { return (double) x; } private double getY() { return (double) y; } private double getWidth() { return (double) width; } private double getHeight() { return (double) height; } boolean contains(double x, double y) { double x0 = getX(); double y0 = getY(); return (x >= x0 && y >= y0 && x < x0 + getWidth() && y < y0 + getHeight()); } /** * 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, y the new coordinates 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 java.awt.Component#setBounds(int, int, int, int) * @since JDK1.1 */ public void setBounds(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 * @since JDK1.1 */ 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) * @since JDK1.1 */ 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, y the coordinates of the new location * @see java.awt.Component#setLocation(int, int) * @since JDK1.1 */ public void setLocation(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 dx the distance to move this <code>Rectangle</code> * along the x axis * @param dy 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 * @since JDK1.1 */ 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) * @since JDK1.1 */ public void setSize(Dimension d) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?