Returns a clone of a specified shape which has a reversed order of the points, lines and curves. - Java java.lang

Java examples for java.lang:Math Curve

Description

Returns a clone of a specified shape which has a reversed order of the points, lines and curves.

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{
    /**/*from ww w .  j a v  a 2  s  .co  m*/
     * Returns a clone of a specified shape which has a reversed order of the
     * points, lines and curves.
     *
     * @param shape Original shape.
     * @return Shape with reversed direction.
     */
    public static Shape reverse(Shape shape) {
        List<PathSegment> segments = getSegments(shape);

        boolean closed = false;
        Path2D reversed = new Path2D.Double(Path2D.WIND_NON_ZERO,
                segments.size());
        ListIterator<PathSegment> i = segments
                .listIterator(segments.size());
        while (i.hasPrevious()) {
            PathSegment segment = i.previous();

            if (segment.type == PathIterator.SEG_CLOSE) {
                closed = true;
                continue;
            }

            if (reversed.getCurrentPoint() == null) {
                reversed.moveTo(segment.end.getX(), segment.end.getY());
            }
            if (segment.type == PathIterator.SEG_LINETO) {
                reversed.lineTo(segment.start.getX(), segment.start.getY());
            } else if (segment.type == PathIterator.SEG_QUADTO) {
                reversed.quadTo(segment.coords[0], segment.coords[1],
                        segment.start.getX(), segment.start.getY());
            } else if (segment.type == PathIterator.SEG_CUBICTO) {
                reversed.curveTo(segment.coords[2], segment.coords[3],
                        segment.coords[0], segment.coords[1],
                        segment.start.getX(), segment.start.getY());
            } else if (segment.type == PathIterator.SEG_MOVETO) {
                if (closed) {
                    reversed.closePath();
                    closed = false;
                }
            }
        }

        return reversed;
    }
    /**
     * 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