开发者

Image.getGraphics() returns null

开发者 https://www.devze.com 2023-04-01 03:53 出处:网络
Here is the code (I am extending JFrame in this class) (Point \'size\' is the size of the screen): setVisible(true);

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 pro开发者_Python百科blem 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消