2016-05-13 21 views
-1

我目前有一個一維雙陣列,其中包含50個不同的點,意思是間隔1分開。我需要通過圖像中的線條來繪製和連接這些點。目前正在製作PNG圖像,如果我添加一行代碼就可以工作,但不知何故,這個循環會使整個圖像變成黑色。關於發生什麼問題的任何想法?使用Java中的點陣創建PNG圖像(總是顯示爲黑色)

BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB); 

    Graphics2D ig2 = bi.createGraphics(); 
    ig2.setBackground(Color.white); 
    ig2.setColor(Color.red); 

    for(int i = 0; i < 49; i++){ 

     Shape line = new Line2D.Double(i,finalpoints[i],i+1,finalpoints[i+1]); 
     ig2.draw(line); 

    } 

    //Export the result to a file 
    try { 
     ImageIO.write(bi, "PNG", new File("C://Users/vince/Desktop/heightmap.png")); 
    } catch (IOException e) { 
     System.out.println("There was an error writing the image to file"); 

    } 
+0

是你的所有在你爲圖像設置的10x10尺寸內的點? – iestync

+0

它實際上是一個50x50尺寸的圖像。尺寸不會改變輸出。 –

+0

'終點'數組中的值是什麼? –

回答

0

有兩種重載Line2D.Double構造:所述第一一個採用兩個的Point2D作爲參數,所以如果您的數組中包含的Point2D對象代碼應該是:

Shape line = new Line2D.Double(finalpoints[i],finalpoints[i+1]); 

第二種方法Line2D.Double(雙X1,Y1雙,雙X2雙Y2) ,它需要點的座標,所以如果你想要第二個,旅遊代碼應該是這樣的:

Shape line = new Line2D.Double(finalpoints[i].getX(), finalpoints[i].getY(), finalpoints[i+1].getX(), finalpoints[i+1].getY()); 

如果您AR ray不包含Point2D對象,只需更新您的文章,以便我們可以幫助您。

0

設置前景色不會填充背景。 也需要Graphics.dispose()

BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB); 

Graphics2D ig2 = bi.createGraphics(); 
ig2.setBackground(Color.white); 
ig2.setColor(Color.white); 
ig2.fillRect(0, 0, 50, 50); 
ig2.setColor(Color.red); 

// Better use a ig2.drawPolyline (Polygon) so the joints are nicer. 
for(int i = 0; i < 49; i++){ 

    Shape line = new Line2D.Double(i,finalpoints[i],i+1,finalpoints[i+1]); 
    ig2.draw(line); 

} 
ig2.dispose(); 
相關問題