Paint java GUI component to image file
- by Simon
Let's say I have
JButton test = new JButton("Test Button");
and I want to draw the button into an image object and save it to a file.
I tried this:
BufferedImage b = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
test.paint(b.createGraphics());
File output = new File("C:\\screenie.png");
try
{
ImageIO.write(b, "png", output);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
This code produced an empty 500x500 PNG-file. Does anyone know how I can draw the GUI component to an image file?