How do you convert a JPanel to a File?

时间:2017-04-06 16:45:35

标签: java image swing file jpanel

How do you convert a JPanel to a File? My panel has a moving Image and I want to get individual frames.

1 个答案:

答案 0 :(得分:4)

You can create a BufferedImage of the panel at different points in time and then save the image to a file.

The basic logic for this would be:

BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
panel.print( g2d );
g2d.dispose();
ImageIO.write(...);

Check out the Screen Image class for convenience methods that implement the above functionality.

Of course this won't be very efficient since you need a completely new image for each frame.