RoundRectangle2Dpublic abstract class RoundRectangle2D extends RectangularShape The RoundRectangle2D class defines a rectangle with
rounded corners defined by a location {@code (x,y)}, a
dimension {@code (w x h)}, and the width and height of an arc
with which to round the corners.
This class is the abstract superclass for all objects that
store a 2D rounded rectangle.
The actual storage representation of the coordinates is left to
the subclass. |
Constructors Summary |
---|
protected RoundRectangle2D()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.
|
Methods Summary |
---|
private int | classify(double coord, double left, double right, double arcsize)
if (coord < left) {
return 0;
} else if (coord < left + arcsize) {
return 1;
} else if (coord < right - arcsize) {
return 2;
} else if (coord < right) {
return 3;
} else {
return 4;
}
| public boolean | contains(double x, double y, double w, double h){@inheritDoc}
if (isEmpty() || w <= 0 || h <= 0) {
return false;
}
return (contains(x, y) &&
contains(x + w, y) &&
contains(x, y + h) &&
contains(x + w, y + h));
| public boolean | contains(double x, double y){@inheritDoc}
if (isEmpty()) {
return false;
}
double rrx0 = getX();
double rry0 = getY();
double rrx1 = rrx0 + getWidth();
double rry1 = rry0 + getHeight();
// Check for trivial rejection - point is outside bounding rectangle
if (x < rrx0 || y < rry0 || x >= rrx1 || y >= rry1) {
return false;
}
double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0;
double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0;
// Check which corner point is in and do circular containment
// test - otherwise simple acceptance
if (x >= (rrx0 += aw) && x < (rrx0 = rrx1 - aw)) {
return true;
}
if (y >= (rry0 += ah) && y < (rry0 = rry1 - ah)) {
return true;
}
x = (x - rrx0) / aw;
y = (y - rry0) / ah;
return (x * x + y * y <= 1.0);
| public boolean | equals(java.lang.Object obj)Determines whether or not the specified Object is
equal to this RoundRectangle2D . The specified
Object is equal to this RoundRectangle2D
if it is an instance of RoundRectangle2D and if its
location, size, and corner arc dimensions are the same as this
RoundRectangle2D .
if (obj == this) {
return true;
}
if (obj instanceof RoundRectangle2D) {
RoundRectangle2D rr2d = (RoundRectangle2D) obj;
return ((getX() == rr2d.getX()) &&
(getY() == rr2d.getY()) &&
(getWidth() == rr2d.getWidth()) &&
(getHeight() == rr2d.getHeight()) &&
(getArcWidth() == rr2d.getArcWidth()) &&
(getArcHeight() == rr2d.getArcHeight()));
}
return false;
| public abstract double | getArcHeight()Gets the height of the arc that rounds off the corners.
| public abstract double | getArcWidth()Gets the width of the arc that rounds off the corners.
| public java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform at)Returns an iteration object that defines the boundary of this
RoundRectangle2D .
The iterator for this class is multi-threaded safe, which means
that this RoundRectangle2D class guarantees that
modifications to the geometry of this RoundRectangle2D
object do not affect any iterations of that geometry that
are already in process.
return new RoundRectIterator(this, at);
| public int | hashCode()Returns the hashcode for this RoundRectangle2D .
long bits = java.lang.Double.doubleToLongBits(getX());
bits += java.lang.Double.doubleToLongBits(getY()) * 37;
bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
bits += java.lang.Double.doubleToLongBits(getArcWidth()) * 53;
bits += java.lang.Double.doubleToLongBits(getArcHeight()) * 59;
return (((int) bits) ^ ((int) (bits >> 32)));
| public boolean | intersects(double x, double y, double w, double h){@inheritDoc}
if (isEmpty() || w <= 0 || h <= 0) {
return false;
}
double rrx0 = getX();
double rry0 = getY();
double rrx1 = rrx0 + getWidth();
double rry1 = rry0 + getHeight();
// Check for trivial rejection - bounding rectangles do not intersect
if (x + w <= rrx0 || x >= rrx1 || y + h <= rry0 || y >= rry1) {
return false;
}
double aw = Math.min(getWidth(), Math.abs(getArcWidth())) / 2.0;
double ah = Math.min(getHeight(), Math.abs(getArcHeight())) / 2.0;
int x0class = classify(x, rrx0, rrx1, aw);
int x1class = classify(x + w, rrx0, rrx1, aw);
int y0class = classify(y, rry0, rry1, ah);
int y1class = classify(y + h, rry0, rry1, ah);
// Trivially accept if any point is inside inner rectangle
if (x0class == 2 || x1class == 2 || y0class == 2 || y1class == 2) {
return true;
}
// Trivially accept if either edge spans inner rectangle
if ((x0class < 2 && x1class > 2) || (y0class < 2 && y1class > 2)) {
return true;
}
// Since neither edge spans the center, then one of the corners
// must be in one of the rounded edges. We detect this case if
// a [xy]0class is 3 or a [xy]1class is 1. One of those two cases
// must be true for each direction.
// We now find a "nearest point" to test for being inside a rounded
// corner.
x = (x1class == 1) ? (x = x + w - (rrx0 + aw)) : (x = x - (rrx1 - aw));
y = (y1class == 1) ? (y = y + h - (rry0 + ah)) : (y = y - (rry1 - ah));
x = x / aw;
y = y / ah;
return (x * x + y * y <= 1.0);
| public void | setFrame(double x, double y, double w, double h){@inheritDoc}
setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
| public abstract void | setRoundRect(double x, double y, double w, double h, double arcWidth, double arcHeight)Sets the location, size, and corner radii of this
RoundRectangle2D to the specified
double values.
| public void | setRoundRect(java.awt.geom.RoundRectangle2D rr)Sets this RoundRectangle2D to be the same as the
specified RoundRectangle2D .
setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
rr.getArcWidth(), rr.getArcHeight());
|
|