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: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 www  .  ja 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  w  w  w  . jav a2  s  .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();
    }
}