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>RoundRectangle2D</code> class defines a rectangle with
0N/A * rounded corners defined by a location {@code (x,y)}, a
0N/A * dimension {@code (w x h)}, and the width and height of an arc
0N/A * with which to round the corners.
0N/A * <p>
0N/A * This class is the abstract superclass for all objects that
0N/A * store a 2D rounded 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 RoundRectangle2D extends RectangularShape {
0N/A
0N/A /**
0N/A * The <code>Float</code> class defines a rectangle with rounded
0N/A * corners all specified in <code>float</code> coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Float extends RoundRectangle2D
0N/A implements Serializable
0N/A {
0N/A /**
0N/A * The X coordinate of this <code>RoundRectangle2D</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>RoundRectangle2D</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>RoundRectangle2D</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>RoundRectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float height;
0N/A
0N/A /**
0N/A * The width of the arc that rounds off the corners.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float arcwidth;
0N/A
0N/A /**
0N/A * The height of the arc that rounds off the corners.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float archeight;
0N/A
0N/A /**
0N/A * Constructs a new <code>RoundRectangle2D</code>, initialized to
0N/A * location (0.0,&nbsp;0.0), size (0.0,&nbsp;0.0), and corner arcs
0N/A * of radius 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>RoundRectangle2D</code>
0N/A * from the specified <code>float</code> coordinates.
0N/A *
0N/A * @param x the X coordinate of the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param y the Y coordinate of the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param w the width to which to set the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param h the height to which to set the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param arcw the width of the arc to use to round off the
0N/A * corners of the newly constructed
0N/A * <code>RoundRectangle2D</code>
0N/A * @param arch the height of the arc to use to round off the
0N/A * corners of the newly constructed
0N/A * <code>RoundRectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public Float(float x, float y, float w, float h,
0N/A float arcw, float arch)
0N/A {
0N/A setRoundRect(x, y, w, h, arcw, arch);
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 double getArcWidth() {
0N/A return (double) arcwidth;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getArcHeight() {
0N/A return (double) archeight;
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, size, and corner radii of this
0N/A * <code>RoundRectangle2D</code> to the specified
0N/A * <code>float</code> values.
0N/A *
0N/A * @param x the X coordinate to which to set the
0N/A * location of this <code>RoundRectangle2D</code>
0N/A * @param y the Y coordinate to which to set the
0N/A * location of this <code>RoundRectangle2D</code>
0N/A * @param w the width to which to set this
0N/A * <code>RoundRectangle2D</code>
0N/A * @param h the height to which to set this
0N/A * <code>RoundRectangle2D</code>
0N/A * @param arcw the width to which to set the arc of this
0N/A * <code>RoundRectangle2D</code>
0N/A * @param arch the height to which to set the arc of this
0N/A * <code>RoundRectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setRoundRect(float x, float y, float w, float h,
0N/A float arcw, float arch)
0N/A {
0N/A this.x = x;
0N/A this.y = y;
0N/A this.width = w;
0N/A this.height = h;
0N/A this.arcwidth = arcw;
0N/A this.archeight = arch;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRoundRect(double x, double y, double w, double h,
0N/A double arcw, double arch)
0N/A {
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 this.arcwidth = (float) arcw;
0N/A this.archeight = (float) arch;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRoundRect(RoundRectangle2D rr) {
0N/A this.x = (float) rr.getX();
0N/A this.y = (float) rr.getY();
0N/A this.width = (float) rr.getWidth();
0N/A this.height = (float) rr.getHeight();
0N/A this.arcwidth = (float) rr.getArcWidth();
0N/A this.archeight = (float) rr.getArcHeight();
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 Rectangle2D.Float(x, y, width, height);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -3423150618393866922L;
0N/A }
0N/A
0N/A /**
0N/A * The <code>Double</code> class defines a rectangle with rounded
0N/A * corners all specified in <code>double</code> coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Double extends RoundRectangle2D
0N/A implements Serializable
0N/A {
0N/A /**
0N/A * The X coordinate of this <code>RoundRectangle2D</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>RoundRectangle2D</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>RoundRectangle2D</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>RoundRectangle2D</code>.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double height;
0N/A
0N/A /**
0N/A * The width of the arc that rounds off the corners.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double arcwidth;
0N/A
0N/A /**
0N/A * The height of the arc that rounds off the corners.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double archeight;
0N/A
0N/A /**
0N/A * Constructs a new <code>RoundRectangle2D</code>, initialized to
0N/A * location (0.0,&nbsp;0.0), size (0.0,&nbsp;0.0), and corner arcs
0N/A * of radius 0.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>RoundRectangle2D</code>
0N/A * from the specified <code>double</code> coordinates.
0N/A *
0N/A * @param x the X coordinate of the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param y the Y coordinate of the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param w the width to which to set the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param h the height to which to set the newly
0N/A * constructed <code>RoundRectangle2D</code>
0N/A * @param arcw the width of the arc to use to round off the
0N/A * corners of the newly constructed
0N/A * <code>RoundRectangle2D</code>
0N/A * @param arch the height of the arc to use to round off the
0N/A * corners of the newly constructed
0N/A * <code>RoundRectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public Double(double x, double y, double w, double h,
0N/A double arcw, double arch)
0N/A {
0N/A setRoundRect(x, y, w, h, arcw, arch);
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 double getArcWidth() {
0N/A return arcwidth;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getArcHeight() {
0N/A return archeight;
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 * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRoundRect(double x, double y, double w, double h,
0N/A double arcw, double arch)
0N/A {
0N/A this.x = x;
0N/A this.y = y;
0N/A this.width = w;
0N/A this.height = h;
0N/A this.arcwidth = arcw;
0N/A this.archeight = arch;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setRoundRect(RoundRectangle2D rr) {
0N/A this.x = rr.getX();
0N/A this.y = rr.getY();
0N/A this.width = rr.getWidth();
0N/A this.height = rr.getHeight();
0N/A this.arcwidth = rr.getArcWidth();
0N/A this.archeight = rr.getArcHeight();
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 Rectangle2D.Double(x, y, width, height);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 1048939333485206117L;
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.RoundRectangle2D.Float
0N/A * @see java.awt.geom.RoundRectangle2D.Double
0N/A * @since 1.2
0N/A */
0N/A protected RoundRectangle2D() {
0N/A }
0N/A
0N/A /**
0N/A * Gets the width of the arc that rounds off the corners.
0N/A * @return the width of the arc that rounds off the corners
0N/A * of this <code>RoundRectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getArcWidth();
0N/A
0N/A /**
0N/A * Gets the height of the arc that rounds off the corners.
0N/A * @return the height of the arc that rounds off the corners
0N/A * of this <code>RoundRectangle2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getArcHeight();
0N/A
0N/A /**
0N/A * Sets the location, size, and corner radii of this
0N/A * <code>RoundRectangle2D</code> to the specified
0N/A * <code>double</code> values.
0N/A *
0N/A * @param x the X coordinate to which to set the
0N/A * location of this <code>RoundRectangle2D</code>
0N/A * @param y the Y coordinate to which to set the
0N/A * location of this <code>RoundRectangle2D</code>
0N/A * @param w the width to which to set this
0N/A * <code>RoundRectangle2D</code>
0N/A * @param h the height to which to set this
0N/A * <code>RoundRectangle2D</code>
0N/A * @param arcWidth the width to which to set the arc of this
0N/A * <code>RoundRectangle2D</code>
0N/A * @param arcHeight the height to which to set the arc of this
0N/A * <code>RoundRectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public abstract void setRoundRect(double x, double y, double w, double h,
0N/A double arcWidth, double arcHeight);
0N/A
0N/A /**
0N/A * Sets this <code>RoundRectangle2D</code> to be the same as the
0N/A * specified <code>RoundRectangle2D</code>.
0N/A * @param rr the specified <code>RoundRectangle2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setRoundRect(RoundRectangle2D rr) {
0N/A setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
0N/A rr.getArcWidth(), rr.getArcHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setFrame(double x, double y, double w, double h) {
0N/A setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
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 if (isEmpty()) {
0N/A return false;
0N/A }
0N/A double rrx0 = getX();
0N/A double rry0 = getY();
0N/A double rrx1 = rrx0 + getWidth();
0N/A double rry1 = rry0 + getHeight();
0N/A // Check for trivial rejection - point is outside bounding rectangle
0N/A if (x < rrx0 || y < rry0 || x >= rrx1 || y >= rry1) {
0N/A return false;
0N/A }
0N/A double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0;
0N/A double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0;
0N/A // Check which corner point is in and do circular containment
0N/A // test - otherwise simple acceptance
0N/A if (x >= (rrx0 += aw) && x < (rrx0 = rrx1 - aw)) {
0N/A return true;
0N/A }
0N/A if (y >= (rry0 += ah) && y < (rry0 = rry1 - ah)) {
0N/A return true;
0N/A }
0N/A x = (x - rrx0) / aw;
0N/A y = (y - rry0) / ah;
0N/A return (x * x + y * y <= 1.0);
0N/A }
0N/A
0N/A private int classify(double coord, double left, double right,
0N/A double arcsize)
0N/A {
0N/A if (coord < left) {
0N/A return 0;
0N/A } else if (coord < left + arcsize) {
0N/A return 1;
0N/A } else if (coord < right - arcsize) {
0N/A return 2;
0N/A } else if (coord < right) {
0N/A return 3;
0N/A } else {
0N/A return 4;
0N/A }
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 rrx0 = getX();
0N/A double rry0 = getY();
0N/A double rrx1 = rrx0 + getWidth();
0N/A double rry1 = rry0 + getHeight();
0N/A // Check for trivial rejection - bounding rectangles do not intersect
0N/A if (x + w <= rrx0 || x >= rrx1 || y + h <= rry0 || y >= rry1) {
0N/A return false;
0N/A }
0N/A double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0;
0N/A double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0;
0N/A int x0class = classify(x, rrx0, rrx1, aw);
0N/A int x1class = classify(x + w, rrx0, rrx1, aw);
0N/A int y0class = classify(y, rry0, rry1, ah);
0N/A int y1class = classify(y + h, rry0, rry1, ah);
0N/A // Trivially accept if any point is inside inner rectangle
0N/A if (x0class == 2 || x1class == 2 || y0class == 2 || y1class == 2) {
0N/A return true;
0N/A }
0N/A // Trivially accept if either edge spans inner rectangle
0N/A if ((x0class < 2 && x1class > 2) || (y0class < 2 && y1class > 2)) {
0N/A return true;
0N/A }
0N/A // Since neither edge spans the center, then one of the corners
0N/A // must be in one of the rounded edges. We detect this case if
0N/A // a [xy]0class is 3 or a [xy]1class is 1. One of those two cases
0N/A // must be true for each direction.
0N/A // We now find a "nearest point" to test for being inside a rounded
0N/A // corner.
0N/A x = (x1class == 1) ? (x = x + w - (rrx0 + aw)) : (x = x - (rrx1 - aw));
0N/A y = (y1class == 1) ? (y = y + h - (rry0 + ah)) : (y = y - (rry1 - ah));
0N/A x = x / aw;
0N/A y = y / ah;
0N/A return (x * x + y * y <= 1.0);
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 return (contains(x, y) &&
0N/A contains(x + w, y) &&
0N/A contains(x, y + h) &&
0N/A contains(x + w, y + h));
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of this
0N/A * <code>RoundRectangle2D</code>.
0N/A * The iterator for this class is multi-threaded safe, which means
0N/A * that this <code>RoundRectangle2D</code> class guarantees that
0N/A * modifications to the geometry of this <code>RoundRectangle2D</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>RoundRectangle2D</code>, one segment at a time.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at) {
0N/A return new RoundRectIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this <code>RoundRectangle2D</code>.
0N/A * @return the hashcode for this <code>RoundRectangle2D</code>.
0N/A * @since 1.6
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 bits += java.lang.Double.doubleToLongBits(getArcWidth()) * 53;
0N/A bits += java.lang.Double.doubleToLongBits(getArcHeight()) * 59;
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>RoundRectangle2D</code>. The specified
0N/A * <code>Object</code> is equal to this <code>RoundRectangle2D</code>
0N/A * if it is an instance of <code>RoundRectangle2D</code> and if its
0N/A * location, size, and corner arc dimensions are the same as this
0N/A * <code>RoundRectangle2D</code>.
0N/A * @param obj an <code>Object</code> to be compared with this
0N/A * <code>RoundRectangle2D</code>.
0N/A * @return <code>true</code> if <code>obj</code> is an instance
0N/A * of <code>RoundRectangle2D</code> and has the same values;
0N/A * <code>false</code> otherwise.
0N/A * @since 1.6
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 RoundRectangle2D) {
0N/A RoundRectangle2D rr2d = (RoundRectangle2D) obj;
0N/A return ((getX() == rr2d.getX()) &&
0N/A (getY() == rr2d.getY()) &&
0N/A (getWidth() == rr2d.getWidth()) &&
0N/A (getHeight() == rr2d.getHeight()) &&
0N/A (getArcWidth() == rr2d.getArcWidth()) &&
0N/A (getArcHeight() == rr2d.getArcHeight()));
0N/A }
0N/A return false;
0N/A }
0N/A}