0N/A/*
2362N/A * Copyright (c) 1997, 2008, 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.awt.Shape;
0N/Aimport java.awt.Rectangle;
243N/Aimport java.beans.Transient;
0N/A
0N/A/**
0N/A * <code>RectangularShape</code> is the base class for a number of
0N/A * {@link Shape} objects whose geometry is defined by a rectangular frame.
0N/A * This class does not directly specify any specific geometry by
0N/A * itself, but merely provides manipulation methods inherited by
0N/A * a whole category of <code>Shape</code> objects.
0N/A * The manipulation methods provided by this class can be used to
0N/A * query and modify the rectangular frame, which provides a reference
0N/A * for the subclasses to define their geometry.
0N/A *
0N/A * @author Jim Graham
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class RectangularShape implements Shape, Cloneable {
0N/A
0N/A /**
0N/A * This is an abstract class that cannot be instantiated directly.
0N/A *
0N/A * @see Arc2D
0N/A * @see Ellipse2D
0N/A * @see Rectangle2D
0N/A * @see RoundRectangle2D
0N/A * @since 1.2
0N/A */
0N/A protected RectangularShape() {
0N/A }
0N/A
0N/A /**
0N/A * Returns the X coordinate of the upper-left corner of
0N/A * the framing rectangle in <code>double</code> precision.
0N/A * @return the X coordinate of the upper-left corner of
0N/A * the framing rectangle.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getX();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the upper-left corner of
0N/A * the framing rectangle in <code>double</code> precision.
0N/A * @return the Y coordinate of the upper-left corner of
0N/A * the framing rectangle.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getY();
0N/A
0N/A /**
0N/A * Returns the width of the framing rectangle in
0N/A * <code>double</code> precision.
0N/A * @return the width of the framing rectangle.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getWidth();
0N/A
0N/A /**
0N/A * Returns the height of the framing rectangle
0N/A * in <code>double</code> precision.
0N/A * @return the height of the framing rectangle.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getHeight();
0N/A
0N/A /**
0N/A * Returns the smallest X coordinate of the framing
0N/A * rectangle of the <code>Shape</code> in <code>double</code>
0N/A * precision.
0N/A * @return the smallest X coordinate of the framing
0N/A * rectangle of the <code>Shape</code>.
0N/A * @since 1.2
0N/A */
0N/A public double getMinX() {
0N/A return getX();
0N/A }
0N/A
0N/A /**
0N/A * Returns the smallest Y coordinate of the framing
0N/A * rectangle of the <code>Shape</code> in <code>double</code>
0N/A * precision.
0N/A * @return the smallest Y coordinate of the framing
0N/A * rectangle of the <code>Shape</code>.
0N/A * @since 1.2
0N/A */
0N/A public double getMinY() {
0N/A return getY();
0N/A }
0N/A
0N/A /**
0N/A * Returns the largest X coordinate of the framing
0N/A * rectangle of the <code>Shape</code> in <code>double</code>
0N/A * precision.
0N/A * @return the largest X coordinate of the framing
0N/A * rectangle of the <code>Shape</code>.
0N/A * @since 1.2
0N/A */
0N/A public double getMaxX() {
0N/A return getX() + getWidth();
0N/A }
0N/A
0N/A /**
0N/A * Returns the largest Y coordinate of the framing
0N/A * rectangle of the <code>Shape</code> in <code>double</code>
0N/A * precision.
0N/A * @return the largest Y coordinate of the framing
0N/A * rectangle of the <code>Shape</code>.
0N/A * @since 1.2
0N/A */
0N/A public double getMaxY() {
0N/A return getY() + getHeight();
0N/A }
0N/A
0N/A /**
0N/A * Returns the X coordinate of the center of the framing
0N/A * rectangle of the <code>Shape</code> in <code>double</code>
0N/A * precision.
0N/A * @return the X coordinate of the center of the framing rectangle
0N/A * of the <code>Shape</code>.
0N/A * @since 1.2
0N/A */
0N/A public double getCenterX() {
0N/A return getX() + getWidth() / 2.0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the center of the framing
0N/A * rectangle of the <code>Shape</code> in <code>double</code>
0N/A * precision.
0N/A * @return the Y coordinate of the center of the framing rectangle
0N/A * of the <code>Shape</code>.
0N/A * @since 1.2
0N/A */
0N/A public double getCenterY() {
0N/A return getY() + getHeight() / 2.0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the framing {@link Rectangle2D}
0N/A * that defines the overall shape of this object.
0N/A * @return a <code>Rectangle2D</code>, specified in
0N/A * <code>double</code> coordinates.
0N/A * @see #setFrame(double, double, double, double)
0N/A * @see #setFrame(Point2D, Dimension2D)
0N/A * @see #setFrame(Rectangle2D)
0N/A * @since 1.2
0N/A */
243N/A @Transient
0N/A public Rectangle2D getFrame() {
0N/A return new Rectangle2D.Double(getX(), getY(), getWidth(), getHeight());
0N/A }
0N/A
0N/A /**
0N/A * Determines whether the <code>RectangularShape</code> is empty.
0N/A * When the <code>RectangularShape</code> is empty, it encloses no
0N/A * area.
0N/A * @return <code>true</code> if the <code>RectangularShape</code> is empty;
0N/A * <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public abstract boolean isEmpty();
0N/A
0N/A /**
0N/A * Sets the location and size of the framing rectangle of this
0N/A * <code>Shape</code> to the specified rectangular values.
0N/A *
0N/A * @param x the X coordinate of the upper-left corner of the
0N/A * specified rectangular shape
0N/A * @param y the Y coordinate of the upper-left corner of the
0N/A * specified rectangular shape
0N/A * @param w the width of the specified rectangular shape
0N/A * @param h the height of the specified rectangular shape
0N/A * @see #getFrame
0N/A * @since 1.2
0N/A */
0N/A public abstract void setFrame(double x, double y, double w, double h);
0N/A
0N/A /**
0N/A * Sets the location and size of the framing rectangle of this
0N/A * <code>Shape</code> to the specified {@link Point2D} and
0N/A * {@link Dimension2D}, respectively. The framing rectangle is used
0N/A * by the subclasses of <code>RectangularShape</code> to define
0N/A * their geometry.
0N/A * @param loc the specified <code>Point2D</code>
0N/A * @param size the specified <code>Dimension2D</code>
0N/A * @see #getFrame
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(Point2D loc, Dimension2D size) {
0N/A setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * Sets the framing rectangle of this <code>Shape</code> to
0N/A * be the specified <code>Rectangle2D</code>. The framing rectangle is
0N/A * used by the subclasses of <code>RectangularShape</code> to define
0N/A * their geometry.
0N/A * @param r the specified <code>Rectangle2D</code>
0N/A * @see #getFrame
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(Rectangle2D r) {
0N/A setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * Sets the diagonal of the framing rectangle of this <code>Shape</code>
0N/A * based on the two specified coordinates. The framing rectangle is
0N/A * used by the subclasses of <code>RectangularShape</code> to define
0N/A * their geometry.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the specified diagonal
0N/A * @param y1 the Y coordinate of the start point of the specified diagonal
0N/A * @param x2 the X coordinate of the end point of the specified diagonal
0N/A * @param y2 the Y coordinate of the end point of the specified diagonal
0N/A * @since 1.2
0N/A */
0N/A public void setFrameFromDiagonal(double x1, double y1,
0N/A double x2, double y2) {
0N/A if (x2 < x1) {
0N/A double t = x1;
0N/A x1 = x2;
0N/A x2 = t;
0N/A }
0N/A if (y2 < y1) {
0N/A double t = y1;
0N/A y1 = y2;
0N/A y2 = t;
0N/A }
0N/A setFrame(x1, y1, x2 - x1, y2 - y1);
0N/A }
0N/A
0N/A /**
0N/A * Sets the diagonal of the framing rectangle of this <code>Shape</code>
0N/A * based on two specified <code>Point2D</code> objects. The framing
0N/A * rectangle is used by the subclasses of <code>RectangularShape</code>
0N/A * to define their geometry.
0N/A *
0N/A * @param p1 the start <code>Point2D</code> of the specified diagonal
0N/A * @param p2 the end <code>Point2D</code> of the specified diagonal
0N/A * @since 1.2
0N/A */
0N/A public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
0N/A setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
0N/A }
0N/A
0N/A /**
0N/A * Sets the framing rectangle of this <code>Shape</code>
0N/A * based on the specified center point coordinates and corner point
0N/A * coordinates. The framing rectangle is used by the subclasses of
0N/A * <code>RectangularShape</code> to define their geometry.
0N/A *
0N/A * @param centerX the X coordinate of the specified center point
0N/A * @param centerY the Y coordinate of the specified center point
0N/A * @param cornerX the X coordinate of the specified corner point
0N/A * @param cornerY the Y coordinate of the specified corner point
0N/A * @since 1.2
0N/A */
0N/A public void setFrameFromCenter(double centerX, double centerY,
0N/A double cornerX, double cornerY) {
0N/A double halfW = Math.abs(cornerX - centerX);
0N/A double halfH = Math.abs(cornerY - centerY);
0N/A setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
0N/A }
0N/A
0N/A /**
0N/A * Sets the framing rectangle of this <code>Shape</code> based on a
0N/A * specified center <code>Point2D</code> and corner
0N/A * <code>Point2D</code>. The framing rectangle is used by the subclasses
0N/A * of <code>RectangularShape</code> to define their geometry.
0N/A * @param center the specified center <code>Point2D</code>
0N/A * @param corner the specified corner <code>Point2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setFrameFromCenter(Point2D center, Point2D corner) {
0N/A setFrameFromCenter(center.getX(), center.getY(),
0N/A corner.getX(), corner.getY());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(Point2D p) {
0N/A return contains(p.getX(), p.getY());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(Rectangle2D r) {
0N/A return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(Rectangle2D r) {
0N/A return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle getBounds() {
0N/A double width = getWidth();
0N/A double height = getHeight();
0N/A if (width < 0 || height < 0) {
0N/A return new Rectangle();
0N/A }
0N/A double x = getX();
0N/A double y = getY();
0N/A double x1 = Math.floor(x);
0N/A double y1 = Math.floor(y);
0N/A double x2 = Math.ceil(x + width);
0N/A double y2 = Math.ceil(y + height);
0N/A return new Rectangle((int) x1, (int) y1,
0N/A (int) (x2 - x1), (int) (y2 - y1));
0N/A }
0N/A
0N/A /**
0N/A * Returns an iterator object that iterates along the
0N/A * <code>Shape</code> object's boundary and provides access to a
0N/A * flattened view of the outline of the <code>Shape</code>
0N/A * object's geometry.
0N/A * <p>
0N/A * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
0N/A * be returned by the iterator.
0N/A * <p>
0N/A * The amount of subdivision of the curved segments is controlled
0N/A * by the <code>flatness</code> parameter, which specifies the
0N/A * maximum distance that any point on the unflattened transformed
0N/A * curve can deviate from the returned flattened path segments.
0N/A * An optional {@link AffineTransform} can
0N/A * be specified so that the coordinates returned in the iteration are
0N/A * transformed accordingly.
0N/A * @param at an optional <code>AffineTransform</code> to be applied to the
0N/A * coordinates as they are returned in the iteration,
0N/A * or <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
0N/A * from any point on the original curve
0N/A * @return a <code>PathIterator</code> object that provides access to
0N/A * the <code>Shape</code> object's flattened geometry.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at, double flatness) {
0N/A return new FlatteningPathIterator(getPathIterator(at), flatness);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new object of the same class and with the same
0N/A * contents as this object.
0N/A * @return a clone of this instance.
0N/A * @exception OutOfMemoryError if there is not enough memory.
0N/A * @see java.lang.Cloneable
0N/A * @since 1.2
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A}