使用 awt 双缓冲

发布于 2024-09-02 03:02:11 字数 273 浏览 2 评论 0原文

awt 可以实现双缓冲(在 java 中)吗?目前,我知道 swing 不应该与 awt 一起使用,所以我不能使用 BufferStrategy 之类的东西(我已经有一些用 awt 编写的代码,我不想在 swing 中重写)。

如果 awt 可以实现双缓冲,我是否必须手动写入缓冲区?与swing不同,awt似乎没有同样内置的双缓冲能力。

如果我必须手动编写代码,有没有好的教程可以看?或者对于新手程序员来说使用 swing 更容易/更明智?

抱歉这个多步骤问题。感谢您的宝贵时间:)

Is double buffering (in java) possible with awt? Currently, I'm aware that swing should not be used with awt, so I can't use BufferStrategy and whatnot (I already have some code written in awt that I don't want to rewrite in swing).

If double buffering is possible with awt, do I have to write the buffer by hand? Unlike swing, awt doesn't seem to have the same built-in double buffering capability.

If I do have to write the code by hand, is there a good tutorial to look at? Or is it just easier/advisable for a novice programmer to use swing instead?

Sorry about the multi-step question. Thanks for your time :)

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

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

发布评论

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

评论(1

旧人哭 2024-09-09 03:02:11

这个问题在网上很容易得到解答。只需搜索“double buffer awt”,您就会发现很多示例。你甚至可以看到一个旧的例子我自己在1998年写的在 Java 1.0 AWT 中。您只需要实例化自己的 Graphics 对象并绘制到图像,然后将该图像传输到画布中。这是我的示例中的关键代码:

  public void paint(Graphics g) {
    if (doubleBuffer) {
      paintSky(top.gBuf);
      g.drawImage(top.buf, 0, 0, this);
    } else {
      paintSky(g);
    }
  }

This is easily answered on the web. Just search for "double buffer awt" and you'll find LOTS of examples. You can even see an old example I wrote myself in 1998 in Java 1.0 AWT. You just need to instantiate your own Graphics object and draw to an Image, then blit that image into a canvas. Here's the key bit of code in my example:

  public void paint(Graphics g) {
    if (doubleBuffer) {
      paintSky(top.gBuf);
      g.drawImage(top.buf, 0, 0, this);
    } else {
      paintSky(g);
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文