Image.getGraphics() 返回 null
这是代码(我在此类中扩展 JFrame)(点“大小”是屏幕的大小):
setVisible(true);
backBuffer = createImage(size.x, size.y);
backGraphics = backBuffer.getGraphics();
我知道 createImage 方法存在问题,正如描述中所说“返回值可能为 null 如果该组件不可显示”。但我设置了Visible(true)!这一直是我的程序中的一个问题,过去的解决方案都很奇怪。然而,这一次我似乎无法修复它。
它会周期性地工作和不工作,可能工作 10 次,然后不工作 3 次,如此循环往复。
我已经尝试将 createImage 转换为 BufferedImage,这是我的许多谷歌搜索建议的,但问题仍然出现。
我也尝试过不扩展 jframe,而是创建一个“JFrame jframe = new JFrame()”,并使用它来绘制/等,但问题仍然出现。
Here is the code (I am extending JFrame in this class) (Point 'size' is the size of the screen):
setVisible(true);
backBuffer = createImage(size.x, size.y);
backGraphics = backBuffer.getGraphics();
I know that the problem exists with the createImage method, as it says in the discription "return value may be null if the component is not displayable". Yet I setVisible(true)! This has been a problem throughout my programs, and solutions in the past have been strange. This time, however, I can't seem to fix it.
It has been periodically working and not working, maybe works for 10 runs then dosnt work for 3, and the cycle repeats.
I have tried casting createImage to BufferedImage, suggested by many google searches by me, but the problem still occurs.
I have also tried not extending jframe, but creating a 'JFrame jframe = new JFrame()', and using that to draw/etc, but the problem still occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这来自此处。
这些示例创建与屏幕兼容的缓冲图像:
This come from here.
These examples create buffered images that are compatible with the screen: