Java 缓冲区策略:编译器不喜欢它
我正在尝试在画布上使用双缓冲,这是我以前从未做过的事情。我利用了在线教程,并设置了以下代码来实例化画布并为其设置缓冲。我编写了完整的过程,包括渲染图形(此处未显示),并且编译器接受它。
volCanvas = new VolCanvas();
volCanvas.setBackground(Color.black);
volCanvas.setBounds(10, 380, 1180, 125);
add(volCanvas);
volCanvas.createBufferStrategy(2); (Program blows up here)
offScreen = volCanvas.getBufferStrategy();
ofsg = (Graphics2D) offScreen.getDrawGraphics();
但程序在下面代码中的标记行处崩溃了。运行时抛出非法状态异常,并解释“组件必须有一个有效的对等点”。
据我所知,源代码基本上与我在几个示例中看到的一样,所以我不知道这里发生了什么。任何帮助将不胜感激。
谢谢,
约翰·多纳
I am attempting to use double buffering with a canvas, something I've never done before. I took advantage of the tutorials online, and set up the following code to instantiate a canvas and set up the buffering for it. I coded the complete process including the rendering graphiocs (not shown here), and the compiler accepts it.
volCanvas = new VolCanvas();
volCanvas.setBackground(Color.black);
volCanvas.setBounds(10, 380, 1180, 125);
add(volCanvas);
volCanvas.createBufferStrategy(2); (Program blows up here)
offScreen = volCanvas.getBufferStrategy();
ofsg = (Graphics2D) offScreen.getDrawGraphics();
But the program blows up at the flagged line in the code below. The runtime throws an illegal state exception, with the explanation "Component must have a valid peer".
So far as I can tell, the source code is essentially as I've seen it in several examples, so I haven't a clue what is going on here. Any help would be greatly appreciated.
Thanks,
John Doner
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这基本上意味着您的 GUI 不可见,或者您尚未将组件添加到可见的 GUI。
您在论坛上的其他问题涉及 Swing 应用程序。您不应该使用 AWT 组件(Canvas)在 Swing 应用程序中,在 JComponent 或 JPanel 上进行自定义绘制默认情况下是双缓冲的。
It basically means your GUI is not visible or you haven't added your component to a visible GUI.
Your other questions on the forum deal with Swing applications. You should not use an AWT component (Canvas) in a Swing application. Do custom painting on a JComponent or JPanel. Swing is double buffered by default.