Graphics2D不会填充路径

时间:2022-06-01 12:33:50

I'm making a game and I want to draw mountains myself. I make the mountains using mid point displacement, store the points in an arraylist and then retrieve them in my Jpanel in my view. I draw other stuff like grass and g2 will fill them with colors but not my mountain. Here is the result:

我正在制作游戏,我想自己画山。我使用中点位移制作山脉,将点存储在一个arraylist中,然后在我的视图中将其检索到我的Jpanel中。我绘制其他东西,如草和g2将填充颜色,但不是我的山。结果如下:

http://i.imgur.com/5ty3C.png

Here's the code:

这是代码:

Point2D.Double start = new Point2D.Double(50, 400);
listePoints.add(start);

Point2D.Double end = new Point2D.Double(modele.getLargeur(), 400);
listePoints.add(end);       
this.maxIterations = 9;
int iterations = 0;
int minHeight = 5;
double nbrRandom = 10;
    while(iterations <  this.maxIterations) {
        iterations ++;
        int counter = 0;
        int size = listePoints.size()-1;
        int index = 0;
        while (compteur < size) {

            Point2D.Double point1 = listePoints.get(index);
            Point2D.Double point2 = listePoints.get(index+1);
            double milieu = Math.abs(point2.x - point1.x)/2;

            int orientation = Equations.randInt(1);
            switch(orientation ) {
            case 0: orientation  = -1;break;
            case 1: orientation  = 1;break;
            }


            Point2D.Double point3 = new Point2D.Double(point1.x+milieu,point1.y+(Equations.rand((nbrRandom+iterations = 0;
int minHeight)*orientation)));
            nbrRandom = nbrRandom /2;

            listePoints.add(index+1,point3);
            index +=2;
            counter++ ;
    }       

    }
    Point2D.Double point1 = new Point2D.Double(start.x,500);

    listePoints.add(0,point1);

    point1 = new Point2D.Double(end.x,500);

    listePoints.add(listePoints.size(),point1);

    point1 = new Point2D.Double(start.x,500);

    listePoints.add(listePoints.size(),point1);

/***************************** VIEW **/


        Path2D.Double path = new Path2D.Double(Path2D.Double.WIND_EVEN_ODD);    
        for (int j = 0;j < list.size()-1; j++) {


            Point2D.Double point1 = list.get(j);    
            Point2D.Double point2 =list.get(j+1);   


            path.moveTo(point1.x, point1.y);
            path.lineTo(point2.x, point2.y);

            path.closePath();
            g2.draw(path);
            g2.fill(path);
        }               
    }

1 个解决方案

#1


2  

If the question is "How do I fill the area below the jagged line with color?", the answer is:

如果问题是“如何填充锯齿线以下​​区域的颜色?”,答案是:

  1. Join the end (presumably the right-hand side of the ArrayList of points) to the bottom of the container on the RHS.
  2. 将末尾(可能是点列表的右侧)加到RHS上容器的底部。

  3. Join that point to the bottom LHS.
  4. 将这一点加入底层LHS。

  5. Then call path.closePath();
  6. 然后调用path.closePath();

Or to put that another way, join the path end to the path start via the 'ground'.

或者换句话说,通过'ground'将路径结束连接到路径start。

If this explanation does not solve the problem for you (or if I guessed the question wrong), post an SSCCE.

如果这个解释没有为您解决问题(或者如果我猜错了),请发布SSCCE。

#1


2  

If the question is "How do I fill the area below the jagged line with color?", the answer is:

如果问题是“如何填充锯齿线以下​​区域的颜色?”,答案是:

  1. Join the end (presumably the right-hand side of the ArrayList of points) to the bottom of the container on the RHS.
  2. 将末尾(可能是点列表的右侧)加到RHS上容器的底部。

  3. Join that point to the bottom LHS.
  4. 将这一点加入底层LHS。

  5. Then call path.closePath();
  6. 然后调用path.closePath();

Or to put that another way, join the path end to the path start via the 'ground'.

或者换句话说,通过'ground'将路径结束连接到路径start。

If this explanation does not solve the problem for you (or if I guessed the question wrong), post an SSCCE.

如果这个解释没有为您解决问题(或者如果我猜错了),请发布SSCCE。