在非光栅图形配置中显示 JFrame
您好,
我正在尝试在非屏幕设备中获取 JFrame 绘图。 JFrame 构造函数
JFrame(GraphicsConfiguration)
似乎允许这样做:
我的第一次尝试是创建自己的 GraphicsConfiguration,当调用 getType() 时,其 GraphicsDevice 报告 GraphicsDevice.TYPE_IMAGE_BUFFER 。
然而,JFrame.init 专门查找类型,如果类型不是 TYPE_RASTER_SCREEN,则抛出异常:
if (graphicsConfig.getDevice().getType() !=
GraphicsDevice.TYPE_RASTER_SCREEN) {
throw new IllegalArgumentException("not a screen device");
}
接下来,我尝试使返回的 GraphicsDevice 报告 GraphicsDevice.TYPE_RASTER_SCREEN。这使得 JFrame 能够正确初始化,但是当它显示它时,我得到了
Exception in thread "main" java.lang.ClassCastException: TestGraphicsConfiguration cannot be cast to sun.awt.X11GraphicsConfig
所以我已经用尽了关于如何绘制一个不显示在屏幕上但仍然完整的 JFrame 的想法布局合理且功能齐全。
我要掉进兔子洞了吗?或者这可以做到吗?
Greetings,
I'm trying to get a JFrame drawing in a non screen device. The JFrame constructor has a
JFrame(GraphicsConfiguration)
to seemingly allow this:
My First attempt was to create my own GraphicsConfiguration, who's GraphicsDevice reported GraphicsDevice.TYPE_IMAGE_BUFFER when getType() was called.
However JFrame.init specifically looks for the type and throws an exception if the type isn't TYPE_RASTER_SCREEN:
if (graphicsConfig.getDevice().getType() !=
GraphicsDevice.TYPE_RASTER_SCREEN) {
throw new IllegalArgumentException("not a screen device");
}
Next i tried to make the GraphicsDevice i returned report GraphicsDevice.TYPE_RASTER_SCREEN. This allowed the JFrame to be initialized correctly, but when it went to display it, I got
Exception in thread "main" java.lang.ClassCastException: TestGraphicsConfiguration cannot be cast to sun.awt.X11GraphicsConfig
So i've run out of ideas, on how to draw a JFrame that doesn't show up on the screen, but is never the less fully layed out and functional.
Am going down a rabbit hole here, or can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java 顶级容器,例如
JFrame
需要访问 对等组件 主机平台本机,通常通过 JNI。或者,您可以使用BufferedImage
或java.awt.headless
模式,如 这里。附录:
我不知道如何替换特定的对等组件,但可以调用特定于平台的本机组件; Java Native Access (JNA) 就是这样一种途径。作为一个极端的例子,这个 6502 JVM 在运行频率为 1 MHz 的 8 位处理器上以 128K 运行。演示(包括右下屏幕截图的源)是使用
javac
。A Java top-level container such as
JFrame
requires access to a peer component native to the host platform, typically via JNI. Alternatively, you may be able to use aBufferedImage
orjava.awt.headless
mode, as discussed here.Addendum:
I don't know how to replace a particular peer component, but it's possible to evoke platform-specific native components; Java Native Access (JNA) is one such avenue. As an extreme example, this 6502 JVM runs in 128K on an 8-bit processor running at 1 MHz. The demos, including source for the lower right screenshot, were compiled using
javac
.