returns a path equal to "path" but with the path point "pointToChange" at location of "new Point". - Java 2D Graphics

Java examples for 2D Graphics:Path

Description

returns a path equal to "path" but with the path point "pointToChange" at location of "new Point".

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   ww  w  . j  av a  2s .  c om*/
     * returns a path equal to "path" but 
     * with the path point "pointToChange" at location of "newPoint".
     */
    public static GeneralPath modifyPathPoint(GeneralPath path,
            Point2D pointToChange, Point2D newPoint) {

        GeneralPath newPath = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
        boolean seen = false;

        double seg[] = new double[6];
        PathIterator pi = path.getPathIterator(null);

        while (!pi.isDone()) {
            int segType = pi.currentSegment(seg);
            switch (segType) {
            case PathIterator.SEG_MOVETO:
                if (GeoUtils.isSamePoint(seg[0], seg[1], pointToChange)
                        && !seen) {
                    newPath.moveTo((int) newPoint.getX(),
                            (int) newPoint.getY());
                    seen = true;
                } else {
                    newPath.moveTo((int) seg[0], (int) seg[1]);
                }
                break;
            case PathIterator.SEG_LINETO:
                if (GeoUtils.isSamePoint(seg[0], seg[1], pointToChange)
                        && !seen) {
                    newPath.lineTo((int) newPoint.getX(),
                            (int) newPoint.getY());
                    seen = true;
                } else {
                    newPath.lineTo((int) seg[0], (int) seg[1]);
                }
                break;
            }
            pi.next();
        }
        return newPath;
    }
}

Related Tutorials