我有一個快速的問題。如何獲得由JComponent.paint或paintComponent生成的圖像?如何獲取圖像paint/paintComponent生成的圖像?
我有一個JComponent,我用它作爲'工作區',並將paintComponent方法覆蓋到我自己的位置。問題是我的工作空間JComponent也有孩子,他們有自己的paintComponent方法。
因此,當Swing渲染我的工作區組件時,它呈現工作區圖形,然後渲染其子節點。
但是,我想要得到我的工作區組件生成的圖像(其中包括工作區圖形和兒童圖形)。
我該怎麼做?
我試圖通過使用我自己的圖形自己調用paintComponent/paint-method,但我只是返回了一個黑色圖像。這是我的嘗試;
public void paintComponent(Graphics g) {
if (bufferedImage != null) {
g.drawImage(bufferedImage, 0, 0, this);
}
else {
g.setColor(Color.WHITE);
g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
}
}
public BufferedImage getImage() {
BufferedImage hello = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = hello.getGraphics();
paintComponent(g);
return hello;
}
歡迎任何想法或意見! :)
方法'getImage'由用戶調用,這意味着應用程序有足夠的時間。 – user20298 2008-09-22 09:12:59