I have a quick question. How to get image created by JComponent.paint or paintComponent?
I have a JComponent that I use as a "workspace" and where I rewrote the paintComponent method with my own. The thing is, there are also children in my JComponent workspace who have their own paintComponent methods.
So, when Swing displays my workspace component, it displays the graphics of the workspace, and then its children.
However, I want to get an image that generates my component of the workspace (which includes workspace graphics and graphics for children).
How to do it?
I tried calling the paintComponent / paint-method myself using my own graphics, but I just returned the black image. Here is what I tried:
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;
}
Any thoughts or comments are welcome! :)
source
share