0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.geom;
0N/A
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * The <code>Rectangle2D</code> class describes a rectangle
0N/A * defined by a location {@code (x,y)} and dimension
0N/A * {@code (w x h)}.
0N/A * <p>
0N/A * This class is only the abstract superclass for all objects that
0N/A * store a 2D rectangle.
0N/A * The actual storage representation of the coordinates is left to
0N/A * the subclass.
0N/A *
0N/A * @author Jim Graham
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class Rectangle2D extends RectangularShape {
0N/A /**
0N/A * The bitmask that indicates that a point lies to the left of
0N/A * this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public static final int OUT_LEFT = 1;
0N/A
0N/A /**
0N/A * The bitmask that indicates that a point lies above
0N/A * this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public static final int OUT_TOP = 2;
0N/A
0N/A /**
0N/A * The bitmask that indicates that a point lies to the right of
0N/A * this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public static final int OUT_RIGHT = 4;
0N/A
0N/A /**
0N/A * The bitmask that indicates that a point lies below
0N/A * this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public static final int OUT_BOTTOM = 8;
0N/A
0N/A /**
0N/A * The <code>Float</code> class defines a rectangle specified in float
0N/A * coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Float extends Rectangle2D implements Serializable {
0N/A /**
0N/A * The X coordinate of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float x;
0N/A
0N/A /**
0N/A * The Y coordinate of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y;
0N/A
0N/A /**
0N/A * The width of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float width;
0N/A
0N/A /**
0N/A * The height of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float height;
0N/A
0N/A /**
0N/A * Constructs a new <code>Rectangle2D</code>, initialized to
0N/A * location (0.0,&nbsp;0.0) and size (0.0,&nbsp;0.0).
0N/A * @since 1.2
0N/A */
0N/A public Float() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Rectangle2D</code>
0N/A * from the specified <code>float</code> coordinates.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of the newly constructed <code>Rectangle2D</code>
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of the newly constructed <code>Rectangle2D</code>
0N/A * @param w the width of the newly constructed
0N/A * <code>Rectangle2D</code>
0N/A * @param h the height of the newly constructed
0N/A * <code>Rectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public Float(float x, float y, float w, float h) {
0N/A setRect(x, y, w, h);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX() {
0N/A return (double) x;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY() {
0N/A return (double) y;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getWidth() {
0N/A return (double) width;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getHeight() {
0N/A return (double) height;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean isEmpty() {
0N/A return (width <= 0.0f) || (height <= 0.0f);
0N/A }
0N/A
0N/A /**
0N/A * Sets the location and size of this <code>Rectangle2D</code>
0N/A * to the specified <code>float</code> values.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of this <code>Rectangle2D</code>
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of this <code>Rectangle2D</code>
0N/A * @param w the width of this <code>Rectangle2D</code>
0N/A * @param h the height of this <code>Rectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setRect(float x, float y, float w, float h) {
0N/A this.x = x;
0N/A this.y = y;
0N/A this.width = w;
0N/A this.height = h;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRect(double x, double y, double w, double h) {
0N/A this.x = (float) x;
0N/A this.y = (float) y;
0N/A this.width = (float) w;
0N/A this.height = (float) h;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRect(Rectangle2D r) {
0N/A this.x = (float) r.getX();
0N/A this.y = (float) r.getY();
0N/A this.width = (float) r.getWidth();
0N/A this.height = (float) r.getHeight();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public int outcode(double x, double y) {
0N/A /*
0N/A * Note on casts to double below. If the arithmetic of
0N/A * x+w or y+h is done in float, then some bits may be
0N/A * lost if the binary exponents of x/y and w/h are not
0N/A * similar. By converting to double before the addition
0N/A * we force the addition to be carried out in double to
0N/A * avoid rounding error in the comparison.
0N/A *
0N/A * See bug 4320890 for problems that this inaccuracy causes.
0N/A */
0N/A int out = 0;
0N/A if (this.width <= 0) {
0N/A out |= OUT_LEFT | OUT_RIGHT;
0N/A } else if (x < this.x) {
0N/A out |= OUT_LEFT;
0N/A } else if (x > this.x + (double) this.width) {
0N/A out |= OUT_RIGHT;
0N/A }
0N/A if (this.height <= 0) {
0N/A out |= OUT_TOP | OUT_BOTTOM;
0N/A } else if (y < this.y) {
0N/A out |= OUT_TOP;
0N/A } else if (y > this.y + (double) this.height) {
0N/A out |= OUT_BOTTOM;
0N/A }
0N/A return out;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A return new Float(x, y, width, height);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D createIntersection(Rectangle2D r) {
0N/A Rectangle2D dest;
0N/A if (r instanceof Float) {
0N/A dest = new Rectangle2D.Float();
0N/A } else {
0N/A dest = new Rectangle2D.Double();
0N/A }
0N/A Rectangle2D.intersect(this, r, dest);
0N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D createUnion(Rectangle2D r) {
0N/A Rectangle2D dest;
0N/A if (r instanceof Float) {
0N/A dest = new Rectangle2D.Float();
0N/A } else {
0N/A dest = new Rectangle2D.Double();
0N/A }
0N/A Rectangle2D.union(this, r, dest);
0N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>String</code> representation of this
0N/A * <code>Rectangle2D</code>.
0N/A * @return a <code>String</code> representing this
0N/A * <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public String toString() {
0N/A return getClass().getName()
0N/A + "[x=" + x +
0N/A ",y=" + y +
0N/A ",w=" + width +
0N/A ",h=" + height + "]";
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 3798716824173675777L;
0N/A }
0N/A
0N/A /**
0N/A * The <code>Double</code> class defines a rectangle specified in
0N/A * double coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Double extends Rectangle2D implements Serializable {
0N/A /**
0N/A * The X coordinate of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double x;
0N/A
0N/A /**
0N/A * The Y coordinate of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y;
0N/A
0N/A /**
0N/A * The width of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double width;
0N/A
0N/A /**
0N/A * The height of this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double height;
0N/A
0N/A /**
0N/A * Constructs a new <code>Rectangle2D</code>, initialized to
0N/A * location (0,&nbsp;0) and size (0,&nbsp;0).
0N/A * @since 1.2
0N/A */
0N/A public Double() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Rectangle2D</code>
0N/A * from the specified <code>double</code> coordinates.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of the newly constructed <code>Rectangle2D</code>
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of the newly constructed <code>Rectangle2D</code>
0N/A * @param w the width of the newly constructed
0N/A * <code>Rectangle2D</code>
0N/A * @param h the height of the newly constructed
0N/A * <code>Rectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public Double(double x, double y, double w, double h) {
0N/A setRect(x, y, w, h);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX() {
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY() {
0N/A return y;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getWidth() {
0N/A return width;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getHeight() {
0N/A return height;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean isEmpty() {
0N/A return (width <= 0.0) || (height <= 0.0);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRect(double x, double y, double w, double h) {
0N/A this.x = x;
0N/A this.y = y;
0N/A this.width = w;
0N/A this.height = h;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRect(Rectangle2D r) {
0N/A this.x = r.getX();
0N/A this.y = r.getY();
0N/A this.width = r.getWidth();
0N/A this.height = r.getHeight();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public int outcode(double x, double y) {
0N/A int out = 0;
0N/A if (this.width <= 0) {
0N/A out |= OUT_LEFT | OUT_RIGHT;
0N/A } else if (x < this.x) {
0N/A out |= OUT_LEFT;
0N/A } else if (x > this.x + this.width) {
0N/A out |= OUT_RIGHT;
0N/A }
0N/A if (this.height <= 0) {
0N/A out |= OUT_TOP | OUT_BOTTOM;
0N/A } else if (y < this.y) {
0N/A out |= OUT_TOP;
0N/A } else if (y > this.y + this.height) {
0N/A out |= OUT_BOTTOM;
0N/A }
0N/A return out;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A return new Double(x, y, width, height);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D createIntersection(Rectangle2D r) {
0N/A Rectangle2D dest = new Rectangle2D.Double();
0N/A Rectangle2D.intersect(this, r, dest);
0N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D createUnion(Rectangle2D r) {
0N/A Rectangle2D dest = new Rectangle2D.Double();
0N/A Rectangle2D.union(this, r, dest);
0N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>String</code> representation of this
0N/A * <code>Rectangle2D</code>.
0N/A * @return a <code>String</code> representing this
0N/A * <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public String toString() {
0N/A return getClass().getName()
0N/A + "[x=" + x +
0N/A ",y=" + y +
0N/A ",w=" + width +
0N/A ",h=" + height + "]";
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 7771313791441850493L;
0N/A }
0N/A
0N/A /**
0N/A * This is an abstract class that cannot be instantiated directly.
0N/A * Type-specific implementation subclasses are available for
0N/A * instantiation and provide a number of formats for storing
0N/A * the information necessary to satisfy the various accessor
0N/A * methods below.
0N/A *
0N/A * @see java.awt.geom.Rectangle2D.Float
0N/A * @see java.awt.geom.Rectangle2D.Double
0N/A * @see java.awt.Rectangle
0N/A * @since 1.2
0N/A */
0N/A protected Rectangle2D() {
0N/A }
0N/A
0N/A /**
0N/A * Sets the location and size of this <code>Rectangle2D</code>
0N/A * to the specified <code>double</code> values.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of this <code>Rectangle2D</code>
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of this <code>Rectangle2D</code>
0N/A * @param w the width of this <code>Rectangle2D</code>
0N/A * @param h the height of this <code>Rectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public abstract void setRect(double x, double y, double w, double h);
0N/A
0N/A /**
0N/A * Sets this <code>Rectangle2D</code> to be the same as the specified
0N/A * <code>Rectangle2D</code>.
0N/A * @param r the specified <code>Rectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setRect(Rectangle2D r) {
0N/A setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * Tests if the specified line segment intersects the interior of this
0N/A * <code>Rectangle2D</code>.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the specified
0N/A * line segment
0N/A * @param y1 the Y coordinate of the start point of the specified
0N/A * line segment
0N/A * @param x2 the X coordinate of the end point of the specified
0N/A * line segment
0N/A * @param y2 the Y coordinate of the end point of the specified
0N/A * line segment
0N/A * @return <code>true</code> if the specified line segment intersects
0N/A * the interior of this <code>Rectangle2D</code>; <code>false</code>
0N/A * otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean intersectsLine(double x1, double y1, double x2, double y2) {
0N/A int out1, out2;
0N/A if ((out2 = outcode(x2, y2)) == 0) {
0N/A return true;
0N/A }
0N/A while ((out1 = outcode(x1, y1)) != 0) {
0N/A if ((out1 & out2) != 0) {
0N/A return false;
0N/A }
0N/A if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
0N/A double x = getX();
0N/A if ((out1 & OUT_RIGHT) != 0) {
0N/A x += getWidth();
0N/A }
0N/A y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
0N/A x1 = x;
0N/A } else {
0N/A double y = getY();
0N/A if ((out1 & OUT_BOTTOM) != 0) {
0N/A y += getHeight();
0N/A }
0N/A x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
0N/A y1 = y;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Tests if the specified line segment intersects the interior of this
0N/A * <code>Rectangle2D</code>.
0N/A * @param l the specified {@link Line2D} to test for intersection
0N/A * with the interior of this <code>Rectangle2D</code>
0N/A * @return <code>true</code> if the specified <code>Line2D</code>
0N/A * intersects the interior of this <code>Rectangle2D</code>;
0N/A * <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean intersectsLine(Line2D l) {
0N/A return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
0N/A }
0N/A
0N/A /**
0N/A * Determines where the specified coordinates lie with respect
0N/A * to this <code>Rectangle2D</code>.
0N/A * This method computes a binary OR of the appropriate mask values
0N/A * indicating, for each side of this <code>Rectangle2D</code>,
0N/A * whether or not the specified coordinates are on the same side
0N/A * of the edge as the rest of this <code>Rectangle2D</code>.
0N/A * @param x the specified X coordinate
0N/A * @param y the specified Y coordinate
0N/A * @return the logical OR of all appropriate out codes.
0N/A * @see #OUT_LEFT
0N/A * @see #OUT_TOP
0N/A * @see #OUT_RIGHT
0N/A * @see #OUT_BOTTOM
0N/A * @since 1.2
0N/A */
0N/A public abstract int outcode(double x, double y);
0N/A
0N/A /**
0N/A * Determines where the specified {@link Point2D} lies with
0N/A * respect to this <code>Rectangle2D</code>.
0N/A * This method computes a binary OR of the appropriate mask values
0N/A * indicating, for each side of this <code>Rectangle2D</code>,
0N/A * whether or not the specified <code>Point2D</code> is on the same
0N/A * side of the edge as the rest of this <code>Rectangle2D</code>.
0N/A * @param p the specified <code>Point2D</code>
0N/A * @return the logical OR of all appropriate out codes.
0N/A * @see #OUT_LEFT
0N/A * @see #OUT_TOP
0N/A * @see #OUT_RIGHT
0N/A * @see #OUT_BOTTOM
0N/A * @since 1.2
0N/A */
0N/A public int outcode(Point2D p) {
0N/A return outcode(p.getX(), p.getY());
0N/A }
0N/A
0N/A /**
0N/A * Sets the location and size of the outer bounds of this
0N/A * <code>Rectangle2D</code> to the specified rectangular values.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner
0N/A * of this <code>Rectangle2D</code>
0N/A * @param y the Y coordinate of the upper-left corner
0N/A * of this <code>Rectangle2D</code>
0N/A * @param w the width of this <code>Rectangle2D</code>
0N/A * @param h the height of this <code>Rectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(double x, double y, double w, double h) {
0N/A setRect(x, y, w, h);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A return (Rectangle2D) clone();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y) {
0N/A double x0 = getX();
0N/A double y0 = getY();
0N/A return (x >= x0 &&
0N/A y >= y0 &&
0N/A x < x0 + getWidth() &&
0N/A y < y0 + getHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(double x, double y, double w, double h) {
0N/A if (isEmpty() || w <= 0 || h <= 0) {
0N/A return false;
0N/A }
0N/A double x0 = getX();
0N/A double y0 = getY();
0N/A return (x + w > x0 &&
0N/A y + h > y0 &&
0N/A x < x0 + getWidth() &&
0N/A y < y0 + getHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y, double w, double h) {
0N/A if (isEmpty() || w <= 0 || h <= 0) {
0N/A return false;
0N/A }
0N/A double x0 = getX();
0N/A double y0 = getY();
0N/A return (x >= x0 &&
0N/A y >= y0 &&
0N/A (x + w) <= x0 + getWidth() &&
0N/A (y + h) <= y0 + getHeight());
0N/A }
0N/A
0N/A /**
0N/A * Returns a new <code>Rectangle2D</code> object representing the
0N/A * intersection of this <code>Rectangle2D</code> with the specified
0N/A * <code>Rectangle2D</code>.
0N/A * @param r the <code>Rectangle2D</code> to be intersected with
0N/A * this <code>Rectangle2D</code>
0N/A * @return the largest <code>Rectangle2D</code> contained in both
0N/A * the specified <code>Rectangle2D</code> and in this
0N/A * <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract Rectangle2D createIntersection(Rectangle2D r);
0N/A
0N/A /**
0N/A * Intersects the pair of specified source <code>Rectangle2D</code>
0N/A * objects and puts the result into the specified destination
0N/A * <code>Rectangle2D</code> object. One of the source rectangles
0N/A * can also be the destination to avoid creating a third Rectangle2D
0N/A * object, but in this case the original points of this source
0N/A * rectangle will be overwritten by this method.
0N/A * @param src1 the first of a pair of <code>Rectangle2D</code>
0N/A * objects to be intersected with each other
0N/A * @param src2 the second of a pair of <code>Rectangle2D</code>
0N/A * objects to be intersected with each other
0N/A * @param dest the <code>Rectangle2D</code> that holds the
0N/A * results of the intersection of <code>src1</code> and
0N/A * <code>src2</code>
0N/A * @since 1.2
0N/A */
0N/A public static void intersect(Rectangle2D src1,
0N/A Rectangle2D src2,
0N/A Rectangle2D dest) {
0N/A double x1 = Math.max(src1.getMinX(), src2.getMinX());
0N/A double y1 = Math.max(src1.getMinY(), src2.getMinY());
0N/A double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
0N/A double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
0N/A dest.setFrame(x1, y1, x2-x1, y2-y1);
0N/A }
0N/A
0N/A /**
0N/A * Returns a new <code>Rectangle2D</code> object representing the
0N/A * union of this <code>Rectangle2D</code> with the specified
0N/A * <code>Rectangle2D</code>.
0N/A * @param r the <code>Rectangle2D</code> to be combined with
0N/A * this <code>Rectangle2D</code>
0N/A * @return the smallest <code>Rectangle2D</code> containing both
0N/A * the specified <code>Rectangle2D</code> and this
0N/A * <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract Rectangle2D createUnion(Rectangle2D r);
0N/A
0N/A /**
0N/A * Unions the pair of source <code>Rectangle2D</code> objects
0N/A * and puts the result into the specified destination
0N/A * <code>Rectangle2D</code> object. One of the source rectangles
0N/A * can also be the destination to avoid creating a third Rectangle2D
0N/A * object, but in this case the original points of this source
0N/A * rectangle will be overwritten by this method.
0N/A * @param src1 the first of a pair of <code>Rectangle2D</code>
0N/A * objects to be combined with each other
0N/A * @param src2 the second of a pair of <code>Rectangle2D</code>
0N/A * objects to be combined with each other
0N/A * @param dest the <code>Rectangle2D</code> that holds the
0N/A * results of the union of <code>src1</code> and
0N/A * <code>src2</code>
0N/A * @since 1.2
0N/A */
0N/A public static void union(Rectangle2D src1,
0N/A Rectangle2D src2,
0N/A Rectangle2D dest) {
0N/A double x1 = Math.min(src1.getMinX(), src2.getMinX());
0N/A double y1 = Math.min(src1.getMinY(), src2.getMinY());
0N/A double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
0N/A double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
0N/A dest.setFrameFromDiagonal(x1, y1, x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * Adds a point, specified by the double precision arguments
0N/A * <code>newx</code> and <code>newy</code>, to this
0N/A * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
0N/A * is the smallest <code>Rectangle2D</code> that
0N/A * contains both the original <code>Rectangle2D</code> and the
0N/A * specified point.
0N/A * <p>
0N/A * After adding a point, a call to <code>contains</code> with the
0N/A * added point as an argument does not necessarily return
0N/A * <code>true</code>. The <code>contains</code> method does not
0N/A * return <code>true</code> for points on the right or bottom
0N/A * edges of a rectangle. Therefore, if the added point falls on
0N/A * the left or bottom edge of the enlarged rectangle,
0N/A * <code>contains</code> returns <code>false</code> for that point.
0N/A * @param newx the X coordinate of the new point
0N/A * @param newy the Y coordinate of the new point
0N/A * @since 1.2
0N/A */
0N/A public void add(double newx, double newy) {
0N/A double x1 = Math.min(getMinX(), newx);
0N/A double x2 = Math.max(getMaxX(), newx);
0N/A double y1 = Math.min(getMinY(), newy);
0N/A double y2 = Math.max(getMaxY(), newy);
0N/A setRect(x1, y1, x2 - x1, y2 - y1);
0N/A }
0N/A
0N/A /**
0N/A * Adds the <code>Point2D</code> object <code>pt</code> to this
0N/A * <code>Rectangle2D</code>.
0N/A * The resulting <code>Rectangle2D</code> is the smallest
0N/A * <code>Rectangle2D</code> that contains both the original
0N/A * <code>Rectangle2D</code> and the specified <code>Point2D</code>.
0N/A * <p>
0N/A * After adding a point, a call to <code>contains</code> with the
0N/A * added point as an argument does not necessarily return
0N/A * <code>true</code>. The <code>contains</code>
0N/A * method does not return <code>true</code> for points on the right
0N/A * or bottom edges of a rectangle. Therefore, if the added point falls
0N/A * on the left or bottom edge of the enlarged rectangle,
0N/A * <code>contains</code> returns <code>false</code> for that point.
0N/A * @param pt the new <code>Point2D</code> to add to this
0N/A * <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public void add(Point2D pt) {
0N/A add(pt.getX(), pt.getY());
0N/A }
0N/A
0N/A /**
0N/A * Adds a <code>Rectangle2D</code> object to this
0N/A * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
0N/A * is the union of the two <code>Rectangle2D</code> objects.
0N/A * @param r the <code>Rectangle2D</code> to add to this
0N/A * <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public void add(Rectangle2D r) {
0N/A double x1 = Math.min(getMinX(), r.getMinX());
0N/A double x2 = Math.max(getMaxX(), r.getMaxX());
0N/A double y1 = Math.min(getMinY(), r.getMinY());
0N/A double y2 = Math.max(getMaxY(), r.getMaxY());
0N/A setRect(x1, y1, x2 - x1, y2 - y1);
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of this
0N/A * <code>Rectangle2D</code>.
0N/A * The iterator for this class is multi-threaded safe, which means
0N/A * that this <code>Rectangle2D</code> class guarantees that
0N/A * modifications to the geometry of this <code>Rectangle2D</code>
0N/A * object do not affect any iterations of that geometry that
0N/A * are already in process.
0N/A * @param at an optional <code>AffineTransform</code> to be applied to
0N/A * the coordinates as they are returned in the iteration, or
0N/A * <code>null</code> if untransformed coordinates are desired
0N/A * @return the <code>PathIterator</code> object that returns the
0N/A * geometry of the outline of this
0N/A * <code>Rectangle2D</code>, one segment at a time.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at) {
0N/A return new RectIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of the
0N/A * flattened <code>Rectangle2D</code>. Since rectangles are already
0N/A * flat, the <code>flatness</code> parameter is ignored.
0N/A * The iterator for this class is multi-threaded safe, which means
0N/A * that this <code>Rectangle2D</code> class guarantees that
0N/A * modifications to the geometry of this <code>Rectangle2D</code>
0N/A * object do not affect any iterations of that geometry that
0N/A * are already in process.
0N/A * @param at an optional <code>AffineTransform</code> to be applied to
0N/A * the coordinates as they are returned in the iteration, or
0N/A * <code>null</code> if untransformed coordinates are desired
0N/A * @param flatness the maximum distance that the line segments used to
0N/A * approximate the curved segments are allowed to deviate from any
0N/A * point on the original curve. Since rectangles are already flat,
0N/A * the <code>flatness</code> parameter is ignored.
0N/A * @return the <code>PathIterator</code> object that returns the
0N/A * geometry of the outline of this
0N/A * <code>Rectangle2D</code>, one segment at a time.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at, double flatness) {
0N/A return new RectIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this <code>Rectangle2D</code>.
0N/A * @return the hashcode for this <code>Rectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public int hashCode() {
0N/A long bits = java.lang.Double.doubleToLongBits(getX());
0N/A bits += java.lang.Double.doubleToLongBits(getY()) * 37;
0N/A bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
0N/A bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
0N/A return (((int) bits) ^ ((int) (bits >> 32)));
0N/A }
0N/A
0N/A /**
0N/A * Determines whether or not the specified <code>Object</code> is
0N/A * equal to this <code>Rectangle2D</code>. The specified
0N/A * <code>Object</code> is equal to this <code>Rectangle2D</code>
0N/A * if it is an instance of <code>Rectangle2D</code> and if its
0N/A * location and size are the same as this <code>Rectangle2D</code>.
0N/A * @param obj an <code>Object</code> to be compared with this
0N/A * <code>Rectangle2D</code>.
0N/A * @return <code>true</code> if <code>obj</code> is an instance
0N/A * of <code>Rectangle2D</code> and has
0N/A * the same values; <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj == this) {
0N/A return true;
0N/A }
0N/A if (obj instanceof Rectangle2D) {
0N/A Rectangle2D r2d = (Rectangle2D) obj;
0N/A return ((getX() == r2d.getX()) &&
0N/A (getY() == r2d.getY()) &&
0N/A (getWidth() == r2d.getWidth()) &&
0N/A (getHeight() == r2d.getHeight()));
0N/A }
0N/A return false;
0N/A }
0N/A}