How do you convert a JPanel
to a File
? My panel has a moving Image
and I want to get individual frames.
答案 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.