2

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.

1 Answers1

1

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);
Rupert Horlick
  • 671
  • 5
  • 15
  • Thank you so much! You have no idea how many problems you saved me from... Its incredibly annoying to have to run 5 times untill it works! –  Sep 01 '11 at 19:49
  • You're welcome! If it fixes the problem be sure to accept the answer so the question is closed! – Rupert Horlick Sep 01 '11 at 19:53