简体   繁体   English

Image.getGraphics()返回null

[英]Image.getGraphics() returns null

Here is the code (I am extending JFrame in this class) (Point 'size' is the size of the screen): 这是代码(我在此类中扩展JFrame)(点“ size”是屏幕的大小):

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". 我知道createImage方法存在问题,因为它在标题中表示“如果组件不可显示,则返回值可能为null”。 Yet I setVisible(true)! 但是我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. 它一直在定期工作,而不工作,也许工作10次,然后不工作3次,然后重复该循环。

I have tried casting createImage to BufferedImage, suggested by many google searches by me, but the problem still occurs. 我尝试将createImage强制转换为BufferedImage,这是我在许多Google搜索中建议的,但问题仍然存在。

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. 我也尝试过不扩展jframe,而是创建一个'JFrame jframe = new JFrame()',并使用它来绘制/等,但是问题仍然存在。

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);

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

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