Java中的双重缓冲 [英] Double Buffering in Java

查看:102
本文介绍了Java中的双重缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上发现了这种双重缓冲代码,但没有任何解释.我对这段代码有些困惑.

I found this code of double buffering on internet but it has no explaination. I am a little confused in this code.

  • 为什么使用图像"i"?如果要使用一次,有什么用?

  • Why is the Image "i" used? What is its use if it is to be used once?

为什么我们已经设置了颜色,为什么要为前景颜色分配变化的颜色?

Why are we assigning changing color to Foreground color,when we already have set color?

g.drawImage()方法在做什么?

What is g.drawImage() method doing?

这是代码:

public void update(Graphics g)
{
    if(i==null)
    {
        i=createImage(getWidth(), getHeight());
        graph=i.getGraphics();
    }

    graph.setColor(getBackground());
    graph.fillRect(0, 0, getWidth(),getHeight());
    graph.setColor(getForeground());

    paint(graph);

    g.drawImage(i,0,0,this);
  }

致谢

推荐答案

Double Buffering的基本思想是在屏幕外创建图像,然后一次全部显示.

The basic idea of Double Buffering is to create the image off screen then display it all at once.

此处找到的Java教程中

From the java tutorials found here

您在那里的代码首先在第一个路径上创建图像,以此作为您的"Back Buffer",我很可能是一个字段,例如

The code you have there first creates an image on first way through to be your "Back Buffer" with this bit, i is likely a field such as

 private Image i;
 private Graphics graph;

 if(i==null)
{
    i=createImage(getWidth(), getHeight());
    graph=i.getGraphics();
}

然后使用此方法将背景色绘制到图像上

Then Paints the background color onto the image with this

graph.setColor(getBackground());
graph.fillRect(0, 0, getWidth(),getHeight());

然后将前面板准备好进行绘制.

Then sets the front ready for drawing.

graph.setColor(getForeground());
paint(graph); /draws

最后将背面的Buffer绘制到主表面上.

Finally drawing the back Buffer over to the primary surface.

g.drawImage(i,0,0,this);

这篇关于Java中的双重缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆