gpt4 book ai didi

java - Java 中的双缓冲

转载 作者:行者123 更新时间:2023-12-02 11:01:52 35 4
gpt4 key购买 nike

我在网上找到了这段双缓冲的代码,但没有解释。我对这段代码有点困惑。

  • 为什么使用图像“i”?如果只用一次有什么用?

  • 当我们已经设置了颜色时,为什么还要将变化的颜色分配给前景色?

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

这是代码:

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

从java教程中找到here

您那里的代码首先在第一种方式中创建一个图像,作为您的“后台缓冲区”,i 可能是一个字段,例如

 private Image i;
private Graphics graph;

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

然后用这个将背景颜色绘制到图像上

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

然后设置正面准备绘制。

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

最后将后缓冲区绘制到主表面上。

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

关于java - Java 中的双缓冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13533344/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com