Image.getGraphics() returns null
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);
精彩评论