As per the JavaDocs, you need to pass ImageIO.write
an instance of RenderedImage
.
If you have a look at the JavaDocs for JFrame
you will find that it doesn't implement RenderedImage
, hence your problem.
If you have a look at just about any example of Image.write
you'll find that almost always use a BufferedImage
because it, unlike JFrame
, does implement RenderedImage
Okay, so the question becomes, "how do I paint the JFrame
to a BufferedImage
?"
The answer, if you look around, is quite simple.
BufferedImage img = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
frame.printAll(g2d);
g2d.dispose();
And now, you can save it...
ImageIO.write(img, "png", new File("filename.png"));