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_QUADTO;
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 i, boolean connect)
{
float[] f = new float[6];
while (! i.isDone())
{
int result = i.currentSegment(f);
switch (result)
{
case PathIterator.SEG_MOVETO:
if (! connect)
{
moveTo(f[0], f[1]);
break;
}
if (subpath >= 0 && f[0] == points[subpath]
&& f[1] == points[subpath + 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;
default:
closePath();
}
connect = false;
}
}
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[subpath], points[subpath + 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());
}
public PathIterator getPathIterator(final AffineTransform at)
{
return new PathIterator()
{
int current = 0;
public int getWindingRule()
{
return rule;
}
public boolean isDone()
{
return current >= index;
}
public void next()
{
current++;
}
public int currentSegment(float[] coords)
{
if (current >= index)
return SEG_CLOSE;
int result = types[current >> 1];
int i = 0;
if (result == 3)
{
coords[i++] = points[current++];
coords[i++] = points[current++];
}
if (result == 2)
{
coords[i++] = points[current++];
coords[i++] = points[current++];
}
if (result < 2)
{
coords[i++] = points[current++];
coords[i++] = points[current++];
if (at != null)
at.transform(coords, 0, coords, 0, result == 0 ? 1 : result);
}
return result;
}
public int currentSegment(double[] coords)
{
if (current >= index)
return SEG_CLOSE;
int result = types[current >> 1];
int i = 0;
if (result == 3)
{
coords[i++] = points[current++];
coords[i++] = points[current++];
}
if (result == 2)
{
coords[i++] = points[current++];
coords[i++] = points[current++];
}
if (result < 2)
{
coords[i++] = points[current++];
coords[i++] = points[current++];
if (at != null)
at.transform(coords, 0, coords, 0, result == 0 ? 1 : result);
}
return result;
}
};
}
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;
}
}