returns a path equal to "path" but with "pointToKill" removed. - Java 2D Graphics

Java examples for 2D Graphics:Path

Description

returns a path equal to "path" but with "pointToKill" removed.

Demo Code


import java.awt.geom.PathIterator;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.awt.Point;

public class Main{
    /** //from  w  ww .  j a v  a2s  .c  o m
     * returns a path equal to "path" but with "pointToKill" removed.
     */
    public static GeneralPath removePathPoint(GeneralPath path,
            Point2D pointToKill) {

        GeneralPath newPath = new GeneralPath(GeneralPath.WIND_EVEN_ODD);

        double seg[] = new double[6];
        for (PathIterator i = path.getPathIterator(null); !i.isDone(); i
                .next()) {
            int segType = i.currentSegment(seg);
            switch (segType) {
            case PathIterator.SEG_MOVETO:
                if (GeoUtils.isSamePoint(seg[0], seg[1], pointToKill)) {
                    i.next();
                    segType = i.currentSegment(seg);
                    if (i.isDone()) {
                        return null;
                    } else {
                        newPath.moveTo((int) seg[0], (int) seg[1]);
                    }
                } else {
                    newPath.moveTo((int) seg[0], (int) seg[1]);
                }
                break;
            case PathIterator.SEG_LINETO:
                if (!GeoUtils.isSamePoint(seg[0], seg[1], pointToKill)) {
                    newPath.lineTo((int) seg[0], (int) seg[1]);
                }
                break;
            }
        }
        return newPath;
    }
}

Related Tutorials