diff --git a/core/src/main/java/org/piccolo2d/nodes/PPath.java b/core/src/main/java/org/piccolo2d/nodes/PPath.java index 7cea604..f68fd02 100644 --- a/core/src/main/java/org/piccolo2d/nodes/PPath.java +++ b/core/src/main/java/org/piccolo2d/nodes/PPath.java @@ -37,6 +37,7 @@ import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Path2D; +import java.awt.geom.Point2D; import java.awt.geom.QuadCurve2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; @@ -283,14 +284,57 @@ return new PPath.Float(new Line2D.Float(x1, y1, x2, y2)); } - /* - need setPathToPolyline + /** + * Create and return a new path node with a shape defined by the specified line segments in single + * precision floating point coordinates. + * + * @param xp array of x coordinates, must contain at least one x coordinate + * @param yp array of y coordinates, must contain at least one y coordinate + * @return a new path node with the a shape defined by the specified line segments in single + * precision floating point coordinates + */ public static final PPath createPolyline(final float[] xp, final float[] yp) { + if (xp.length < 1) { + throw new IllegalArgumentException("xp must contain at least one x coordinate"); + } + if (yp.length < 1) { + throw new IllegalArgumentException("yp must contain at least one x coordinate"); + } + if (xp.length != yp.length) { + throw new IllegalArgumentException("xp and yp must contain the same number of coordinates"); + } + Path2D.Float path = new Path2D.Float(); + path.moveTo(xp[0], yp[0]); + for (int i = 1; i < xp.length; i++) { + path.lineTo(xp[i], yp[i]); + } + path.closePath(); + return new PPath.Float(path); } + /** + * Create and return a new path node with a shape defined by the specified line segments in single + * precision floating point coordinates. + * + * @param points array of points, must not be null and must contain at least one point + * @return a new path node with the a shape defined by the specified line segments in single + * precision floating point coordinates + */ public static final PPath createPolyline(final Point2D.Float[] points) { + if (points == null) { + throw new NullPointerException("points must not be null"); + } + if (points.length < 1) { + throw new IllegalArgumentException("points must contain at least one point"); + } + Path2D.Float path = new Path2D.Float(); + path.moveTo(points[0].getX(), points[0].getY()); + for (int i = 1; i < points.length; i++) { + path.lineTo(points[i].getX(), points[i].getY()); + } + path.closePath(); + return new PPath.Float(path); } - */ /** * Create and return a new path node with the specified quadratic curve in single @@ -668,4 +712,4 @@ protected final void transform(final AffineTransform transform) { path.transform(transform); } -} \ No newline at end of file +} diff --git a/core/src/test/java/org/piccolo2d/nodes/AbstractPPathTest.java b/core/src/test/java/org/piccolo2d/nodes/AbstractPPathTest.java index a11064e..8b53c5a 100644 --- a/core/src/test/java/org/piccolo2d/nodes/AbstractPPathTest.java +++ b/core/src/test/java/org/piccolo2d/nodes/AbstractPPathTest.java @@ -156,6 +156,62 @@ assertNotNull(PPath.createLine(0.0d, 0.0d, 50.0d, 100.0d)); } + public void testCreatePolylineFloatArraysEmpty() { + try { + PPath.createPolyline(new float[0], new float[0]); + fail("expected IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + // expected + } + } + + public void testCreatePolylineFloatArraysDifferentSizes() { + try { + PPath.createPolyline(new float[0], new float[] { 100.0f }); + fail("expected IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + // expected + } + } + + public void testCreatePolylineFloatArraysDifferentSingle() { + assertNotNull(PPath.createPolyline(new float[] { 100.0f }, new float[] { 100.0f })); + } + + public void testCreatePolylineFloatArrays() { + assertNotNull(PPath.createPolyline(new float[] { 100.0f, 100.0f, 200.0f }, new float[] { 100.0f, 200.0f, 200.0f })); + } + + public void testCreatePolylinePoint2DFloatArrayNull() { + try { + PPath.createPolyline((Point2D.Float[]) null); + fail("createPolyline(null) expected NullPointerException"); + } + catch (NullPointerException e) { + // expected + } + } + + public void testCreatePolylinePoint2DFloatArrayEmpty() { + try { + PPath.createPolyline(new Point2D.Float[0]); + fail("expected IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + // expected + } + } + + public void testCreatePolylinePoint2DFloatArraySingle() { + assertNotNull(PPath.createPolyline(new Point2D.Float[] { new Point2D.Float(100.0f, 100.0f) })); + } + + public void testCreatePolylinePoint2DFloatArray() { + assertNotNull(PPath.createPolyline(new Point2D.Float[] { new Point2D.Float(100.0f, 100.0f), new Point2D.Float(100.0f, 200.0f), new Point2D.Float(200.0f, 200.0f) })); + } + public void testCreateQuadCurveDouble() { assertNotNull(PPath.createQuadCurve(0.0d, 0.0d, 25.0d, 75.0d, 50.0d, 100.0d)); }