Example usage for java.awt.geom PathIterator SEG_MOVETO

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

Introduction

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

Prototype

int SEG_MOVETO

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

Click Source Link

Document

The segment type constant for a point that specifies the starting location for a new subpath.

Usage

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  w  w . ja v a  2s .  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.  ja  v  a  2s.  com*/
 */
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;
        }
    }
}

From source file:org.apache.pdfbox.pdfviewer.font.CFFGlyph2D.java

private GeneralPath transformGlyph(GeneralPath glyph) {
    // we have to invert all y-coordinates due to the moved 0,0-reference
    PathIterator iter = glyph.getPathIterator(null);
    float[] currentSegment = new float[6];
    Path2D.Float path = new Path2D.Float(iter.getWindingRule());
    boolean glyphTransformed = false;
    while (!iter.isDone()) {
        glyphTransformed = true;/*from   www  .j a v a2s  .  c  o m*/
        int type = iter.currentSegment(currentSegment);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            path.moveTo(currentSegment[0], -currentSegment[1]);
            break;
        case PathIterator.SEG_LINETO:
            path.lineTo(currentSegment[0], -currentSegment[1]);
            break;
        case PathIterator.SEG_QUADTO:
            path.quadTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            path.curveTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3],
                    currentSegment[4], -currentSegment[5]);
            break;
        case PathIterator.SEG_CLOSE:
            path.closePath();
            break;
        }
        iter.next();
    }
    if (glyphTransformed) {
        return new GeneralPath(path);
    } else {
        return glyph;
    }
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

/**
 * Returns true if the given path is rectangular.
 *//*from w w  w  .j  av a 2  s.c  o m*/
private boolean isRectangular(GeneralPath path) {
    PathIterator iter = path.getPathIterator(null);
    double[] coords = new double[6];
    int count = 0;
    int[] xs = new int[4];
    int[] ys = new int[4];
    while (!iter.isDone()) {
        switch (iter.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            if (count == 0) {
                xs[count] = (int) Math.floor(coords[0]);
                ys[count] = (int) Math.floor(coords[1]);
            } else {
                return false;
            }
            count++;
            break;

        case PathIterator.SEG_LINETO:
            if (count < 4) {
                xs[count] = (int) Math.floor(coords[0]);
                ys[count] = (int) Math.floor(coords[1]);
            } else {
                return false;
            }
            count++;
            break;

        case PathIterator.SEG_CUBICTO:
            return false;

        case PathIterator.SEG_CLOSE:
            break;
        }
        iter.next();
    }

    if (count == 4) {
        return xs[0] == xs[1] || xs[0] == xs[2] || ys[0] == ys[1] || ys[0] == ys[3];
    }
    return false;
}

From source file:org.eclipse.birt.chart.device.svg.SVGGraphics2D.java

protected Element createShape(Shape shape) {
    PathIterator pathIter = shape.getPathIterator(null);
    StringBuffer pathStr = new StringBuffer();
    while (!pathIter.isDone()) {
        float[] points = new float[6];
        int TYPE = pathIter.currentSegment(points);
        switch (TYPE) {
        case PathIterator.SEG_CLOSE:
            pathStr.append(" Z"); //$NON-NLS-1$
            break;
        case PathIterator.SEG_LINETO:
            pathStr.append(" L").append(toString(points, 2, ' ')); //$NON-NLS-1$ 
            break;
        case PathIterator.SEG_QUADTO:
            pathStr.append(" Q").append(toString(points, 4, ' ')); //$NON-NLS-1$ 
            break;
        case PathIterator.SEG_CUBICTO:
            pathStr.append(" C").append(toString(points, 6, ' ')); //$NON-NLS-1$ 
            break;
        case PathIterator.SEG_MOVETO:
            pathStr.append(" M").append(toString(points, 2, ' ')); //$NON-NLS-1$ 
            break;
        }/*from  ww w . j  a  va2 s  .  c o m*/
        pathIter.next();
    }
    Element elem = dom.createElement("path"); //$NON-NLS-1$
    elem.setAttribute("d", pathStr.toString()); //$NON-NLS-1$
    return elem;
}

From source file:org.esa.snap.graphbuilder.gpf.ui.worldmap.NestWorldMapPane.java

public 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();/*from  w w  w  .  j a v a 2 s .c  om*/
        }
        iterator.next();
    }
    return pixelPath;
}

From source file:org.geotools.gce.imagemosaic.Utils.java

/**
 * Checks if the Shape equates to a Rectangle, if it does it performs a conversion, otherwise
 * returns null/*w w w . j av  a  2s  .c  o  m*/
 * @param shape
 * @return
 */
static Rectangle toRectangle(Shape shape) {
    if (shape instanceof Rectangle) {
        return (Rectangle) shape;
    }

    if (shape == null) {
        return null;
    }

    // check if it's equivalent to a rectangle
    PathIterator iter = shape.getPathIterator(new AffineTransform());
    double[] coords = new double[2];

    // not enough points?
    if (iter.isDone()) {
        return null;
    }

    // get the first and init the data structures
    iter.next();
    int action = iter.currentSegment(coords);
    if (action != PathIterator.SEG_MOVETO && action != PathIterator.SEG_LINETO) {
        return null;
    }
    double minx = coords[0];
    double miny = coords[1];
    double maxx = minx;
    double maxy = miny;
    double prevx = minx;
    double prevy = miny;
    int i = 0;

    // at most 4 steps, if more it's not a strict rectangle
    for (; i < 4 && !iter.isDone(); i++) {
        iter.next();
        action = iter.currentSegment(coords);

        if (action == PathIterator.SEG_CLOSE) {
            break;
        }
        if (action != PathIterator.SEG_LINETO) {
            return null;
        }

        // check orthogonal step (x does not change and y does, or vice versa)
        double x = coords[0];
        double y = coords[1];
        if (!(prevx == x && prevy != y) && !(prevx != x && prevy == y)) {
            return null;
        }

        // update mins and maxes
        if (x < minx) {
            minx = x;
        } else if (x > maxx) {
            maxx = x;
        }
        if (y < miny) {
            miny = y;
        } else if (y > maxy) {
            maxy = y;
        }

        // keep track of prev step
        prevx = x;
        prevy = y;
    }

    // if more than 4 other points it's not a standard rectangle
    iter.next();
    if (!iter.isDone() || i != 3) {
        return null;
    }

    // turn it into a rectangle
    return new Rectangle2D.Double(minx, miny, maxx - minx, maxy - miny).getBounds();
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private void followPath(Shape s, final int drawType) {
    if (s == null) {
        return;/*  www.  j  a v  a  2s.  c o  m*/
    }
    if (drawType == PdfGraphics2D.STROKE) {
        if (!(stroke instanceof BasicStroke)) {
            s = stroke.createStrokedShape(s);
            followPath(s, PdfGraphics2D.FILL);
            return;
        }
    }
    if (drawType == PdfGraphics2D.STROKE) {
        setStrokeDiff(stroke, oldStroke);
        oldStroke = stroke;
        setStrokePaint();
    } else if (drawType == PdfGraphics2D.FILL) {
        setFillPaint();
    }
    final PathIterator points;
    if (drawType == PdfGraphics2D.CLIP) {
        points = s.getPathIterator(PdfGraphics2D.IDENTITY);
    } else {
        points = s.getPathIterator(transform);
    }
    final float[] coords = new float[6];
    int traces = 0;
    while (!points.isDone()) {
        ++traces;
        final int segtype = points.currentSegment(coords);
        normalizeY(coords);
        switch (segtype) {
        case PathIterator.SEG_CLOSE:
            cb.closePath();
            break;

        case PathIterator.SEG_CUBICTO:
            cb.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;

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

        case PathIterator.SEG_MOVETO:
            cb.moveTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_QUADTO:
            cb.curveTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        default:
            throw new IllegalStateException("Invalid segment type in path");
        }
        points.next();
    }
    switch (drawType) {
    case PdfGraphics2D.FILL:
        if (traces > 0) {
            if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
                cb.eoFill();
            } else {
                cb.fill();
            }
        }
        break;
    case PdfGraphics2D.STROKE:
        if (traces > 0) {
            cb.stroke();
        }
        break;
    default: // drawType==CLIP
        if (traces == 0) {
            cb.rectangle(0, 0, 0, 0);
        }
        if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
            cb.eoClip();
        } else {
            cb.clip();
        }
        cb.newPath();
    }
}