Returns a list of a shape's segments as they are returned by its path iterator. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Returns a list of a shape's segments as they are returned by its path iterator.

Demo Code


import java.awt.BasicStroke;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.FlatteningPathIterator;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Main{
    /**/*  ww w.j a  va  2 s.com*/
     * Returns a list of a shape's segments as they are returned by its path
     * iterator.
     *
     * @param shape Shape to be iterated.
     * @return A list of path segment objects.
     */
    public static List<PathSegment> getSegments(Shape shape) {
        PathIterator path = shape.getPathIterator(null);

        Point2D pointStart = null, pointEnd = null;
        double[] coords = new double[6];
        List<PathSegment> segments = new LinkedList<PathSegment>();
        while (!path.isDone()) {
            int type = path.currentSegment(coords);

            if (type == PathIterator.SEG_MOVETO
                    || type == PathIterator.SEG_LINETO) {
                pointEnd = new Point2D.Double(coords[0], coords[1]);
            } else if (type == PathIterator.SEG_QUADTO) {
                pointEnd = new Point2D.Double(coords[2], coords[3]);
            } else if (type == PathIterator.SEG_CUBICTO) {
                pointEnd = new Point2D.Double(coords[4], coords[5]);
            }

            PathSegment segment = new PathSegment(type, pointStart,
                    pointEnd, coords);
            segments.add(segment);

            pointStart = pointEnd;
            path.next();
        }

        return segments;
    }
}

Related Tutorials