package java.awt.geom;
import java.awt.Rectangle;
import java.awt.Shape;
public final class GeneralPath implements Shape, Cloneable
{
public static final int WIND_EVEN_ODD = PathIterator.WIND_EVEN_ODD;
public static final int WIND_NON_ZERO = PathIterator.WIND_NON_ZERO;
private static final int INIT_SIZE = 20;
private int rule;
private byte[] types;
private float[] points;
private int subpath = -1;
private int index;
public GeneralPath()
{
this(WIND_NON_ZERO, INIT_SIZE);
}
public GeneralPath(int rule)
{
this(rule, INIT_SIZE);
}
public GeneralPath(int rule, int capacity)
{
if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO)
throw new IllegalArgumentException();
this.rule = rule;
if (capacity < INIT_SIZE)
capacity = INIT_SIZE;
types = new byte[capacity >> 1];
points = new float[capacity];
}
public GeneralPath(Shape s)
{
types = new byte[INIT_SIZE >> 1];
points = new float[INIT_SIZE];
PathIterator pi = s.getPathIterator(null);
setWindingRule(pi.getWindingRule());
append(pi, false);
}
public void moveTo(float x, float y)
{
subpath = index;
ensureSize(index + 2);
types[index >> 1] = PathIterator.SEG_MOVETO;
points[index++] = x;
points[index++] = y;
}
public void lineTo(float x, float y)
{
ensureSize(index + 2);
types[index >> 1] = PathIterator.SEG_LINETO;
points[index++] = x;
points[index++] = y;
}
public void quadTo(float x1, float y1, float x2, float y2)
{
ensureSize(index + 4);
types[index >> 1] = PathIterator.SEG_QUADTO;
points[index++] = x1;
points[index++] = y1;
points[index++] = x2;
points[index++] = y2;
}
public void curveTo(float x1, float y1, float x2, float y2,
float x3, float y3)
{
ensureSize(index + 6);
types[index >> 1] = PathIterator.SEG_CUBICTO;
points[index++] = x1;
points[index++] = y1;
points[index++] = x2;
points[index++] = y2;
points[index++] = x3;
points[index++] = y3;
}
public void closePath()
{
ensureSize(index + 2);
types[index >> 1] = PathIterator.SEG_CLOSE;
points[index++] = points[subpath];
points[index++] = points[subpath + 1];
}
public void append(Shape s, boolean connect)
{
append(s.getPathIterator(null), connect);
}
public void append(PathIterator iter, boolean connect)
{
float[] f = new float[6];
while (!iter.isDone())
{
switch (iter.currentSegment(f))
{
case PathIterator.SEG_MOVETO:
if (!connect || (index == 0))
{
moveTo(f[0], f[1]);
break;
}
if ((index >= 2) && (types[(index - 2) >> 2] == PathIterator.SEG_CLOSE)
&& (f[0] == points[index - 2]) && (f[1] == points[index - 1]))
break;
case PathIterator.SEG_LINETO:
lineTo(f[0], f[1]);
break;
case PathIterator.SEG_QUADTO:
quadTo(f[0], f[1], f[2], f[3]);
break;
case PathIterator.SEG_CUBICTO:
curveTo(f[0], f[1], f[2], f[3], f[4], f[5]);
break;
case PathIterator.SEG_CLOSE:
closePath();
break;
}
connect = false;
iter.next();
}
}
public int getWindingRule()
{
return rule;
}
public void setWindingRule(int rule)
{
if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO)
throw new IllegalArgumentException();
this.rule = rule;
}
public Point2D getCurrentPoint()
{
if (subpath < 0)
return null;
return new Point2D.Float(points[index - 2], points[index - 1]);
}
public void reset()
{
subpath = -1;
index = 0;
}
public void transform(AffineTransform xform)
{
xform.transform(points, 0, points, 0, index >> 1);
}
public Shape createTransformedShape(AffineTransform xform)
{
GeneralPath p = new GeneralPath(this);
p.transform(xform);
return p;
}
public Rectangle getBounds()
{
return getBounds2D().getBounds();
}
public Rectangle2D getBounds2D()
{
throw new Error("not implemented");
}
public boolean contains(double x, double y)
{
throw new Error("not implemented");
}
public boolean contains(Point2D p)
{
return contains(p.getX(), p.getY());
}
public boolean contains(double x, double y, double w, double h)
{
throw new Error("not implemented");
}
public boolean contains(Rectangle2D r)
{
return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
public boolean intersects(double x, double y, double w, double h)
{
throw new Error("not implemented");
}
public boolean intersects(Rectangle2D r)
{
return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
private static class GeneralPathIterator
implements PathIterator
{
private static final int[] NUM_COORDS =
{
2,
2,
4,
6,
0
};
private final GeneralPath path;
private final AffineTransform transform;
private int pos;
GeneralPathIterator(GeneralPath path, AffineTransform transform)
{
this.path = path;
this.transform = transform;
}
public int getWindingRule()
{
return path.rule;
}
public boolean isDone()
{
return pos >= path.index;
}
public void next()
{
int seg;
seg = path.types[pos >> 1];
if (seg == SEG_CLOSE)
pos += 2;
else
pos += NUM_COORDS[seg];
}
public int currentSegment(float[] coords)
{
int seg, numCoords;
seg = path.types[pos >> 1];
numCoords = NUM_COORDS[seg];
if (numCoords > 0)
{
if (transform == null)
System.arraycopy(path.points, pos, coords, 0, numCoords);
else
transform.transform( path.points, pos,
coords, 0,
numCoords >> 1);
}
return seg;
}
public int currentSegment(double[] coords)
{
int seg, numCoords;
seg = path.types[pos >> 1];
numCoords = NUM_COORDS[seg];
if (numCoords > 0)
{
if (transform == null)
{
for (int i = 0; i < numCoords; i++)
coords[i] = (double) path.points[pos + i];
}
else
transform.transform( path.points, pos,
coords, 0,
numCoords >> 1);
}
return seg;
}
}
public PathIterator getPathIterator(AffineTransform at)
{
return new GeneralPathIterator(this, at);
}
public PathIterator getPathIterator(AffineTransform at, double flatness)
{
return new FlatteningPathIterator(getPathIterator(at), flatness);
}
public Object clone()
{
return new GeneralPath(this);
}
private void ensureSize(int size)
{
if (subpath < 0)
throw new IllegalPathStateException("need initial moveto");
if (size <= points.length)
return;
byte[] b = new byte[points.length];
System.arraycopy(types, 0, b, 0, index >> 1);
types = b;
float[] f = new float[points.length << 1];
System.arraycopy(points, 0, f, 0, index);
points = f;
}
}