实施双缓冲?

发布于 2024-12-09 00:58:07 字数 550 浏览 0 评论 0原文

我正在制作一款游戏,我正在尝试找到在其中实现双缓冲的最佳方法。有人能告诉我如何使用下面的渲染代码来做到这一点吗?

public void paint(Graphics g){
    super.paint(g);
    
    
    Graphics2D g2d = (Graphics2D) g;

    
    g2d.drawImage(p.getImage(), p.getX(),p.getY(),null);

    //draw each ball object
        for(int i=0;i<balls.size(); i++){
             Ball tmp = (Ball) balls.get(i);
             g2d.drawImage(tmp.getImage(), tmp.getX(),tmp.getY(),null);
            }   
         //strings
         g2d.drawString("Score: "+score,50,20);
        }

有人可以帮忙吗?

I am making a game and I am trying to find the best way to implement Doublebuffering into it. Would anyone be able to show me how I could do it with my rendering code below?

public void paint(Graphics g){
    super.paint(g);
    
    
    Graphics2D g2d = (Graphics2D) g;

    
    g2d.drawImage(p.getImage(), p.getX(),p.getY(),null);

    //draw each ball object
        for(int i=0;i<balls.size(); i++){
             Ball tmp = (Ball) balls.get(i);
             g2d.drawImage(tmp.getImage(), tmp.getX(),tmp.getY(),null);
            }   
         //strings
         g2d.drawString("Score: "+score,50,20);
        }

Could someone please help?

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

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

发布评论

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

评论(1

瑕疵 2024-12-16 00:58:07

如果您使用 Swing,则可以使用内置双缓冲: http://java.sun.com/products/jfc/tsc/articles/painting/#db

如果您正在实现自己的渲染,这里有一些提示:

双缓冲基本上意味着您有两次绘制缓冲器您可以在显示另一个的同时交替写入。

在您的情况下,您可以使用图像来绘制游戏内容,然后将该图像绘制到组件。从某种意义上说,这应该给你双重缓冲。您可以使用交换的两个图像以减少并发访问,即一个是显示的“前”缓冲区,另一个是您绘制的“后”缓冲区。

也就是说,我强烈建议不要自己实现。相反,您应该尝试使用现有的 2D 库(例如 Swing)或 3D 库(例如带有 JOGL 的 OpenGL 或作为 Java 绑定的 LWJGL - 请注意,它不必是带有 OpenGL 的 3D)。或者,您也可以寻找游戏引擎,那里有很多游戏引擎。

If you're using Swing you just can use the built-in double buffering: http://java.sun.com/products/jfc/tsc/articles/painting/#db

In case you're implementing your own rendering, here are some hints:

Double Buffering basically means you have two draw buffers that you alternatingly write to while the other is displayed.

In your case, you might use an image to draw your game content to and then draw that image to the component. This should give you double buffering in a sense. You might use two images which you swap in order to reduce concurrent access, i.e. one would be the "front" buffer that is displayed and the other is the "back" buffer you draw to.

That said, I'd strongly recommend not to implement that yourself. Instead you should try and use one of the existing 2D libraries (like Swing) or 3D libraries (like OpenGL with JOGL or LWJGL as Java bindings - note that it doesn't have to be 3D with OpenGL). Alternatively you could also look for a game engine, there are plenty of them out there.

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