开发者

VolatileImage JFrame on multiple screens

I have a JFrame in which I am using Graphics2D to draw a VolatileImage using this tutorial. I have mainly copied the code to see how it works, but have slightly edited it for my game. I am running my computer with two screens.

The problem arises when I drag the window of the game onto the other screen which the window did not originally appear on. The window goes grey and no graphics are shown on screen, even the simple rectangles I have drawn with the Graphics2D. This only happens when I call for the draw method of the volatileimage as shown in the tutorial.

I believe it may have something to do with this...

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice开发者_开发知识库().getDefaultConfiguration();

...but I am not sure.

Any help would be greatly appreciated. It would also be interesting to know if VolatileImage is the way I should be going for my game or if BufferedImage or something else is a better method for performance and frame rate.


Yes, you are correct. VolatileImage is device-specific. From "The VolatileImage API User Guide" you can read:

A VolatileImage is device-specific: if you created a VolatileImage with one GraphicsDevice, you might not be able to copy that VolatileImage to another GraphicsDevice. For this reason, you need to call validate before attempting to copy the VolatileImage.

and

If the code is IMAGE_INCOMPATIBLE then the VolatileImage is not compatible with the current GraphicsConfiguration. This incompatibility can occur if the image was created with one GraphicsConfiguration and then drawn into another. For example, in a multiple-monitor situation, the VolatileImage exists is associated with a particular GraphicsConfiguration. Copying that image onto a different GraphicsConfiguration could cause unpredictable results. To correct this problem, you need to create a new VolatileImage that is compatible with the current GraphicsConfiguration

When dragging your frame to another screen device you need to check the result from the VolatileImage.validate(gc) method of your and recreate the image to the new device. Note that there are cases when you cannot create a VolatileImage, in those cases you need to fall back on another Image implementation like BufferedImage.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜