Image.getGraphics() 返回 null

发布于 2024-12-02 16:25:16 字数 489 浏览 1 评论 0原文

这是代码(我在此类中扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2024-12-09 16:25:16

这来自此处

这些示例创建与屏幕兼容的缓冲图像:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice(); 
GraphicsConfiguration gc = gs.getDefaultConfiguration();

// Create an image that does not support transparency 

bimage = gc.createCompatibleImage(width, height, Transparency.OPAQUE);

// Create an image that supports transparent pixels 

bimage = gc.createCompatibleImage(width, height, Transparency.BITMASK);

// Create an image that supports arbitrary levels of transparency 

bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

This come from here.

These examples create buffered images that are compatible with the screen:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice(); 
GraphicsConfiguration gc = gs.getDefaultConfiguration();

// Create an image that does not support transparency 

bimage = gc.createCompatibleImage(width, height, Transparency.OPAQUE);

// Create an image that supports transparent pixels 

bimage = gc.createCompatibleImage(width, height, Transparency.BITMASK);

// Create an image that supports arbitrary levels of transparency 

bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文