Java JFrame & JPanel 与 Repaint() 的差异
我一直在尝试一些简单的 Graphics2D
绘画,并从这里的社区获得了一些非常好的帮助。
我设法通过将代码从主 JFrame
类移到 JPanel
中,然后将其添加到 中,解决了“弹力球”的闪烁问题。 JFrame
类,谁能告诉我为什么这会产生如此大的差异?
I have been playing about with some simple painting of Graphics2D
and have some extremely good help from the community here.
I managed to get the flickering resolved from my "bouncy balls" by moving the code away from the main JFrame
class and into a JPanel
which I then added to the JFrame
class, can anyone tell me why this would make such a difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 JComponent 的 PaintComponent 方法(例如 JPanel 的)中绘制时,您可以使用 Swing,它在默认情况下绘制时使用双缓冲。直接在 JFrame 的 Paint 方法中绘图将只允许 AWT 类型绘图,因为 JFrame 直接继承自 Frame(一个重量级容器),并且 AWT 图形默认情况下不使用双缓冲,这将导致动画断断续续。
When you draw in a JComponent's paintComponent method (such as a JPanel's), you use Swing which uses double-buffering when drawing by default. Drawing directly in a JFrame's paint method will only allow AWT type drawing since the JFrame directly inherits from Frame, a heavy weight container, and since AWT graphics does not use double buffering by default and this will lead to choppy animation.