多个屏幕上的 VolatileImage JFrame

发布于 2024-12-01 22:18:16 字数 642 浏览 1 评论 0原文

我有一个 JFrame,其中使用 Graphics2D 使用 this 教程。我主要复制了代码来看看它是如何工作的,但为我的游戏稍微编辑了它。我正在运行带有两个屏幕的计算机。

当我将游戏窗口拖到最初没有出现该窗口的另一个屏幕上时,就会出现问题。窗口变成灰色,屏幕上没有显示任何图形,即使是我用 Graphics2D 绘制的简单矩形也是如此。仅当我调用 易失性图像的绘制方法时才会发生这种情况,如教程中所示。

我相信这可能与此有关……

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

但我不确定。

任何帮助将不胜感激。了解 VolatileImage 是否是我的游戏应该采用的方式,或者 BufferedImage 或其他东西是否是性能和帧速率更好的方法,这也很有趣。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2024-12-08 22:18:16

是的,你是对的。 VolatileImage 是特定于设备的。从“VolatileImage API 用户指南” 中您可以阅读:

VolatileImage 是特定于设备的:如果您使用一个设备创建了一个 VolatileImage
GraphicsDevice,您可能无法将该 VolatileImage 复制到另一个
图形设备。因此,您需要在尝试复制之前调用 validate
易失性图像。

如果代码是IMAGE_INCOMPATIBLE,则VolatileImage不是
与当前的GraphicsConfiguration兼容。这
如果图像是用一个创建的,则可能会出现不兼容的情况
GraphicsConfiguration 然后绘制到另一个中。 例如,
在多显示器情况下,VolatileImage 存在的是
与特定GraphicsConfiguration关联。复制那个
图像到不同的 GraphicsConfiguration 可能会导致
不可预测的结果。
要解决此问题,您需要创建一个
与当前版本兼容的新 VolatileImage
图形配置

将框架拖动到另一个屏幕设备时,您需要检查 VolatileImage.validate(gc) 方法并重新创建图像到新设备。请注意,有些情况下您无法创建 VolatileImage,在这些情况下您需要依靠另一个 Image 实现,例如 BufferedImage

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文