Example usage for java.awt.geom GeneralPath getPathIterator

List of usage examples for java.awt.geom GeneralPath getPathIterator

Introduction

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

Prototype

public final PathIterator getPathIterator(AffineTransform at, double flatness) 

Source Link

Document

The iterator for this class is not multi-threaded safe, which means that this Path2D class does not guarantee that modifications to the geometry of this Path2D object do not affect any iterations of that geometry that are already in process.

Usage

From source file:de.bund.bfr.jung.JungUtils.java

static Line2D getLineInMiddle(Shape edgeShape) {
    GeneralPath path = new GeneralPath(edgeShape);
    float[] seg = new float[6];
    List<Point2D> points = new ArrayList<>();

    for (PathIterator i = path.getPathIterator(null, 1); !i.isDone(); i.next()) {
        i.currentSegment(seg);/*ww w .  j  a  v a 2  s.  co  m*/
        points.add(new Point2D.Float(seg[0], seg[1]));
    }

    Point2D first = points.get(0);
    Point2D last = points.get(points.size() - 1);

    if (first.equals(last)) {
        Point2D minP = points.stream().min((p1, p2) -> Double.compare(p1.getY(), p2.getY())).get();

        return new Line2D.Float(minP, new Point2D.Float((float) (minP.getX() + 1.0), (float) minP.getY()));
    } else {
        for (int i = 0; i < points.size() - 1; i++) {
            Point2D p1 = points.get(i);
            Point2D p2 = points.get(i + 1);

            if (p2.distance(last) < p2.distance(first)) {
                Line2D ortho = getOrthogonal(new Line2D.Float(first, last));
                Point2D pp1 = getIntersection(new Line2D.Float(p1, p2), ortho);
                Point2D pp2 = new Point2D.Float((float) (pp1.getX() + last.getX() - first.getX()),
                        (float) (pp1.getY() + last.getY() - first.getY()));

                return new Line2D.Float(pp1, pp2);
            }
        }

        return null;
    }
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * <p>Returns a transform to position the arrowhead on this edge shape at the
 * point where it intersects the passed vertex shape.</p>
 * /*from w ww  .  j a  v a 2 s .com*/
 * <p>The Loop edge is a special case because its staring point is not inside
 * the vertex. The passedGo flag handles this case.</p>
 * 
 * @param edgeShape
 * @param vertexShape
 * @param passedGo - used only for Loop edges
 */
public AffineTransform getReverseArrowTransform(GeneralPath edgeShape, Shape vertexShape, boolean passedGo) {
    float[] seg = new float[6];
    Point2D p1 = null;
    Point2D p2 = null;

    AffineTransform at = new AffineTransform();
    for (PathIterator i = edgeShape.getPathIterator(null, 1); !i.isDone(); i.next()) {
        int ret = i.currentSegment(seg);
        if (ret == PathIterator.SEG_MOVETO) {
            p2 = new Point2D.Float(seg[0], seg[1]);
        } else if (ret == PathIterator.SEG_LINETO) {
            p1 = p2;
            p2 = new Point2D.Float(seg[0], seg[1]);
            if (passedGo == false && vertexShape.contains(p2)) {
                passedGo = true;
            } else if (passedGo == true && vertexShape.contains(p2) == false) {
                at = getReverseArrowTransform(new Line2D.Float(p1, p2), vertexShape);
                break;
            }
        }
    }
    return at;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Returns a transform to position the arrowhead on this edge shape at the
 * point where it intersects the passed vertex shape.
 *//*  w  w w  .  j  av  a  2  s  . c om*/
public AffineTransform getArrowTransform(GeneralPath edgeShape, Shape vertexShape) {
    float[] seg = new float[6];
    Point2D p1 = null;
    Point2D p2 = null;
    AffineTransform at = new AffineTransform();
    // when the PathIterator is done, switch to the line-subdivide
    // method to get the arrowhead closer.
    for (PathIterator i = edgeShape.getPathIterator(null, 1); !i.isDone(); i.next()) {
        int ret = i.currentSegment(seg);
        if (ret == PathIterator.SEG_MOVETO) {
            p2 = new Point2D.Float(seg[0], seg[1]);
        } else if (ret == PathIterator.SEG_LINETO) {
            p1 = p2;
            p2 = new Point2D.Float(seg[0], seg[1]);
            if (vertexShape.contains(p2)) {
                at = getArrowTransform(new Line2D.Float(p1, p2), vertexShape);
                break;
            }
        }
    }
    return at;
}