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, double flatness);

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Shape s = new Rectangle2D.Double(0, 0, 72, 72);

    PathIterator pi = s.getPathIterator(null, 2);

    while (pi.isDone() == false) {
        describeCurrentSegment(pi);//from w  w w  . ja  va2 s . com
        pi.next();
    }

}

From source file:com.t_oster.visicut.misc.Helper.java

public static Rectangle2D smallestBoundingBox(Shape s, AffineTransform t) {
    double minX = 0;
    double maxX = 0;
    double minY = 0;
    double maxY = 0;
    PathIterator pi = s.getPathIterator(t, 1);
    double[] last = null;
    boolean first = true;
    while (!pi.isDone()) {
        double[] d = new double[8];
        switch (pi.currentSegment(d)) {
        case PathIterator.SEG_LINETO: {
            if (last != null) {
                if (first) {
                    minX = last[0];/*from  w w w . j a  v a 2s  . c  om*/
                    maxX = last[0];
                    minY = last[1];
                    maxY = last[1];
                    first = false;
                } else {
                    if (last[0] < minX) {
                        minX = last[0];
                    }
                    if (last[0] > maxX) {
                        maxX = last[0];
                    }
                    if (last[1] < minY) {
                        minY = last[1];
                    }
                    if (last[1] > maxY) {
                        maxY = last[1];
                    }
                }
            }
            if (first) {
                minX = d[0];
                maxX = d[0];
                minY = d[1];
                maxY = d[1];
                first = false;
            } else {
                if (d[0] < minX) {
                    minX = d[0];
                }
                if (d[0] > maxX) {
                    maxX = d[0];
                }
                if (d[1] < minY) {
                    minY = d[1];
                }
                if (d[1] > maxY) {
                    maxY = d[1];
                }
            }
            break;
        }
        case PathIterator.SEG_MOVETO: {
            last = d;
            break;
        }
        }
        pi.next();
    }
    return new Rectangle.Double(minX, minY, maxX - minX, maxY - minY);
}