2012-07-15 65 views
2

BufferedImage具有返回圖像的getSubimage(x,y,width,height)方法。我正在JPanel上繪製圖形,如何使用JPanel做類似於BufferedImage方法的操作?如何獲取JPanel的Subimage?

例如(只要它是正確的):

BufferedImage bi = jPanel.getSubimage(x, y, width, height); 

謝謝

回答

0

你或許可以提供任何Swing組件到一個緩衝的圖像。

因爲你只需要一個子圖像,我想這會做一個2步的過程,1)呈現你的JPanel到一個BufferedImage,2)獲取這個子圖像。

BufferedImage bi = new BufferedImage(jPanel.getWidth(), jPanel.getHeight(), BufferedImage.TYPE_INT_RGB); 
Graphics2D g2 = (Graphics2D)bi.getGraphics(); 
panel.printAll(g); //I assume you wanted child elements too... otherwise just use paint(g) 
BufferedImage result= bi.getSubimage(...); 

注意:這隻會在所有組件初始化,定位等並顯示後纔有效。

+0

非常感謝你:) – 2012-07-15 05:26:29