填充Generalpath会导致不需要的结果

时间:2017-03-14 14:47:11

标签: java graphics graphics2d fill polyline

为了以后创建随机粒子(在游戏中),我创建了一些函数,它们获取椭圆的片段,自定义它们,然后绘制它们。这适用于g.draw(... the_line ..)[Ellipse outer_Bounds得到],但是一旦我使用g.fill(... the_line ...),内部部分就没有被正确填充。

如何修复:错误的移动点...只需移动最初的pi.SEG_Moveto中的moveto语句

The resulting graphic (ignore the number and the little box behind it)

private Ellipse2D.Double oval=new Ellipse2D.Double(100, 100, 100, 100);
private ArrayList<double[]> Ellipse_Generation_Points = new ArrayList<double[]>();
private double [] vals = new double[6];

public void Projectile_Basis(){
    for (PathIterator pi = oval.getPathIterator(null); !pi.isDone(); pi.next()){

        //currentSegment übergibt Type und Koordinatenpaare
        int type = pi.currentSegment(vals);
        //System.out.println(type);

        //Überprüfung des Übergebenen Types, Unterschiedliche Typen geben eine unterschiedliche Anzahl an Koordinaten-Paaren an
        //Type benötigt, um später die Form korrekt nachzuzeichnen
        if (type==pi.SEG_MOVETO||type==pi.SEG_LINETO) {
            double [] pathIteratorCoords = {type, vals[0], vals[1]};
            Ellipse_Generation_Points.add(pathIteratorCoords);
        }

        else if (type==pi.SEG_QUADTO){
            double [] pathIteratorCoords = {type, vals[0], vals[1], vals[2], vals[3]};
            Ellipse_Generation_Points.add(pathIteratorCoords);
        }

        else if (type==pi.SEG_CUBICTO){
            //RANDOMIZER
            vals[0]+=Math.random()*50;
            vals[1]-=Math.random()*50;

            double [] pathIteratorCoords = {type, vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]};
            Ellipse_Generation_Points.add(pathIteratorCoords);

        }

        else if (type==pi.SEG_CLOSE){
            double [] pathIteratorCoords = {type, vals[0], vals[1]};
            Ellipse_Generation_Points.add(pathIteratorCoords);
        }

    }
}

public GeneralPath Generate_Random_Particle(){
    PathIterator pi = oval.getPathIterator(null);
    double [] start_element = new double [3]; // Zum Schließen des Objekts
    double [] last_element = new double [3];

    GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, Ellipse_Generation_Points.size());

    for(int i=0; i<Ellipse_Generation_Points.size(); i++){


        //Übergabe des Double_Arrays aus Projectile_Basis, spätere Aufteilung in die einzelnen Bausteine
        double [] current_element=Ellipse_Generation_Points.get(i);

        if (current_element[0]==PathIterator.SEG_MOVETO){

        start_element=current_element;  //MOVETO only am Start des Objekts
        last_element=current_element;   

        }

            else if (current_element[0]==PathIterator.SEG_LINETO){

                polyline.moveTo(last_element[1], last_element[2]);

                polyline.lineTo(current_element[1], current_element[2]);

                last_element[1]=current_element[1];
                last_element[2]=current_element[2];

            }
            else if (current_element[0]==PathIterator.SEG_QUADTO){

                polyline.moveTo(last_element[1], last_element[2]);

                polyline.quadTo(
                                        current_element[1], current_element[2],
                                        current_element[3], current_element[4]
                                );

                last_element[1]=current_element[3];
                last_element[2]=current_element[4];

            }
            else if (current_element[0]==PathIterator.SEG_CUBICTO){

                polyline.moveTo(last_element[1], last_element[2]);

                polyline.curveTo(
                                        current_element[1], current_element[2],
                                        current_element[3], current_element[4],
                                        current_element[5], current_element[6]  
                                );

                last_element[1]=current_element[5];
                last_element[2]=current_element[6];

            }
            else if (current_element[0]==PathIterator.SEG_CLOSE){

                polyline.moveTo(start_element[1], start_element[2]);
                polyline.closePath();

            }
    }

    return polyline;
}

0 个答案:

没有答案