Example usage for java.awt Shape getPathIterator

List of usage examples for java.awt Shape getPathIterator

Introduction

In this page you can find the example usage for java.awt Shape getPathIterator.

Prototype

public PathIterator getPathIterator(AffineTransform at);

Source Link

Document

Returns an iterator object that iterates along the Shape boundary and provides access to the geometry of the Shape outline.

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.//  w w w  .  j a v a2  s  .  co 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

/**
 * Handle the Batik drawing event/*from  ww w  . j a  v a  2  s  . c o  m*/
 *
 * @param shape
 *            the shape to draw
 * @param fill
 *            true if the shape is to be drawn filled
 */
private void doDrawing(Shape shape, boolean fill) {
    if (!fill) {
        graphicsObj.newSegment();
    }

    graphicsObj.setColor(gc.getColor());

    applyPaint(gc.getPaint(), fill);

    if (fill) {
        graphicsObj.beginArea();
    } else {
        applyStroke(gc.getStroke());
    }

    AffineTransform trans = gc.getTransform();
    PathIterator iter = shape.getPathIterator(trans);
    if (shape instanceof Line2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.currentSegment(dstPts);
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        graphicsObj.addLine(coords);
    } else if (shape instanceof Rectangle2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.next();
        iter.currentSegment(dstPts);
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        graphicsObj.addBox(coords);
    } else if (shape instanceof Ellipse2D) {
        double[] dstPts = new double[6];
        Ellipse2D elip = (Ellipse2D) shape;
        double scale = trans.getScaleX();
        double radiusWidth = elip.getWidth() / 2;
        double radiusHeight = elip.getHeight() / 2;
        graphicsObj.setArcParams((int) Math.round(radiusWidth * scale), (int) Math.round(radiusHeight * scale),
                0, 0);
        double[] srcPts = new double[] { elip.getCenterX(), elip.getCenterY() };
        trans.transform(srcPts, 0, dstPts, 0, 1);
        final int mh = 1;
        final int mhr = 0;
        graphicsObj.addFullArc((int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]), mh, mhr);
    } else {
        processPathIterator(iter);
    }

    if (fill) {
        graphicsObj.endArea();
    }
}

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;
        }/*  w w  w .j ava  2  s.co 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.geotools.gce.imagemosaic.Utils.java

/**
 * Checks if the Shape equates to a Rectangle, if it does it performs a conversion, otherwise
 * returns null/*from  w  w  w  .  j a v 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;/*from  ww w  . j  a  va  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();
    }
}