Example usage for java.awt.geom PathIterator SEG_CUBICTO

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

Introduction

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

Prototype

int SEG_CUBICTO

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

Click Source Link

Document

The segment type constant for the set of 3 points that specify a cubic parametric curve to be drawn from the most recently specified point.

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 ww  w .  j ava 2s . c o  m
    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 w  w. j  av  a2 s  .c  o m*/
            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: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 va  2  s  .co 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: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;
        }/*from   w  ww . ja va2 s . c o m*/
    }

    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   ww  w .  ja v a2 s  . co  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 ww  w  .ja va  2s.c o  m
public synchronized void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) {
    checkMoveTo(); // check if prev command was moveto
    path.curveTo(x1, y1, x2, y2, x3, y3);

    makeRoom(6);
    types[numSeg++] = PathIterator.SEG_CUBICTO;
    values[numVals++] = x1;
    values[numVals++] = y1;
    values[numVals++] = x2;
    values[numVals++] = y2;
    cx = values[numVals++] = x3;
    cy = values[numVals++] = y3;
}

From source file:ExtendedGeneralPath.java

/**
 * Delegates to the enclosed <code>GeneralPath</code>.
 *///from   www  .ja v  a  2 s.  co m
public void append(PathIterator pi, boolean connect) {
    double[] vals = new double[6];

    while (!pi.isDone()) {
        Arrays.fill(vals, 0);
        int type = pi.currentSegment(vals);
        pi.next();
        if (connect && (numVals != 0)) {
            if (type == PathIterator.SEG_MOVETO) {
                double x = vals[0];
                double y = vals[1];
                if ((x != cx) || (y != cy)) {
                    // Change MOVETO to LINETO.
                    type = PathIterator.SEG_LINETO;
                } else {
                    // Redundent segment (move to current loc) drop it...
                    if (pi.isDone())
                        break; // Nothing interesting
                    type = pi.currentSegment(vals);
                    pi.next();
                }
            }
            connect = false;
        }

        switch (type) {
        case PathIterator.SEG_CLOSE:
            closePath();
            break;
        case PathIterator.SEG_MOVETO:
            moveTo((float) vals[0], (float) vals[1]);
            break;
        case PathIterator.SEG_LINETO:
            lineTo((float) vals[0], (float) vals[1]);
            break;
        case PathIterator.SEG_QUADTO:
            quadTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            curveTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3], (float) vals[4],
                    (float) vals[5]);
            break;
        }
    }
}

From source file:ExtendedGeneralPath.java

/**
 * Delegates to the enclosed <code>GeneralPath</code>.
 *///from  ww w  .j  a  v  a 2 s  . com
public void append(ExtendedPathIterator epi, boolean connect) {
    float[] vals = new float[7];
    while (!epi.isDone()) {
        Arrays.fill(vals, 0);
        int type = epi.currentSegment(vals);
        epi.next();
        if (connect && (numVals != 0)) {
            if (type == PathIterator.SEG_MOVETO) {
                float x = vals[0];
                float y = vals[1];
                if ((x != cx) || (y != cy)) {
                    // Change MOVETO to LINETO.
                    type = PathIterator.SEG_LINETO;
                } else {
                    // Redundant segment (move to current loc) drop it...
                    if (epi.isDone())
                        break; // Nothing interesting
                    type = epi.currentSegment(vals);
                    epi.next();
                }
            }
            connect = false;
        }

        switch (type) {
        case PathIterator.SEG_CLOSE:
            closePath();
            break;
        case PathIterator.SEG_MOVETO:
            moveTo(vals[0], vals[1]);
            break;
        case PathIterator.SEG_LINETO:
            lineTo(vals[0], vals[1]);
            break;
        case PathIterator.SEG_QUADTO:
            quadTo(vals[0], vals[1], vals[2], vals[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            curveTo(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]);
            break;
        case ExtendedPathIterator.SEG_ARCTO:
            arcTo(vals[0], vals[1], vals[2], (vals[3] != 0), (vals[4] != 0), vals[5], vals[6]);
            break;
        }
    }
}

From source file:SWTGraphics2D.java

/**
 * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
 *
 * @param shape  the shape (<code>null</code> not permitted).
 *
 * @return The path./*from  w  ww. j  a  v  a 2  s . c  o m*/
 */
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;
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Processes a path iterator generating the necessary painting operations.
 *
 * @param iter PathIterator to process/*from w  ww.  jav  a 2s  .c  om*/
 */
private void processPathIterator(PathIterator iter) {
    double[] dstPts = new double[6];
    double[] currentPosition = new double[2];
    for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) {
        switch (iter.currentSegment(dstPts)) {
        case PathIterator.SEG_LINETO:
            graphicsObj.addLine(new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) }, true);
            currentPosition = new double[] { dstPts[X], dstPts[Y] };
            break;
        case PathIterator.SEG_QUADTO:
            graphicsObj.addFillet(new int[] { (int) Math.round(dstPts[X1]), (int) Math.round(dstPts[Y1]),
                    (int) Math.round(dstPts[X2]), (int) Math.round(dstPts[Y2]) }, true);
            currentPosition = new double[] { dstPts[X2], dstPts[Y2] };
            break;
        case PathIterator.SEG_CUBICTO:
            double[] cubicCoords = new double[] { currentPosition[0], currentPosition[1], dstPts[X1],
                    dstPts[Y1], dstPts[X2], dstPts[Y2], dstPts[X3], dstPts[Y3] };
            double[][] quadParts = CubicBezierApproximator.fixedMidPointApproximation(cubicCoords);
            if (quadParts.length >= 4) {
                for (int segIndex = 0; segIndex < quadParts.length; segIndex++) {
                    double[] quadPts = quadParts[segIndex];
                    if (quadPts != null && quadPts.length == 4) {
                        graphicsObj.addFillet(
                                new int[] { (int) Math.round(quadPts[X1]), (int) Math.round(quadPts[Y1]),
                                        (int) Math.round(quadPts[X2]), (int) Math.round(quadPts[Y2]) },
                                true);
                        currentPosition = new double[] { quadPts[X2], quadPts[Y2] };
                    }
                }
            }
            break;
        case PathIterator.SEG_MOVETO:
            openingCoords = new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) };
            currentPosition = new double[] { dstPts[X], dstPts[Y] };
            graphicsObj.setCurrentPosition(openingCoords);
            break;
        case PathIterator.SEG_CLOSE:
            graphicsObj.addLine(openingCoords, true);
            currentPosition = new double[] { openingCoords[0], openingCoords[1] };
            break;
        default:
            LOG.debug("Unrecognised path iterator type");
            break;
        }
    }
}