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 theVolatileImage
is not compatible with the currentGraphicsConfiguration
. This incompatibility can occur if the image was created with oneGraphicsConfiguration
and then drawn into another. For example, in a multiple-monitor situation, theVolatileImage
exists is associated with a particularGraphicsConfiguration
. Copying that image onto a differentGraphicsConfiguration
could cause unpredictable results. To correct this problem, you need to create a newVolatileImage
that is compatible with the currentGraphicsConfiguration
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
.
精彩评论