2011-11-15 89 views
-1

我在做一個遊戲,我想自己畫山。我使用中點位移來製作山脈,將點存儲在一個數組列表中,然後在我的視圖中在我的Jpanel中檢索它們。我畫了其他的東西,像草,G2會填充顏色,但不是我的山。下面是結果:Graphics2D不會填充路徑

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

下面的代碼:

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); 
     }    
    } 
+0

1)爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 2)你有問題嗎? –

+0

我去編輯帖子以線上截屏,但選擇不在線內。這是一個不太清楚,並且像素表示很大的簡單問題。 3)爲了製作精美的屏幕截圖,請參閱[如何創建屏幕截圖?]的提示(http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-說明-A-POST)。最重要的提示是a)小! b)白色BG。除了這些提示之外,請勿在上傳之前調整圖像大小。調整大小通常會使圖像*以字節爲單位變大,但不夠清晰和精確。 –

+0

*「不要調整圖像大小」*(愚蠢的笑容)忽略那個評論 - 這是我的瀏覽器在調整大小(從巨大的1632x1224)。 –

回答

2

如果問題是,答案是「我如何用顏色填充的鋸齒線以下​​的區域?」:

  1. 加入到RHS容器底部的末端(推測是ArrayList點的右側)。
  2. 將該點加入底部LHS。
  3. 然後調用path.closePath();

,或者換另外一種方式,通過「地」加入路徑結束路徑開始。

如果這個解釋不能解決您的問題(或者我猜錯了問題),請發佈SSCCE。

+0

是的,這是我的問題!我知道解析代碼有點複雜,我必須使用MVC作爲項目,以便將模型從視圖中分離出來。無論如何,path.closePath()不起作用。我想我的問題一般是什麼使填充方法不工作的路徑。 –

+0

調試了一下之後,我很確定路徑中必須有TINY:P孔,這就是爲什麼它沒有填充。我認爲這是填充工作正常的條件,對吧? –

+0

如果您想獲得更多幫助,請發佈SSCCE。 –