Constructs a geometric shape with single precision from a list of path segments. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Constructs a geometric shape with single precision from a list of path segments.

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 .  jav a 2s .c  om
     * Constructs a geometric shape with single precision from a list of path
     * segments.
     *
     * @param segments List of path segments.
     * @return A geometric shape.
     */
    private static Shape getShapeFloat(List<PathSegment> segments) {
        Path2D.Float path = new Path2D.Float(Path2D.WIND_NON_ZERO,
                segments.size());
        for (PathSegment segment : segments) {
            float[] coords = new float[segment.coords.length];
            for (int i = 0; i < coords.length; i++) {
                coords[i] = (float) segment.coords[i];
            }
            if (segment.type == PathIterator.SEG_MOVETO) {
                path.moveTo(coords[0], coords[1]);
            } else if (segment.type == PathIterator.SEG_LINETO) {
                path.lineTo(coords[0], coords[1]);
            } else if (segment.type == PathIterator.SEG_QUADTO) {
                path.quadTo(coords[0], coords[1], coords[2], coords[3]);
            } else if (segment.type == PathIterator.SEG_CUBICTO) {
                path.curveTo(coords[0], coords[1], coords[2], coords[3],
                        coords[4], coords[5]);
            } else if (segment.type == PathIterator.SEG_CLOSE) {
                path.closePath();
            }
        }
        return path;
    }
}

Related Tutorials