Java Code Examples for java.awt.geom.PathIterator#SEG_MOVETO

The following examples show how to use java.awt.geom.PathIterator#SEG_MOVETO . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedGeneralPath.java    From blog-codes with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if previous command was a moveto command,
 * skipping a close command (if present).
 */
protected void checkMoveTo()
{
	if (numSeg == 0)
		return;

	switch (types[numSeg - 1])
	{

		case PathIterator.SEG_MOVETO:
			path.moveTo(values[numVals - 2], values[numVals - 1]);
			break;

		case PathIterator.SEG_CLOSE:
			if (numSeg == 1)
				return;
			if (types[numSeg - 2] == PathIterator.SEG_MOVETO)
				path.moveTo(values[numVals - 2], values[numVals - 1]);
			break;

		default:
			break;
	}
}
 
Example 2
Source File: ShapeUtils.java    From pumpernickel with MIT License 6 votes vote down vote up
/**
 * Return true if every subpath in the shape provided is closed.
 * 
 * @param shape
 *            the shape to inspect
 * @return true if every subpath in the shape provided ends with a
 *         SEG_CLOSE.
 */
public static boolean isClosed(Shape shape) {
	PathIterator i = shape.getPathIterator(null);
	float[] coords = new float[6];
	int ctr = 0;
	int lastSegmentType = -1;
	while (!i.isDone()) {
		int segmentType = i.currentSegment(coords);
		if (segmentType == PathIterator.SEG_MOVETO) {
			if (ctr != 0) {
				if (lastSegmentType != PathIterator.SEG_CLOSE)
					return false;
			}
		}
		i.next();
		ctr++;
		lastSegmentType = segmentType;
	}
	if (lastSegmentType != PathIterator.SEG_CLOSE)
		return false;
	return ctr > 0;
}
 
Example 3
Source File: NestWorldMapPane.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
private static GeneralPath areaToPath(final Area negativeArea, final double deltaX, final GeneralPath pixelPath) {

        final float[] floats = new float[6];
        // move to correct rectangle
        final AffineTransform transform = AffineTransform.getTranslateInstance(deltaX, 0.0);
        final PathIterator iterator = negativeArea.getPathIterator(transform);

        while (!iterator.isDone()) {
            final int segmentType = iterator.currentSegment(floats);
            if (segmentType == PathIterator.SEG_LINETO) {
                pixelPath.lineTo(floats[0], floats[1]);
            } else if (segmentType == PathIterator.SEG_MOVETO) {
                pixelPath.moveTo(floats[0], floats[1]);
            } else if (segmentType == PathIterator.SEG_CLOSE) {
                pixelPath.closePath();
            }
            iterator.next();
        }
        return pixelPath;
    }
 
Example 4
Source File: ShapeUtils.java    From pumpernickel with MIT License 5 votes vote down vote up
/** Return true if two shapes are equal. */
public static boolean equals(Shape shape, Shape shape2) {
	PathIterator iter1 = shape.getPathIterator(null);
	PathIterator iter2 = shape.getPathIterator(null);
	double[] coords1 = new double[6];
	double[] coords2 = new double[6];
	while ((!iter1.isDone()) && (!iter2.isDone())) {
		int k1 = iter1.currentSegment(coords1);
		int k2 = iter2.currentSegment(coords2);
		if (k1 != k2)
			return false;
		if (k1 == PathIterator.SEG_MOVETO
				|| k1 == PathIterator.SEG_LINETO) {
			if (!equals(coords1, coords2, 2))
				return false;
		} else if (k1 == PathIterator.SEG_QUADTO) {
			if (!equals(coords1, coords2, 4))
				return false;
		} else if (k1 == PathIterator.SEG_CUBICTO) {
			if (!equals(coords1, coords2, 6))
				return false;
		} else if (k1 == PathIterator.SEG_CLOSE) {
			// do nothing
		} else {
			throw new RuntimeException("unrecognized segment " + k1);
		}

		iter1.next();
		iter2.next();
	}
	return iter1.isDone() && iter2.isDone();
}
 
Example 5
Source File: Geometry.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Transforms the points in a path according to a method.
 *
 * @param it The iterator of the path to change the points on.
 * @param method The method to use to transform the points. Takes a float[2] array with x and y coordinates as parameter.
 * @return The transformed path.
 */
public static GeneralPath transformPath(PathIterator it, Consumer<float[]> method)
{
	GeneralPath path = new GeneralPath();
	float[] coords = new float[2];
	while (!it.isDone())
	{
		int type = it.currentSegment(coords);
		if (type == PathIterator.SEG_MOVETO)
		{
			method.accept(coords);
			path.moveTo(coords[0], coords[1]);
		}
		else if (type == PathIterator.SEG_LINETO)
		{
			method.accept(coords);
			path.lineTo(coords[0], coords[1]);
		}
		else if (type == PathIterator.SEG_CLOSE)
		{
			path.closePath();
		}
		it.next();
	}

	return path;
}
 
Example 6
Source File: SWTGraphics2D.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
 *
 * @param shape  the shape (<code>null</code> not permitted).
 *
 * @return The path.
 */
private Path toSwtPath(Shape shape) {
    int type;
    float[] coords = new float[6];
    Path path = new Path(this.gc.getDevice());
    PathIterator pit = shape.getPathIterator(null);
    while (!pit.isDone()) {
        type = pit.currentSegment(coords);
        switch (type) {
            case (PathIterator.SEG_MOVETO):
                path.moveTo(coords[0], coords[1]);
                break;
            case (PathIterator.SEG_LINETO):
                path.lineTo(coords[0], coords[1]);
                break;
            case (PathIterator.SEG_QUADTO):
                path.quadTo(coords[0], coords[1], coords[2], coords[3]);
                break;
            case (PathIterator.SEG_CUBICTO):
                path.cubicTo(coords[0], coords[1], coords[2],
                        coords[3], coords[4], coords[5]);
                break;
            case (PathIterator.SEG_CLOSE):
                path.close();
                break;
            default:
                break;
        }
        pit.next();
    }
    return path;
}
 
Example 7
Source File: RenderingEngine.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to feed a {@link PathConsumer2D} object from a
 * given {@link PathIterator}.
 * This method deals with the details of running the iterator and
 * feeding the consumer a segment at a time.
 */
public static void feedConsumer(PathIterator pi, PathConsumer2D consumer) {
    float coords[] = new float[6];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            consumer.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            consumer.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            consumer.quadTo(coords[0], coords[1],
                            coords[2], coords[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            consumer.curveTo(coords[0], coords[1],
                             coords[2], coords[3],
                             coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            consumer.closePath();
            break;
        }
        pi.next();
    }
}
 
Example 8
Source File: SWTGraphics2D.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
 *
 * @param shape  the shape (<code>null</code> not permitted).
 *
 * @return The path.
 */
private Path toSwtPath(Shape shape) {
    int type;
    float[] coords = new float[6];
    Path path = new Path(this.gc.getDevice());
    PathIterator pit = shape.getPathIterator(null);
    while (!pit.isDone()) {
        type = pit.currentSegment(coords);
        switch (type) {
            case (PathIterator.SEG_MOVETO):
                path.moveTo(coords[0], coords[1]);
                break;
            case (PathIterator.SEG_LINETO):
                path.lineTo(coords[0], coords[1]);
                break;
            case (PathIterator.SEG_QUADTO):
                path.quadTo(coords[0], coords[1], coords[2], coords[3]);
                break;
            case (PathIterator.SEG_CUBICTO):
                path.cubicTo(coords[0], coords[1], coords[2],
                        coords[3], coords[4], coords[5]);
                break;
            case (PathIterator.SEG_CLOSE):
                path.close();
                break;
            default:
                break;
        }
        pit.next();
    }
    return path;
}
 
Example 9
Source File: Crossings.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static Crossings findCrossings(PathIterator pi,
                                      double xlo, double ylo,
                                      double xhi, double yhi)
{
    Crossings cross;
    if (pi.getWindingRule() == pi.WIND_EVEN_ODD) {
        cross = new EvenOdd(xlo, ylo, xhi, yhi);
    } else {
        cross = new NonZero(xlo, ylo, xhi, yhi);
    }
    // coords array is big enough for holding:
    //     coordinates returned from currentSegment (6)
    //     OR
    //         two subdivided quadratic curves (2+4+4=10)
    //         AND
    //             0-1 horizontal splitting parameters
    //             OR
    //             2 parametric equation derivative coefficients
    //     OR
    //         three subdivided cubic curves (2+6+6+6=20)
    //         AND
    //             0-2 horizontal splitting parameters
    //             OR
    //             3 parametric equation derivative coefficients
    double coords[] = new double[23];
    double movx = 0;
    double movy = 0;
    double curx = 0;
    double cury = 0;
    double newx, newy;
    while (!pi.isDone()) {
        int type = pi.currentSegment(coords);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            if (movy != cury &&
                cross.accumulateLine(curx, cury, movx, movy))
            {
                return null;
            }
            movx = curx = coords[0];
            movy = cury = coords[1];
            break;
        case PathIterator.SEG_LINETO:
            newx = coords[0];
            newy = coords[1];
            if (cross.accumulateLine(curx, cury, newx, newy)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_QUADTO:
            newx = coords[2];
            newy = coords[3];
            if (cross.accumulateQuad(curx, cury, coords)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_CUBICTO:
            newx = coords[4];
            newy = coords[5];
            if (cross.accumulateCubic(curx, cury, coords)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_CLOSE:
            if (movy != cury &&
                cross.accumulateLine(curx, cury, movx, movy))
            {
                return null;
            }
            curx = movx;
            cury = movy;
            break;
        }
        pi.next();
    }
    if (movy != cury) {
        if (cross.accumulateLine(curx, cury, movx, movy)) {
            return null;
        }
    }
    if (debug) {
        cross.print();
    }
    return cross;
}
 
Example 10
Source File: Order0.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public int getSegment(double coords[]) {
    coords[0] = x;
    coords[1] = y;
    return PathIterator.SEG_MOVETO;
}
 
Example 11
Source File: CurveX.java    From pumpernickel with MIT License 4 votes vote down vote up
/**
 * Accumulate the number of times the path crosses the shadow extending to
 * the right of the rectangle. See the comment for the RECT_INTERSECTS
 * constant for more complete details. The return value is the sum of all
 * crossings for both the top and bottom of the shadow for every segment in
 * the path, or the special value RECT_INTERSECTS if the path ever enters
 * the interior of the rectangle. The path must start with a SEG_MOVETO,
 * otherwise an exception is thrown. The caller must check r[xy]{min,max}
 * for NaN values.
 */
public static int rectCrossingsForPath(PathIterator pi, double rxmin,
		double rymin, double rxmax, double rymax) {
	if (rxmax <= rxmin || rymax <= rymin) {
		return 0;
	}
	if (pi.isDone()) {
		return 0;
	}
	double coords[] = new double[6];
	if (pi.currentSegment(coords) != PathIterator.SEG_MOVETO) {
		throw new IllegalPathStateException("missing initial moveto "
				+ "in path definition");
	}
	pi.next();
	double curx, cury, movx, movy, endx, endy;
	curx = movx = coords[0];
	cury = movy = coords[1];
	int crossings = 0;
	while (crossings != RECT_INTERSECTS && !pi.isDone()) {
		switch (pi.currentSegment(coords)) {
		case PathIterator.SEG_MOVETO:
			if (curx != movx || cury != movy) {
				crossings = rectCrossingsForLine(crossings, rxmin, rymin,
						rxmax, rymax, curx, cury, movx, movy);
			}
			// Count should always be a multiple of 2 here.
			// assert((crossings & 1) != 0);
			movx = curx = coords[0];
			movy = cury = coords[1];
			break;
		case PathIterator.SEG_LINETO:
			endx = coords[0];
			endy = coords[1];
			crossings = rectCrossingsForLine(crossings, rxmin, rymin,
					rxmax, rymax, curx, cury, endx, endy);
			curx = endx;
			cury = endy;
			break;
		case PathIterator.SEG_QUADTO:
			endx = coords[2];
			endy = coords[3];
			crossings = rectCrossingsForQuad(crossings, rxmin, rymin,
					rxmax, rymax, curx, cury, coords[0], coords[1], endx,
					endy, 0);
			curx = endx;
			cury = endy;
			break;
		case PathIterator.SEG_CUBICTO:
			endx = coords[4];
			endy = coords[5];
			crossings = rectCrossingsForCubic(crossings, rxmin, rymin,
					rxmax, rymax, curx, cury, coords[0], coords[1],
					coords[2], coords[3], endx, endy, 0);
			curx = endx;
			cury = endy;
			break;
		case PathIterator.SEG_CLOSE:
			if (curx != movx || cury != movy) {
				crossings = rectCrossingsForLine(crossings, rxmin, rymin,
						rxmax, rymax, curx, cury, movx, movy);
			}
			curx = movx;
			cury = movy;
			// Count should always be a multiple of 2 here.
			// assert((crossings & 1) != 0);
			break;
		}
		pi.next();
	}
	if (crossings != RECT_INTERSECTS && (curx != movx || cury != movy)) {
		crossings = rectCrossingsForLine(crossings, rxmin, rymin, rxmax,
				rymax, curx, cury, movx, movy);
	}
	// Count should always be a multiple of 2 here.
	// assert((crossings & 1) != 0);
	return crossings;
}
 
Example 12
Source File: RandomStroke.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Shape createStrokedShape ( Shape shape )
{
    final GeneralPath result = new GeneralPath ();
    shape = new BasicStroke ( 10 ).createStrokedShape ( shape );
    final PathIterator it = new FlatteningPathIterator ( shape.getPathIterator ( null ), FLATNESS );
    final float[] points = new float[ 6 ];
    float moveX = 0;
    float moveY = 0;
    float lastX = 0;
    float lastY = 0;
    float thisX;
    float thisY;
    int type;
    float next = 0;

    while ( !it.isDone () )
    {
        type = it.currentSegment ( points );
        switch ( type )
        {
            case PathIterator.SEG_MOVETO:
                moveX = lastX = randomize ( points[ 0 ] );
                moveY = lastY = randomize ( points[ 1 ] );
                result.moveTo ( moveX, moveY );
                next = 0;
                break;

            case PathIterator.SEG_CLOSE:
                points[ 0 ] = moveX;
                points[ 1 ] = moveY;
                // Fall into....

            case PathIterator.SEG_LINETO:
                thisX = randomize ( points[ 0 ] );
                thisY = randomize ( points[ 1 ] );
                final float dx = thisX - lastX;
                final float dy = thisY - lastY;
                final float distance = ( float ) Math.sqrt ( dx * dx + dy * dy );
                if ( distance >= next )
                {
                    final float r = 1.0f / distance;
                    while ( distance >= next )
                    {
                        final float x = lastX + next * dx * r;
                        final float y = lastY + next * dy * r;
                        result.lineTo ( randomize ( x ), randomize ( y ) );
                        next += detail;
                    }
                }
                next -= distance;
                lastX = thisX;
                lastY = thisY;
                break;
        }
        it.next ();
    }

    return result;
}
 
Example 13
Source File: Curve.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates the number of times the given path
 * crosses the ray extending to the right from (px,py).
 * If the point lies on a part of the path,
 * then no crossings are counted for that intersection.
 * +1 is added for each crossing where the Y coordinate is increasing
 * -1 is added for each crossing where the Y coordinate is decreasing
 * The return value is the sum of all crossings for every segment in
 * the path.
 * The path must start with a SEG_MOVETO, otherwise an exception is
 * thrown.
 * The caller must check p[xy] for NaN values.
 * The caller may also reject infinite p[xy] values as well.
 */
public static int pointCrossingsForPath(PathIterator pi,
                                        double px, double py)
{
    if (pi.isDone()) {
        return 0;
    }
    double coords[] = new double[6];
    if (pi.currentSegment(coords) != PathIterator.SEG_MOVETO) {
        throw new IllegalPathStateException("missing initial moveto "+
                                            "in path definition");
    }
    pi.next();
    double movx = coords[0];
    double movy = coords[1];
    double curx = movx;
    double cury = movy;
    double endx, endy;
    int crossings = 0;
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            if (cury != movy) {
                crossings += pointCrossingsForLine(px, py,
                                                   curx, cury,
                                                   movx, movy);
            }
            movx = curx = coords[0];
            movy = cury = coords[1];
            break;
        case PathIterator.SEG_LINETO:
            endx = coords[0];
            endy = coords[1];
            crossings += pointCrossingsForLine(px, py,
                                               curx, cury,
                                               endx, endy);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_QUADTO:
            endx = coords[2];
            endy = coords[3];
            crossings += pointCrossingsForQuad(px, py,
                                               curx, cury,
                                               coords[0], coords[1],
                                               endx, endy, 0);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_CUBICTO:
            endx = coords[4];
            endy = coords[5];
            crossings += pointCrossingsForCubic(px, py,
                                                curx, cury,
                                                coords[0], coords[1],
                                                coords[2], coords[3],
                                                endx, endy, 0);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_CLOSE:
            if (cury != movy) {
                crossings += pointCrossingsForLine(px, py,
                                                   curx, cury,
                                                   movx, movy);
            }
            curx = movx;
            cury = movy;
            break;
        }
        pi.next();
    }
    if (cury != movy) {
        crossings += pointCrossingsForLine(px, py,
                                           curx, cury,
                                           movx, movy);
    }
    return crossings;
}
 
Example 14
Source File: DuctusRenderingEngine.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void feedConsumer(PathConsumer consumer, PathIterator pi) {
    try {
        consumer.beginPath();
        boolean pathClosed = false;
        float mx = 0.0f;
        float my = 0.0f;
        float point[]  = new float[6];

        while (!pi.isDone()) {
            int type = pi.currentSegment(point);
            if (pathClosed == true) {
                pathClosed = false;
                if (type != PathIterator.SEG_MOVETO) {
                    // Force current point back to last moveto point
                    consumer.beginSubpath(mx, my);
                }
            }
            switch (type) {
            case PathIterator.SEG_MOVETO:
                mx = point[0];
                my = point[1];
                consumer.beginSubpath(point[0], point[1]);
                break;
            case PathIterator.SEG_LINETO:
                consumer.appendLine(point[0], point[1]);
                break;
            case PathIterator.SEG_QUADTO:
                consumer.appendQuadratic(point[0], point[1],
                                         point[2], point[3]);
                break;
            case PathIterator.SEG_CUBICTO:
                consumer.appendCubic(point[0], point[1],
                                     point[2], point[3],
                                     point[4], point[5]);
                break;
            case PathIterator.SEG_CLOSE:
                consumer.closedSubpath();
                pathClosed = true;
                break;
            }
            pi.next();
        }

        consumer.endPath();
    } catch (PathException e) {
        throw new InternalError("Unable to Stroke shape ("+
                                e.getMessage()+")", e);
    }
}
 
Example 15
Source File: ShapeConverter.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String format(Object value) {
    Shape shape = (Shape) value;
    PathIterator pathIterator = shape.getPathIterator(null, 1.0);
    ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
    ArrayList<Geometry> geometries = new ArrayList<Geometry>();
    double[] coord = new double[6];
    while (!pathIterator.isDone()) {
        int type = pathIterator.currentSegment(coord);
        if (type == PathIterator.SEG_MOVETO) {
            coordinatesToGeometry(coordinates, geometries);
            coordinates.add(new Coordinate(coord[0], coord[1]));
        } else if (type == PathIterator.SEG_LINETO) {
            coordinates.add(new Coordinate(coord[0], coord[1]));
        } else if (type == PathIterator.SEG_CLOSE) {
            if (coordinates.size() > 0) {
                if (!coordinates.get(0).equals(coordinates.get(coordinates.size() - 1))) {
                    coordinates.add(coordinates.get(0));
                }
                coordinatesToGeometry(coordinates, geometries);
            }
        }
        pathIterator.next();
    }
    coordinatesToGeometry(coordinates, geometries);

    if (geometries.isEmpty()) {
        return "";
    }

    if (geometries.get(0) instanceof LinearRing) {
        ArrayList<LinearRing> holes = new ArrayList<LinearRing>();
        for (int i = 1; i < geometries.size(); i++) {
            Geometry geometry = geometries.get(i);
            if (geometry instanceof LinearRing) {
                holes.add((LinearRing) geometry);
            }
        }
        if (holes.size() == geometries.size() - 1) {
            return geometryFactory.createPolygon((LinearRing) geometries.get(0),
                                                 holes.toArray(new LinearRing[holes.size()])).toText();
        }
    }
    if (geometries.size() == 1) {
        return geometries.get(0).toText();
    } else {
        return geometryFactory.createGeometryCollection(geometries.toArray(new Geometry[geometries.size()])).toText();
    }
}
 
Example 16
Source File: MutablePath.java    From pumpernickel with MIT License 4 votes vote down vote up
public synchronized boolean setSegment(int pathIndex, int segmentIndex,
		int type, double[] coords) {
	List<MutablePathSegment> segments = paths.get(pathIndex);
	MutablePathSegment s = segments.get(segmentIndex);
	if (s.equals(type, coords))
		return false;

	releaseIterators();
	int oldType = s.type;
	if (oldType == PathIterator.SEG_MOVETO
			&& type != PathIterator.SEG_MOVETO) {
		// this used to mark the start of a new list of segments,
		// but now it does not:
		if (pathIndex == 0) {
			throw new IllegalArgumentException(
					"the first segment must be a MOVETO segment.");
		} else {
			List<MutablePathSegment> previousSegments = paths
					.get(pathIndex - 1);
			MutablePathSegment tail = previousSegments.get(previousSegments
					.size() - 1);
			if (tail.type == PathIterator.SEG_CLOSE) {
				throw new IllegalArgumentException(
						"only a MOVETO segment can follow a CLOSE segment");
			}
			previousSegments.addAll(segments);
			paths.remove(pathIndex);
		}
	} else if (oldType != PathIterator.SEG_MOVETO
			&& type == PathIterator.SEG_MOVETO) {
		// this used to be a regular segment, but not it needs to mark the
		// beginning
		// of a new list of segments:
		List<MutablePathSegment> newSegments = new ArrayList<MutablePathSegment>();
		for (int b = segmentIndex; b < segments.size(); b++) {
			newSegments.add(segments.remove(segmentIndex));
		}
		paths.add(pathIndex, newSegments);
	} else if (oldType != PathIterator.SEG_CLOSE
			&& type == PathIterator.SEG_CLOSE) {
		if (segmentIndex != segments.size() - 1)
			throw new IllegalArgumentException(
					"only a MOVETO segment can follow a CLOSE segment");
	}
	s.setData(type, coords);
	return true;
}
 
Example 17
Source File: PSPrinterJob.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
* Given a Java2D <code>PathIterator</code> instance,
* this method translates that into a PostScript path..
*/
void convertToPSPath(PathIterator pathIter) {

    float[] segment = new float[6];
    int segmentType;

    /* Map the PathIterator's fill rule into the PostScript
     * fill rule.
     */
    int fillRule;
    if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        fillRule = FILL_EVEN_ODD;
    } else {
        fillRule = FILL_WINDING;
    }

    beginPath();

    setFillMode(fillRule);

    while (pathIter.isDone() == false) {
        segmentType = pathIter.currentSegment(segment);

        switch (segmentType) {
         case PathIterator.SEG_MOVETO:
            moveTo(segment[0], segment[1]);
            break;

         case PathIterator.SEG_LINETO:
            lineTo(segment[0], segment[1]);
            break;

        /* Convert the quad path to a bezier.
         */
         case PathIterator.SEG_QUADTO:
            float lastX = getPenX();
            float lastY = getPenY();
            float c1x = lastX + (segment[0] - lastX) * 2 / 3;
            float c1y = lastY + (segment[1] - lastY) * 2 / 3;
            float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
            float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
            bezierTo(c1x, c1y,
                     c2x, c2y,
                     segment[2], segment[3]);
            break;

         case PathIterator.SEG_CUBICTO:
            bezierTo(segment[0], segment[1],
                     segment[2], segment[3],
                     segment[4], segment[5]);
            break;

         case PathIterator.SEG_CLOSE:
            closeSubpath();
            break;
        }


        pathIter.next();
    }
}
 
Example 18
Source File: OutlineZigzagEffect.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** 
 * @see java.awt.Stroke#createStrokedShape(java.awt.Shape)
 */
public Shape createStrokedShape (Shape shape) {
	GeneralPath result = new GeneralPath();
	PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
	float points[] = new float[6];
	float moveX = 0, moveY = 0;
	float lastX = 0, lastY = 0;
	float thisX = 0, thisY = 0;
	int type = 0;
	float next = 0;
	int phase = 0;
	while (!it.isDone()) {
		type = it.currentSegment(points);
		switch (type) {
		case PathIterator.SEG_MOVETO:
			moveX = lastX = points[0];
			moveY = lastY = points[1];
			result.moveTo(moveX, moveY);
			next = wavelength / 2;
			break;

		case PathIterator.SEG_CLOSE:
			points[0] = moveX;
			points[1] = moveY;
			// Fall into....

		case PathIterator.SEG_LINETO:
			thisX = points[0];
			thisY = points[1];
			float dx = thisX - lastX;
			float dy = thisY - lastY;
			float distance = (float)Math.sqrt(dx * dx + dy * dy);
			if (distance >= next) {
				float r = 1.0f / distance;
				while (distance >= next) {
					float x = lastX + next * dx * r;
					float y = lastY + next * dy * r;
					if ((phase & 1) == 0)
						result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r);
					else
						result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r);
					next += wavelength;
					phase++;
				}
			}
			next -= distance;
			lastX = thisX;
			lastY = thisY;
			if (type == PathIterator.SEG_CLOSE) result.closePath();
			break;
		}
		it.next();
	}
	return new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(result);
}
 
Example 19
Source File: PathSegment.java    From pumpernickel with MIT License 4 votes vote down vote up
public float[] getYCoeffs(AffineTransform transform) {
	if (prev == null)
		throw new NullPointerException("prev was null");
	if (prev.data == null)
		throw new NullPointerException("prev.data was null");
	if (transform != null && transform.isIdentity())
		transform = null;
	if (yCoeffs != null && transform == null)
		return yCoeffs;

	double[] last = new double[] { prev.data[prev.data.length - 2],
			prev.data[prev.data.length - 1] };
	if (transform != null) {
		transform.transform(last, 0, last, 0, 1);
	}
	float[] myData;
	if (transform != null) {
		myData = new float[data.length];
		transform.transform(data, 0, myData, 0, data.length / 2);
	} else {
		myData = data;
	}
	float[] rValue = null;
	if (type == PathIterator.SEG_CUBICTO) {
		rValue = new float[4];
		rValue[0] = (float) (-last[1] + 3 * myData[1] - 3 * myData[3] + myData[5]);
		rValue[1] = (float) (3 * last[1] - 6 * myData[1] + 3 * myData[3]);
		rValue[2] = (float) (-3 * last[1] + 3 * myData[1]);
		rValue[3] = (float) (last[1]);
	} else if (type == PathIterator.SEG_QUADTO) {
		rValue = new float[3];
		rValue[0] = (float) (last[1] - 2 * myData[1] + myData[3]);
		rValue[1] = (float) (-2 * last[1] + 2 * myData[1]);
		rValue[2] = (float) (last[1]);
	} else if (type == PathIterator.SEG_LINETO) {
		rValue = new float[2];
		rValue[0] = (float) (-last[1] + myData[1]);
		rValue[1] = (float) (last[1]);
	} else if (type == PathIterator.SEG_MOVETO) {
		throw new UnsupportedOperationException(
				"MOVETO segments cannot be broken down into parametric equations.");
	} else if (type == PathIterator.SEG_CLOSE) {
		throw new UnsupportedOperationException(
				"CLOSE segments cannot be broken down into parametric equations.");
	}
	if (transform == null)
		yCoeffs = rValue;
	return rValue;
}
 
Example 20
Source File: DuctusRenderingEngine.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void feedConsumer(PathConsumer consumer, PathIterator pi) {
    try {
        consumer.beginPath();
        boolean pathClosed = false;
        float mx = 0.0f;
        float my = 0.0f;
        float point[]  = new float[6];

        while (!pi.isDone()) {
            int type = pi.currentSegment(point);
            if (pathClosed == true) {
                pathClosed = false;
                if (type != PathIterator.SEG_MOVETO) {
                    // Force current point back to last moveto point
                    consumer.beginSubpath(mx, my);
                }
            }
            switch (type) {
            case PathIterator.SEG_MOVETO:
                mx = point[0];
                my = point[1];
                consumer.beginSubpath(point[0], point[1]);
                break;
            case PathIterator.SEG_LINETO:
                consumer.appendLine(point[0], point[1]);
                break;
            case PathIterator.SEG_QUADTO:
                consumer.appendQuadratic(point[0], point[1],
                                         point[2], point[3]);
                break;
            case PathIterator.SEG_CUBICTO:
                consumer.appendCubic(point[0], point[1],
                                     point[2], point[3],
                                     point[4], point[5]);
                break;
            case PathIterator.SEG_CLOSE:
                consumer.closedSubpath();
                pathClosed = true;
                break;
            }
            pi.next();
        }

        consumer.endPath();
    } catch (PathException e) {
        throw new InternalError("Unable to Stroke shape ("+
                                e.getMessage()+")", e);
    }
}