VolatileImage JFrame on multiple screens

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

The problem occurs when I drag the game window onto another screen that did not initially appear in the window. The window turns gray and no graphics are displayed on the screen, even simple rectangles that I drew using Graphics2D. This only happens when I call the draw volatileimage method, as shown in the tutorial.

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

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); 

... but I'm not sure.

Any help would be greatly appreciated :) It would also be interesting to know if VolatileImage is the way I should go for my game, or if BufferedImage or something else is the best method for performance and frame rate :)

+4
source share
1 answer

Yes you are right. VolatileImage is device dependent. From the VolatileImage API User Guide, you can read:

A VolatileImage is device specific: if you created VolatileImage with one GraphicsDevice, you may not be able to copy this VolatileImage to another GraphicsDevice. For this reason, you need to call confirmation before trying to copy VolatileImage.

and

If the code is IMAGE_INCOMPATIBLE , then VolatileImage not compatible with the current GraphicsConfiguration . This incompatibility can occur if the image was created using one GraphicsConfiguration and then pulled into another. For example, in a multi-monitor situation, there is a VolatileImage associated with a particular GraphicsConfiguration . Copying this image to another GraphicsConfiguration can cause unpredictable results. To fix 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 using the VolatileImage.validate(gc) method and recreate the image on the new device. Please note that there are times when you cannot create a VolatileImage , in those cases you need to return to another Image implementation, such as BufferedImage .

+4
source

Source: https://habr.com/ru/post/1369365/


All Articles