java - 尝试在 Swing 中保存图形图像会导致图像被截断,每隔几张图像
问题描述
我正在尝试使用BufferedImage
,Graphics2D
和保存图形图像JAIHelper
,但由于某种原因,每 4-5 张图片中的一张在缺少数据的情况下保存(图像被切断)。首先我得到了一个NullPointerexception
,然后我稍微改变了它,现在它变得更好了,但仍然每 4-5 个图像我都会得到一个截断的图像。
这是我的代码:
@Override
public void saveGraph(State state, String fileName, IndexTable xVals, IndexTable yVals) {
try {
graphPanel.updateGraph(xVals, yVals);
JPanel panel = graphPanel.getGraph();
JFrame frame = new JFrame();
try {
frame.add(panel);
frame.setSize(500, 350);
frame.setVisible(true);
BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
try {
panel.paint(graphics);
JAIHelper.saveImage(PlanarImage.wrapRenderedImage(image), fileName);
} finally {
if (graphics != null) {
graphics.dispose();
}
}
} finally {
frame.dispose();
}
} catch (Exception e) {
e.printStackTrace();
}
}
解决方案
推荐阅读
- github - GitHub Linguist 的 language.yml 是否允许在扩展中使用通配符?
- python - 是否可以通过 doctest 测试使用 get_type_hints 的函数?
- c# - C# - 如何在 Linq 中执行 SUM() / SUM()
- c++ - 你能安全地在 Lua CFunction 中弹出函数参数吗?
- automated-tests - 空手道; 单个场景中的多个 url
- google-cloud-dataflow - 如何使用 Apache Beam Python SDK 在两组 json 存储桶上执行“差异”?
- r - 如何修复“FUN中的错误(X [[i]],...):运算符'<-'未实现”在R中
- c# - azure 服务总线的 Azure Function App 延迟重试
- r - 如何洗牌表示为R中总和的函数的一部分?
- java - 向注册表中的环境添加多个值并在 Java 应用程序中检索