|
Java example source code file (Rectangle2D.java)
The Rectangle2D.java Java example source code/* * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code 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. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 in the LICENSE file that * accompanied this code). * * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.awt.geom; import java.io.Serializable; /** * The <code>Rectangle2D class describes a rectangle * defined by a location {@code (x,y)} and dimension * {@code (w x h)}. * <p> * This class is only the abstract superclass for all objects that * store a 2D rectangle. * The actual storage representation of the coordinates is left to * the subclass. * * @author Jim Graham * @since 1.2 */ public abstract class Rectangle2D extends RectangularShape { /** * The bitmask that indicates that a point lies to the left of * this <code>Rectangle2D. * @since 1.2 */ public static final int OUT_LEFT = 1; /** * The bitmask that indicates that a point lies above * this <code>Rectangle2D. * @since 1.2 */ public static final int OUT_TOP = 2; /** * The bitmask that indicates that a point lies to the right of * this <code>Rectangle2D. * @since 1.2 */ public static final int OUT_RIGHT = 4; /** * The bitmask that indicates that a point lies below * this <code>Rectangle2D. * @since 1.2 */ public static final int OUT_BOTTOM = 8; /** * The <code>Float class defines a rectangle specified in float * coordinates. * @since 1.2 */ public static class Float extends Rectangle2D implements Serializable { /** * The X coordinate of this <code>Rectangle2D. * @since 1.2 * @serial */ public float x; /** * The Y coordinate of this <code>Rectangle2D. * @since 1.2 * @serial */ public float y; /** * The width of this <code>Rectangle2D. * @since 1.2 * @serial */ public float width; /** * The height of this <code>Rectangle2D. * @since 1.2 * @serial */ public float height; /** * Constructs a new <code>Rectangle2D, initialized to * location (0.0, 0.0) and size (0.0, 0.0). * @since 1.2 */ public Float() { } /** * Constructs and initializes a <code>Rectangle2D * from the specified <code>float coordinates. * * @param x the X coordinate of the upper-left corner * of the newly constructed <code>Rectangle2D * @param y the Y coordinate of the upper-left corner * of the newly constructed <code>Rectangle2D * @param w the width of the newly constructed * <code>Rectangle2D * @param h the height of the newly constructed * <code>Rectangle2D * @since 1.2 */ public Float(float x, float y, float w, float h) { setRect(x, y, w, h); } /** * {@inheritDoc} * @since 1.2 */ public double getX() { return (double) x; } /** * {@inheritDoc} * @since 1.2 */ public double getY() { return (double) y; } /** * {@inheritDoc} * @since 1.2 */ public double getWidth() { return (double) width; } /** * {@inheritDoc} * @since 1.2 */ public double getHeight() { return (double) height; } /** * {@inheritDoc} * @since 1.2 */ public boolean isEmpty() { return (width <= 0.0f) || (height <= 0.0f); } /** * Sets the location and size of this <code>Rectangle2D * to the specified <code>float values. * * @param x the X coordinate of the upper-left corner * of this <code>Rectangle2D * @param y the Y coordinate of the upper-left corner * of this <code>Rectangle2D * @param w the width of this <code>Rectangle2D * @param h the height of this <code>Rectangle2D * @since 1.2 */ public void setRect(float x, float y, float w, float h) { this.x = x; this.y = y; this.width = w; this.height = h; } /** * {@inheritDoc} * @since 1.2 */ public void setRect(double x, double y, double w, double h) { this.x = (float) x; this.y = (float) y; this.width = (float) w; this.height = (float) h; } /** * {@inheritDoc} * @since 1.2 */ public void setRect(Rectangle2D r) { this.x = (float) r.getX(); this.y = (float) r.getY(); this.width = (float) r.getWidth(); this.height = (float) r.getHeight(); } /** * {@inheritDoc} * @since 1.2 */ public int outcode(double x, double y) { /* * Note on casts to double below. If the arithmetic of * x+w or y+h is done in float, then some bits may be * lost if the binary exponents of x/y and w/h are not * similar. By converting to double before the addition * we force the addition to be carried out in double to * avoid rounding error in the comparison. * * See bug 4320890 for problems that this inaccuracy causes. */ int out = 0; if (this.width <= 0) { out |= OUT_LEFT | OUT_RIGHT; } else if (x < this.x) { out |= OUT_LEFT; } else if (x > this.x + (double) this.width) { out |= OUT_RIGHT; } if (this.height <= 0) { out |= OUT_TOP | OUT_BOTTOM; } else if (y < this.y) { out |= OUT_TOP; } else if (y > this.y + (double) this.height) { out |= OUT_BOTTOM; } return out; } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D getBounds2D() { return new Float(x, y, width, height); } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D createIntersection(Rectangle2D r) { Rectangle2D dest; if (r instanceof Float) { dest = new Rectangle2D.Float(); } else { dest = new Rectangle2D.Double(); } Rectangle2D.intersect(this, r, dest); return dest; } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D createUnion(Rectangle2D r) { Rectangle2D dest; if (r instanceof Float) { dest = new Rectangle2D.Float(); } else { dest = new Rectangle2D.Double(); } Rectangle2D.union(this, r, dest); return dest; } /** * Returns the <code>String representation of this * <code>Rectangle2D. * @return a <code>String representing this * <code>Rectangle2D. * @since 1.2 */ public String toString() { return getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width + ",h=" + height + "]"; } /* * JDK 1.6 serialVersionUID */ private static final long serialVersionUID = 3798716824173675777L; } /** * The <code>Double class defines a rectangle specified in * double coordinates. * @since 1.2 */ public static class Double extends Rectangle2D implements Serializable { /** * The X coordinate of this <code>Rectangle2D. * @since 1.2 * @serial */ public double x; /** * The Y coordinate of this <code>Rectangle2D. * @since 1.2 * @serial */ public double y; /** * The width of this <code>Rectangle2D. * @since 1.2 * @serial */ public double width; /** * The height of this <code>Rectangle2D. * @since 1.2 * @serial */ public double height; /** * Constructs a new <code>Rectangle2D, initialized to * location (0, 0) and size (0, 0). * @since 1.2 */ public Double() { } /** * Constructs and initializes a <code>Rectangle2D * from the specified <code>double coordinates. * * @param x the X coordinate of the upper-left corner * of the newly constructed <code>Rectangle2D * @param y the Y coordinate of the upper-left corner * of the newly constructed <code>Rectangle2D * @param w the width of the newly constructed * <code>Rectangle2D * @param h the height of the newly constructed * <code>Rectangle2D * @since 1.2 */ public Double(double x, double y, double w, double h) { setRect(x, y, w, h); } /** * {@inheritDoc} * @since 1.2 */ public double getX() { return x; } /** * {@inheritDoc} * @since 1.2 */ public double getY() { return y; } /** * {@inheritDoc} * @since 1.2 */ public double getWidth() { return width; } /** * {@inheritDoc} * @since 1.2 */ public double getHeight() { return height; } /** * {@inheritDoc} * @since 1.2 */ public boolean isEmpty() { return (width <= 0.0) || (height <= 0.0); } /** * {@inheritDoc} * @since 1.2 */ public void setRect(double x, double y, double w, double h) { this.x = x; this.y = y; this.width = w; this.height = h; } /** * {@inheritDoc} * @since 1.2 */ public void setRect(Rectangle2D r) { this.x = r.getX(); this.y = r.getY(); this.width = r.getWidth(); this.height = r.getHeight(); } /** * {@inheritDoc} * @since 1.2 */ public int outcode(double x, double y) { int out = 0; if (this.width <= 0) { out |= OUT_LEFT | OUT_RIGHT; } else if (x < this.x) { out |= OUT_LEFT; } else if (x > this.x + this.width) { out |= OUT_RIGHT; } if (this.height <= 0) { out |= OUT_TOP | OUT_BOTTOM; } else if (y < this.y) { out |= OUT_TOP; } else if (y > this.y + this.height) { out |= OUT_BOTTOM; } return out; } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D getBounds2D() { return new Double(x, y, width, height); } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D createIntersection(Rectangle2D r) { Rectangle2D dest = new Rectangle2D.Double(); Rectangle2D.intersect(this, r, dest); return dest; } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D createUnion(Rectangle2D r) { Rectangle2D dest = new Rectangle2D.Double(); Rectangle2D.union(this, r, dest); return dest; } /** * Returns the <code>String representation of this * <code>Rectangle2D. * @return a <code>String representing this * <code>Rectangle2D. * @since 1.2 */ public String toString() { return getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width + ",h=" + height + "]"; } /* * JDK 1.6 serialVersionUID */ private static final long serialVersionUID = 7771313791441850493L; } /** * This is an abstract class that cannot be instantiated directly. * Type-specific implementation subclasses are available for * instantiation and provide a number of formats for storing * the information necessary to satisfy the various accessor * methods below. * * @see java.awt.geom.Rectangle2D.Float * @see java.awt.geom.Rectangle2D.Double * @see java.awt.Rectangle * @since 1.2 */ protected Rectangle2D() { } /** * Sets the location and size of this <code>Rectangle2D * to the specified <code>double values. * * @param x the X coordinate of the upper-left corner * of this <code>Rectangle2D * @param y the Y coordinate of the upper-left corner * of this <code>Rectangle2D * @param w the width of this <code>Rectangle2D * @param h the height of this <code>Rectangle2D * @since 1.2 */ public abstract void setRect(double x, double y, double w, double h); /** * Sets this <code>Rectangle2D to be the same as the specified * <code>Rectangle2D. * @param r the specified <code>Rectangle2D * @since 1.2 */ public void setRect(Rectangle2D r) { setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight()); } /** * Tests if the specified line segment intersects the interior of this * <code>Rectangle2D. * * @param x1 the X coordinate of the start point of the specified * line segment * @param y1 the Y coordinate of the start point of the specified * line segment * @param x2 the X coordinate of the end point of the specified * line segment * @param y2 the Y coordinate of the end point of the specified * line segment * @return <code>true if the specified line segment intersects * the interior of this <code>Rectangle2D; Other Java examples (source code examples)Here is a short list of links related to this Java Rectangle2D.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.