convert GeneralPath To Curve - Java java.lang

Java examples for java.lang:Math Curve

Description

convert GeneralPath To Curve

Demo Code


//package com.java2s;

import java.awt.geom.*;
import java.util.ArrayList;

public class Main {
    public static GeneralPath convertToCurve(GeneralPath gpIni) {
        GeneralPath gp = new GeneralPath();

        ArrayList alPuntsDelPath = getPoints(gpIni);
        PathIterator pi = gpIni.getPathIterator(null);
        float af[] = new float[6];
        int j = 0;
        for (; !pi.isDone(); pi.next()) {
            int i = pi.currentSegment(af);
            if (i == PathIterator.SEG_MOVETO) {
                gp.moveTo(af[0], af[1]);
                j++;//from  w  w w .java 2  s.  c o m
            } else if (i == PathIterator.SEG_LINETO) {
                if (j + 3 < alPuntsDelPath.size()) {
                    gp.curveTo((float) ((Point2D.Float) alPuntsDelPath
                            .get(j)).getX(),
                            (float) ((Point2D.Float) alPuntsDelPath.get(j))
                                    .getY(),
                            (float) ((Point2D.Float) alPuntsDelPath
                                    .get(j + 1)).getX(),
                            (float) ((Point2D.Float) alPuntsDelPath
                                    .get(j + 1)).getY(),
                            (float) ((Point2D.Float) alPuntsDelPath
                                    .get(j + 2)).getX(),
                            (float) ((Point2D.Float) alPuntsDelPath
                                    .get(j + 2)).getY());
                    j += 3;
                    pi.next();
                    pi.next();
                } else {
                    gp.lineTo(af[0], af[1]);
                    j++;
                }
            } else if (i == PathIterator.SEG_LINETO) {
                if (j + 2 < alPuntsDelPath.size()) {
                    gp.quadTo((float) ((Point2D.Float) alPuntsDelPath
                            .get(j)).getX(),
                            (float) ((Point2D.Float) alPuntsDelPath.get(j))
                                    .getY(),
                            (float) ((Point2D.Float) alPuntsDelPath
                                    .get(j + 1)).getX(),
                            (float) ((Point2D.Float) alPuntsDelPath
                                    .get(j + 1)).getY());
                    j += 2;
                    pi.next();
                } else {
                    gp.lineTo(af[0], af[1]);
                    j++;
                }
            } else if (i == PathIterator.SEG_CLOSE) {
                gp.closePath();
            }
        }
        return gp;
    }

    /** Retorna els punts finals de cada segment de la figura */
    private static java.util.ArrayList getPoints(GeneralPath gp) {
        ArrayList alPuntsDelPath = new ArrayList();
        PathIterator pi = gp.getPathIterator(null);
        float af[] = new float[6];
        for (; !pi.isDone(); pi.next()) {
            int i = pi.currentSegment(af);
            if (i == PathIterator.SEG_MOVETO) {
                alPuntsDelPath.add(new java.awt.geom.Point2D.Float(af[0],
                        af[1]));
            } else if (i == PathIterator.SEG_LINETO) {
                alPuntsDelPath.add(new java.awt.geom.Point2D.Float(af[0],
                        af[1]));
            } else if (i == PathIterator.SEG_QUADTO) {
                alPuntsDelPath.add(new java.awt.geom.Point2D.Float(af[2],
                        af[3]));
            } else if (i == PathIterator.SEG_CUBICTO) {
                alPuntsDelPath.add(new java.awt.geom.Point2D.Float(af[4],
                        af[5]));
            } else if (i == PathIterator.SEG_CLOSE) {
                Point2D primerPunt = (Point2D) alPuntsDelPath.get(0);
                alPuntsDelPath.add(primerPunt.clone());
            }
        }
        return alPuntsDelPath;
    }
}

Related Tutorials