RoundRectangle2D.java [plain text]
package java.awt.geom;
import java.util.NoSuchElementException;
public abstract class RoundRectangle2D extends RectangularShape
{
public abstract double getArcHeight();
public abstract double getArcWidth();
public abstract void setRoundRect(double x, double y, double w, double h,
double arcWidth, double arcHeight);
protected RoundRectangle2D()
{
}
public boolean contains(double x, double y)
{
double mx = getX();
double mw = getWidth();
if (x < mx || x >= mx + mw)
return false;
double my = getY();
double mh = getHeight();
if (y < my || y >= my + mh)
return false;
double dy = Math.min(Math.abs(my - y), Math.abs(my + mh - y));
double dx = Math.min(Math.abs(mx - x), Math.abs(mx + mw - x));
double aw = getArcWidth() / 2.0;
double ah = getArcHeight() / 2.0;
if (dx > aw || dy > ah)
return true;
dy = (ah - dy) / ah;
dx = (aw - dx) / aw;
return dx * dx + dy * dy <= 1.0;
}
public boolean contains(double x, double y, double w, double h)
{
return (contains(x, y) && contains(x, y + h) && contains(x + w, y + h)
&& contains(x + w, y));
}
public PathIterator getPathIterator(final AffineTransform at)
{
final double minx = getX();
final double miny = getY();
final double maxx = minx + getWidth();
final double maxy = miny + getHeight();
final double arcwidth = getArcWidth();
final double archeight = getArcHeight();
return new PathIterator()
{
private int current = 0;
private PathIterator corner;
private Arc2D arc = new Arc2D.Double();
private double[] temp = new double[2];
public int getWindingRule()
{
return WIND_NON_ZERO;
}
public boolean isDone()
{
return current > 9;
}
private void getPoint(int val)
{
switch (val)
{
case 0:
case 8:
temp[0] = maxx;
temp[1] = miny + archeight;
break;
case 7:
temp[0] = maxx;
temp[1] = maxy - archeight;
break;
case 6:
temp[0] = maxx - arcwidth;
temp[1] = maxy;
break;
case 5:
temp[0] = minx + arcwidth;
temp[1] = maxy;
break;
case 4:
temp[0] = minx;
temp[1] = maxy - archeight;
break;
case 3:
temp[0] = minx;
temp[1] = miny + archeight;
break;
case 2:
temp[0] = minx + arcwidth;
temp[1] = miny;
break;
case 1:
temp[0] = maxx - arcwidth;
temp[1] = miny;
break;
}
}
public void next()
{
if (current >= 8)
++current;
else if (corner != null)
{
corner.next();
if (corner.isDone())
{
corner = null;
++current;
}
}
else
{
getPoint(current);
double x1 = temp[0];
double y1 = temp[1];
getPoint(current + 1);
Rectangle2D.Double r = new Rectangle2D.Double(Math.min(x1,
temp[0]),
Math.min(y1,
temp[1]),
Math.abs(x1
- temp[0]),
Math.abs(y1
- temp[1]));
arc.setArc(r, (current >> 1) * 90.0, 90.0, Arc2D.OPEN);
corner = arc.getPathIterator(at);
}
}
public int currentSegment(float[] coords)
{
if (corner != null)
{
int r = corner.currentSegment(coords);
if (r == SEG_MOVETO)
r = SEG_LINETO;
return r;
}
if (current < 9)
{
getPoint(current);
coords[0] = (float) temp[0];
coords[1] = (float) temp[1];
}
else if (current == 9)
return SEG_CLOSE;
else
throw new NoSuchElementException("rect iterator out of bounds");
if (at != null)
at.transform(coords, 0, coords, 0, 1);
return current == 0 ? SEG_MOVETO : SEG_LINETO;
}
public int currentSegment(double[] coords)
{
if (corner != null)
{
int r = corner.currentSegment(coords);
if (r == SEG_MOVETO)
r = SEG_LINETO;
return r;
}
if (current < 9)
{
getPoint(current);
coords[0] = temp[0];
coords[1] = temp[1];
}
else if (current == 9)
return SEG_CLOSE;
else
throw new NoSuchElementException("rect iterator out of bounds");
if (at != null)
at.transform(coords, 0, coords, 0, 1);
return current == 0 ? SEG_MOVETO : SEG_LINETO;
}
};
}
public boolean intersects(double x, double y, double w, double h)
{
return (contains(x, y) || contains(x, y + h) || contains(x + w, y + h)
|| contains(x + w, y));
}
public void setFrame(double x, double y, double w, double h)
{
setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
}
public void setRoundRect(RoundRectangle2D rr)
{
setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
rr.getArcWidth(), rr.getArcHeight());
}
public static class Double extends RoundRectangle2D
{
public double archeight;
public double arcwidth;
public double x;
public double y;
public double width;
public double height;
public Double()
{
}
public Double(double x, double y, double w, double h, double arcWidth,
double arcHeight)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.arcwidth = arcWidth;
this.archeight = arcHeight;
}
public double getArcHeight()
{
return archeight;
}
public double getArcWidth()
{
return arcwidth;
}
public Rectangle2D getBounds2D()
{
return new Rectangle2D.Double(x, y, width, height);
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public boolean isEmpty()
{
return width <= 0 || height <= 0;
}
public void setRoundRect(double x, double y, double w, double h,
double arcWidth, double arcHeight)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.arcwidth = arcWidth;
this.archeight = arcHeight;
}
}
public static class Float extends RoundRectangle2D
{
public float archeight;
public float arcwidth;
public float x;
public float y;
public float width;
public float height;
public Float()
{
}
public Float(float x, float y, float w, float h, float arcWidth,
float arcHeight)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.arcwidth = arcWidth;
this.archeight = arcHeight;
}
public double getArcHeight()
{
return archeight;
}
public double getArcWidth()
{
return arcwidth;
}
public Rectangle2D getBounds2D()
{
return new Rectangle2D.Float(x, y, width, height);
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public boolean isEmpty()
{
return width <= 0 || height <= 0;
}
public void setRoundRect(float x, float y, float w, float h,
float arcWidth, float arcHeight)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.arcwidth = arcWidth;
this.archeight = arcHeight;
}
public void setRoundRect(double x, double y, double w, double h,
double arcWidth, double arcHeight)
{
this.x = (float) x;
this.y = (float) y;
this.width = (float) w;
this.height = (float) h;
this.arcwidth = (float) arcWidth;
this.archeight = (float) arcHeight;
}
} }