Example usage for java.awt.geom PathIterator SEG_CLOSE

List of usage examples for java.awt.geom PathIterator SEG_CLOSE

Introduction

In this page you can find the example usage for java.awt.geom PathIterator SEG_CLOSE.

Prototype

int SEG_CLOSE

To view the source code for java.awt.geom PathIterator SEG_CLOSE.

Click Source Link

Document

The segment type constant that specifies that the preceding subpath should be closed by appending a line segment back to the point corresponding to the most recent SEG_MOVETO.

Usage

From source file:DescribePath.java

public static void describeCurrentSegment(PathIterator pi) {
    double[] coordinates = new double[6];
    int type = pi.currentSegment(coordinates);
    switch (type) {
    case PathIterator.SEG_MOVETO:
        System.out.println("move to " + coordinates[0] + ", " + coordinates[1]);
        break;/*from w w w  . j  av a  2  s  . c  om*/
    case PathIterator.SEG_LINETO:
        System.out.println("line to " + coordinates[0] + ", " + coordinates[1]);
        break;
    case PathIterator.SEG_QUADTO:
        System.out.println("quadratic to " + coordinates[0] + ", " + coordinates[1] + ", " + coordinates[2]
                + ", " + coordinates[3]);
        break;
    case PathIterator.SEG_CUBICTO:
        System.out.println("cubic to " + coordinates[0] + ", " + coordinates[1] + ", " + coordinates[2] + ", "
                + coordinates[3] + ", " + coordinates[4] + ", " + coordinates[5]);
        break;
    case PathIterator.SEG_CLOSE:
        System.out.println("close");
        break;
    default:
        break;
    }
}

From source file:Main.java

public static void writePath(GeneralPath path, ObjectOutputStream out) throws IOException {
    PathIterator i = path.getPathIterator(null);
    float[] data = new float[6];

    while (!i.isDone()) {
        switch (i.currentSegment(data)) {
        case PathIterator.SEG_MOVETO:
            out.writeInt(PathIterator.SEG_MOVETO);
            out.writeFloat(data[0]);//w  ww .j  av a  2s.  com
            out.writeFloat(data[1]);
            break;

        case PathIterator.SEG_LINETO:
            out.writeInt(PathIterator.SEG_LINETO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            break;

        case PathIterator.SEG_QUADTO:
            out.writeInt(PathIterator.SEG_QUADTO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            out.writeFloat(data[2]);
            out.writeFloat(data[3]);
            break;

        case PathIterator.SEG_CUBICTO:
            out.writeInt(PathIterator.SEG_CUBICTO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            out.writeFloat(data[2]);
            out.writeFloat(data[3]);
            out.writeFloat(data[4]);
            out.writeFloat(data[5]);
            break;

        case PathIterator.SEG_CLOSE:
            out.writeInt(PathIterator.SEG_CLOSE);
            break;

        default:
            throw new IOException();
        }

        i.next();
    }

    out.writeInt(PATH_IS_DONE);
}

From source file:com.jhlabs.awt.TextStroke.java

public Shape createStrokedShape(Shape shape) {
    FontRenderContext frc = new FontRenderContext(null, true, true);
    GlyphVector glyphVector = font.createGlyphVector(frc, text);

    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 currentChar = 0;
    int length = glyphVector.getNumGlyphs();

    if (length == 0)
        return result;

    float factor = stretchToFit ? measurePathLength(shape) / (float) glyphVector.getLogicalBounds().getWidth()
            : 1.0f;// w w w.j a va  2s .  co m
    float height = (float) glyphVector.getLogicalBounds().getHeight();
    float nextAdvance = 0;

    while (currentChar < length && !it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];
            moveY = lastY = points[1];
            result.moveTo(moveX, moveY);
            nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f;
            next = nextAdvance;
            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) FastMath.sqrt(dx * dx + dy * dy);
            if (distance >= next) {
                float r = 1.0f / distance;
                float angle = (float) FastMath.atan2(dy, dx);
                while (currentChar < length && distance >= next) {
                    Shape glyph = glyphVector.getGlyphOutline(currentChar);
                    Point2D p = glyphVector.getGlyphPosition(currentChar);
                    float px = (float) p.getX();
                    float py = (float) p.getY();
                    float x = lastX + next * dx * r;
                    float y = lastY + next * dy * r;
                    float advance = nextAdvance;
                    nextAdvance = currentChar < length - 1
                            ? glyphVector.getGlyphMetrics(currentChar + 1).getAdvance() * 0.5f
                            : 0;
                    t.setToTranslation(x, y);
                    t.rotate(angle);
                    t.translate(-px - advance, -py + height * factor / 2.0f);
                    result.append(t.createTransformedShape(glyph), false);
                    next += (advance + nextAdvance) * factor;
                    currentChar++;
                    if (repeat)
                        currentChar %= length;
                }
            }
            next -= distance;
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return result;
}

From source file:Main.java

/**
 * Reads a <code>Shape</code> object that has been serialised by the
 * {@link #writeShape(Shape, ObjectOutputStream)} method.
 *
 * @param stream  the input stream (<code>null</code> not permitted).
 *
 * @return The shape object (possibly <code>null</code>).
 *
 * @throws IOException  if there is an I/O problem.
 * @throws ClassNotFoundException  if there is a problem loading a class.
 *//*  w w w .j a  v  a  2 s. c o m*/
public static Shape readShape(final ObjectInputStream stream) throws IOException, ClassNotFoundException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    Shape result = null;
    final boolean isNull = stream.readBoolean();
    if (!isNull) {
        final Class c = (Class) stream.readObject();
        if (c.equals(Line2D.class)) {
            final double x1 = stream.readDouble();
            final double y1 = stream.readDouble();
            final double x2 = stream.readDouble();
            final double y2 = stream.readDouble();
            result = new Line2D.Double(x1, y1, x2, y2);
        } else if (c.equals(Rectangle2D.class)) {
            final double x = stream.readDouble();
            final double y = stream.readDouble();
            final double w = stream.readDouble();
            final double h = stream.readDouble();
            result = new Rectangle2D.Double(x, y, w, h);
        } else if (c.equals(Ellipse2D.class)) {
            final double x = stream.readDouble();
            final double y = stream.readDouble();
            final double w = stream.readDouble();
            final double h = stream.readDouble();
            result = new Ellipse2D.Double(x, y, w, h);
        } else if (c.equals(Arc2D.class)) {
            final double x = stream.readDouble();
            final double y = stream.readDouble();
            final double w = stream.readDouble();
            final double h = stream.readDouble();
            final double as = stream.readDouble(); // Angle Start
            final double ae = stream.readDouble(); // Angle Extent
            final int at = stream.readInt(); // Arc type
            result = new Arc2D.Double(x, y, w, h, as, ae, at);
        } else if (c.equals(GeneralPath.class)) {
            final GeneralPath gp = new GeneralPath();
            final float[] args = new float[6];
            boolean hasNext = stream.readBoolean();
            while (!hasNext) {
                final int type = stream.readInt();
                for (int i = 0; i < 6; i++) {
                    args[i] = stream.readFloat();
                }
                switch (type) {
                case PathIterator.SEG_MOVETO:
                    gp.moveTo(args[0], args[1]);
                    break;
                case PathIterator.SEG_LINETO:
                    gp.lineTo(args[0], args[1]);
                    break;
                case PathIterator.SEG_CUBICTO:
                    gp.curveTo(args[0], args[1], args[2], args[3], args[4], args[5]);
                    break;
                case PathIterator.SEG_QUADTO:
                    gp.quadTo(args[0], args[1], args[2], args[3]);
                    break;
                case PathIterator.SEG_CLOSE:
                    gp.closePath();
                    break;
                default:
                    throw new RuntimeException("JFreeChart - No path exists");
                }
                gp.setWindingRule(stream.readInt());
                hasNext = stream.readBoolean();
            }
            result = gp;
        } else {
            result = (Shape) stream.readObject();
        }
    }
    return result;

}

From source file:com.jhlabs.awt.TextStroke.java

public float measurePathLength(Shape shape) {
    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 total = 0;

    while (!it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];/*from  w  w  w  . j av  a 2  s . co m*/
            moveY = lastY = points[1];
            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;
            total += (float) Math.sqrt(dx * dx + dy * dy);
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return total;
}

From source file:PathLength.java

/**
 * Flattens the path and determines the path length.
 *//*from  w w w  .j  a  v a2 s. c  o  m*/
protected void initialise() {
    pathLength = 0f;

    PathIterator pi = path.getPathIterator(new AffineTransform());
    SingleSegmentPathIterator sspi = new SingleSegmentPathIterator();
    segments = new ArrayList(20);
    List indexes = new ArrayList(20);
    int index = 0;
    int origIndex = -1;
    float lastMoveX = 0f;
    float lastMoveY = 0f;
    float currentX = 0f;
    float currentY = 0f;
    float[] seg = new float[6];
    int segType;

    segments.add(new PathSegment(PathIterator.SEG_MOVETO, 0f, 0f, 0f, origIndex));

    while (!pi.isDone()) {
        origIndex++;
        indexes.add(new Integer(index));
        segType = pi.currentSegment(seg);
        switch (segType) {
        case PathIterator.SEG_MOVETO:
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            lastMoveX = currentX;
            lastMoveY = currentY;
            index++;
            pi.next();
            break;
        case PathIterator.SEG_LINETO:
            pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            index++;
            pi.next();
            break;
        case PathIterator.SEG_CLOSE:
            pathLength += Point2D.distance(currentX, currentY, lastMoveX, lastMoveY);
            segments.add(new PathSegment(PathIterator.SEG_LINETO, lastMoveX, lastMoveY, pathLength, origIndex));
            currentX = lastMoveX;
            currentY = lastMoveY;
            index++;
            pi.next();
            break;
        default:
            sspi.setPathIterator(pi, currentX, currentY);
            FlatteningPathIterator fpi = new FlatteningPathIterator(sspi, 0.01f);
            while (!fpi.isDone()) {
                segType = fpi.currentSegment(seg);
                if (segType == PathIterator.SEG_LINETO) {
                    pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
                    segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
                    currentX = seg[0];
                    currentY = seg[1];
                    index++;
                }
                fpi.next();
            }
        }
    }
    segmentIndexes = new int[indexes.size()];
    for (int i = 0; i < segmentIndexes.length; i++) {
        segmentIndexes[i] = ((Integer) indexes.get(i)).intValue();
    }
    initialised = true;
}

From source file:it.unibo.alchemist.model.implementations.linkingrules.ConnectionBeam.java

private boolean projectedBeamOvercomesObstacle(final Position pos1, final Position pos2) {
    final double p1x = pos1.getCoordinate(0);
    final double p1y = pos1.getCoordinate(1);
    final double p2x = pos2.getCoordinate(0);
    final double p2y = pos2.getCoordinate(1);
    final double x = p2x - p1x;
    final double y = p2y - p1y;
    /*/*from  w ww  .  ja  v  a2  s  .  c om*/
     * Compute the angle
     */
    final double angle = atan2(y, x);
    /*
     * Deduce surrounding beam vertices
     */
    final double dx = range * cos(PI / 2 + angle);
    final double dy = range * sin(PI / 2 + angle);
    /*
     * Enlarge the beam
     */
    final double cx = range * cos(angle);
    final double cy = range * sin(angle);
    /*
     * Create the beam
     */
    final Path2D.Double beamShape = new Path2D.Double();
    beamShape.moveTo(p1x + dx - cx, p1y + dy - cy);
    beamShape.lineTo(p1x - dx - cx, p1y - dy - cy);
    beamShape.lineTo(p2x - dx + cx, p2y - dy + cy);
    beamShape.lineTo(p2x + dx + cx, p2y + dy + cy);
    beamShape.closePath();
    final Area beam = new Area(beamShape);
    /*
     * Perform subtraction
     */
    beam.subtract(obstacles);
    /*
     * Rebuild single areas
     */
    final List<Path2D.Double> subareas = new ArrayList<>();
    Path2D.Double curpath = new Path2D.Double();
    final PathIterator pi = beam.getPathIterator(null);
    final double[] coords = new double[COORDS];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            curpath = new Path2D.Double();
            curpath.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            curpath.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_CLOSE:
            curpath.closePath();
            subareas.add(curpath);
            break;
        default:
            throw new IllegalArgumentException();
        }
        pi.next();
    }
    /*
     * At least one area must contain both points
     */
    for (final Path2D.Double p : subareas) {
        if (p.contains(p1x, p1y) && p.contains(p2x, p2y)) {
            return true;
        }
    }
    return false;
}

From source file:CustomStrokes.java

public Shape createStrokedShape(Shape shape) {
    // Start off by stroking the shape with a thin line. Store the
    // resulting shape in a GeneralPath object so we can add to it.
    GeneralPath strokedShape = new GeneralPath(new BasicStroke(1.0f).createStrokedShape(shape));

    // Use a PathIterator object to iterate through each of the line and
    // curve segments of the shape. For each one, mark the endpoint and
    // control points (if any) by adding a rectangle to the GeneralPath
    float[] coords = new float[6];
    for (PathIterator i = shape.getPathIterator(null); !i.isDone(); i.next()) {
        int type = i.currentSegment(coords);
        Shape s = null, s2 = null, s3 = null;
        switch (type) {
        case PathIterator.SEG_CUBICTO:
            markPoint(strokedShape, coords[4], coords[5]); // falls through
        case PathIterator.SEG_QUADTO:
            markPoint(strokedShape, coords[2], coords[3]); // falls through
        case PathIterator.SEG_MOVETO:
        case PathIterator.SEG_LINETO:
            markPoint(strokedShape, coords[0], coords[1]); // falls through
        case PathIterator.SEG_CLOSE:
            break;
        }//w  w  w  .ja  v a2  s.  c  om
    }

    return strokedShape;
}

From source file:CustomStrokes.java

public Shape createStrokedShape(Shape shape) {
    GeneralPath newshape = new GeneralPath(); // Start with an empty shape

    // Iterate through the specified shape, perturb its coordinates, and
    // use them to build up the new shape.
    float[] coords = new float[6];
    for (PathIterator i = shape.getPathIterator(null); !i.isDone(); i.next()) {
        int type = i.currentSegment(coords);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            perturb(coords, 2);//from w  w  w  . ja v a 2s .  c o m
            newshape.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            perturb(coords, 2);
            newshape.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            perturb(coords, 4);
            newshape.quadTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            perturb(coords, 6);
            newshape.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            newshape.closePath();
            break;
        }
    }

    // Finally, stroke the perturbed shape and return the result
    return stroke.createStrokedShape(newshape);
}

From source file:ExtendedGeneralPath.java

/**
 * Delegates to the enclosed <code>GeneralPath</code>.
 *//*from w  w w .j  a v a  2s  . co  m*/
public synchronized void closePath() {
    // Don't double close path.
    if ((numSeg != 0) && (types[numSeg - 1] == PathIterator.SEG_CLOSE))
        return;

    // Only close path if the previous command wasn't a moveto
    if ((numSeg != 0) && (types[numSeg - 1] != PathIterator.SEG_MOVETO))
        path.closePath();

    makeRoom(0);
    types[numSeg++] = PathIterator.SEG_CLOSE;
    cx = mx;
    cy = my;
}