2014-09-01 53 views
0

繪製下圖如何在之前繪製如下圖?現在結束了......這將是漏斗狀的,所以我希望每個下一個橢圓形都在多邊形和圓弧下面。 我有這樣的代碼:Java Graphics2D:根據之前的

for (int i = 0; i < pdLen; i++) { 
     .... 
     .... 
     g2.fillPolygon(poly); 

     g2.fillOval(topLeft[0], topLeft[1] - 10, topRight[0] - topLeft[0], arcHeight); 

     g2.fillArc(botLeft[0], botLeft[1] - arcHeight/2, botRight[0] - botLeft[0], arcHeight, 0, -180); 
    } 

感謝 PS:按照座標是從先前的計算,所以對(INT I = pdLen-1; i> 0; I - ){將不起作用

UPD:

每個循環步驟計算topLeft和topRight。

 int[] topLeft = {(int)Math.round(startX + (procSum/onePixX)), (int)Math.round(startY + (procSum/onePixY))}; 
     int[] topRight = {(int)Math.round(topRightX - (procSum/onePixX)), (int)Math.round(startY + (procSum/onePixY))}; 
     procSum += (pieceData[i] * pieceProc); 

     int[] botLeft = {(int)Math.round(startX + (procSum/onePixX)), (int)Math.round(startY + (procSum/onePixY))}; 
     int[] botRight = {(int)Math.round(topRightX - (procSum/onePixX)), (int)Math.round(startY + (procSum/onePixY))}; 
     procSum += padProc; 

回答

2

您可以反轉循環。您可能不得不考慮座標的計算,但可以完成。

一般來說,在這種情況下,您應該發佈MCVE。這將爲每個人節省大量時間。

然而,便宜(即簡單)的解決辦法是什麼存儲必須要畫,然後以相反的順序繪製這些形狀:

List<Shape> ovals = new ArrayList<Shape>(); 
List<Shape> arcs = new ArrayList<Shape>(); 
for (int i = 0; i < pdLen; i++) { 
    //g2.fillOval(topLeft[0], topLeft[1] - 10, topRight[0] - topLeft[0], arcHeight); 
    ovals.add(new Ellipse2D.Double(topLeft[0], topLeft[1] - 10, topRight[0] - topLeft[0], arcHeight)); 
    //g2.fillArc(botLeft[0], botLeft[1] - arcHeight/2, botRight[0] - botLeft[0], arcHeight, 0, -180); 
    arcs.add(new Arc2D.Double(botLeft[0], botLeft[1] - arcHeight/2, botRight[0] - botLeft[0], arcHeight, 0, -180)); 
} 
for (int i=ovals.size()-1; i>=0; i--) 
{ 
    g2.fill(ovals.get(i)); 
    g2.fill(arcs.get(i)); 
} 
+0

感謝。 它的工作原理,後來會改變算法 – Gwalk 2014-09-01 13:23:10