Java component.createimage返回null,甚至headless()返回false

Java component.createimage返回null,甚至headless()返回false,java,graphics,jpanel,Java,Graphics,Jpanel,我正在尝试绘制一个图,其中对于双缓冲,我使用组件类的CreateImage方法。但使用与输入相同的属性,此方法随机返回空值。当我搜索时,我发现GraphicsEnvironment.headless返回true时会发生这种情况。然后我打印graphicsEnvironment.headless的值,它总是返回false。 createimage返回null时是否存在其他情况。我搜索了很多,但没有找到合适的答案。如 如果组件不可显示,则返回值可能为空 是的,如果headless为false,则会发

我正在尝试绘制一个图,其中对于双缓冲,我使用组件类的CreateImage方法。但使用与输入相同的属性,此方法随机返回空值。当我搜索时,我发现GraphicsEnvironment.headless返回true时会发生这种情况。然后我打印graphicsEnvironment.headless的值,它总是返回false。 createimage返回null时是否存在其他情况。我搜索了很多,但没有找到合适的答案。

如果组件不可显示,则返回值可能为空

是的,如果headless为false,则会发生这种情况,但如果组件未连接到显示的容器,也会发生这种情况。当组件连接到本机对等机时,它将变得可显示。如果组件从未在屏幕上显示,或者直接或间接连接到的窗口已被处理,则会发生这种情况

你可以用一些像

GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(width, height, transparency);
这将创建一个BuffereImage,该图像经过优化,可用于指定的图形配置