简体   繁体   English

如何将JPanel转换为文件?

[英]How do you convert a JPanel to a File?

How do you convert a JPanel to a File ? 如何将JPanel转换为File My panel has a moving Image and I want to get individual frames. 我的面板上有一个正在移动的Image ,我想获取单个帧。

You can create a BufferedImage of the panel at different points in time and then save the image to a file. 您可以在不同的时间点创建面板的BufferedImage ,然后将图像保存到文件中。

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. 请查看Screen Image类以获得实现上述功能的便捷方法。

Of course this won't be very efficient since you need a completely new image for each frame. 当然,这将不是非常有效,因为您需要为每一帧使用全新的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM