JFrame 中的 BufferStrategy 与 DIY 双缓冲

发布于 2024-08-17 12:41:27 字数 241 浏览 6 评论 0原文

到目前为止,我已经通过创建 Image 来完成双缓冲,使用其关联的 Graphics 对象在该 Image 上绘制我想要的内容,然后使用 Paint 方法的 Graphics 对象将该 Image 绘制到屏幕上。最近,我了解了 BufferStrategy 类及其用途。我想知道这两种方法的优缺点是什么。

编辑: 我认为我的问题没有说得很清楚。我想知道 DIY 方法和 BufferStrategy 的优缺点,以及何时(如果有的话)我应该使用其中一种。

Until now, I've done double buffering by creating and Image, drawing what I wanted to that Image using its associated Graphics object then draw that Image to the screen using the paint method's Graphics object. Recently, I learned about the BufferStrategy class and its uses. I was wondering what are the pros and cons of the two methods.

EDIT:
I dont't think I made my question very clear. I wanted to know the pros/cons of both the DIY method and the BufferStrategy and when, if ever, I should use one or the other.

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

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

发布评论

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

评论(2

叹沉浮 2024-08-24 12:41:27

通过小心地

这个优秀的 示例 必须双缓冲,因为它在初始线程上连续绘制,而不是在EDT。相比之下,这个相当繁忙的示例仅依赖于repaint()< /code> 调用以响应 Swing Timer。除了composite之外,我很少需要屏幕外缓冲区。最后,这篇教程文章提供了更多指南。

I've always had good results using the default BufferStrategy by being careful to

  • Always construct GUI components on the EDT
  • Never draw from a thread except the EDT

This excellent example must double buffer because it draws continually on the initial thread rather than the EDT. In contrast, this fairly busy example relies on nothing more than repaint() called in response to a Swing Timer. I rarely need an offscreen buffer except for a composite. Finally, this tutorial article offers more guidelines.

最好是你 2024-08-24 12:41:27

如果您还没有阅读过在 AWT 和 Swing 中绘画,我建议您阅读。

如果您使用 JFrame,我认为您通常不需要自己动手双缓冲。 Swing 内置了双缓冲,默认情况下处于打开状态。自己手动执行此操作只会减慢速度。您可以通过在任何 JComponent 上调用 isDoubleBufferingEnabled() 来检查双缓冲是否已打开。

在某些情况下,您可能想自己执行此操作,但这应该是例外而不是规则。也许您正在做一些涉及编写游戏的事情,在这种情况下我的建议可能不适用。无论如何,希望以上内容是有用的信息。

I recommend reading Painting in AWT and Swing if you haven't.

I don't think you usually need Do-It-Yourself double buffering if you're using JFrame. Swing has built in double buffering which is turned on by default. Manually doing this yourself will just slow things down. You can check if double buffering is on by calling isDoubleBufferingEnabled() on any of your JComponents.

There are cases where you may want to do this yourself, but that should be the exception rather than the rule. Maybe you're doing something involved like writing a game, in which case maybe my advice doesn't apply. Anyway, hopefully the above is useful info.

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