GUI component for graphic file

Say I have

JButton test = new JButton("Test Button");

and I want to draw a 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 created an empty PNG file of size 500x500. Does anyone know how I can draw a GUI component to an image file?

+3
source share
1 answer

The image is not empty, it contains a button of size 0x0 in 0,0.

Decision. You must add a layout or set the size of the button manually.

Note. To test it, first render the component on a JFrame. This allows you to quickly see what happens.

+1
source

Source: https://habr.com/ru/post/1749186/


All Articles