I've looked over the forums but I can't find anything to help me. I've tried out some of the code but I still end up with a blank image.
What I'm trying to do is allow the user to draw an image in a JPanel and then I want to save it. It doesn't really matter what type of image. All I'm getting is a blank image of the JPanel. It will capture any background color I have in the JPanel but not anything the user has drawn.
I don't have any JFrame which from what I've read could be the problem. Is there any way to save the JPanel as an image without the use of a JFrame? The JPanel is on a LayeredPane along with a lot of other items.
Here's the code that just gives me the blank JPanel. What am I missing?
private void saveImage(JPanel p) {
try {
int w = p.getWidth(), h = p.getHeight();
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
p.setVisible(true);
p.paint(g);
ImageIO.write(image, "jpeg", new File("example.jpeg"));
}
catch (IOException e) {
System.err.println(e);
}
}
Thanks in advance.