java 了解缓冲策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13590002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understand BufferStrategy
提问by Charlie Villy
I'm kind of new to java. I want to make a game. After a lot of research, I can't understand how bufferstrategy works.. I know the basics.. it creates an off-screen image that you can later put into your windows object.. I got this
我对java有点陌生。我想做一个游戏。经过大量研究,我无法理解 bufferstrategy 的工作原理.. 我知道基础知识.. 它创建了一个离屏图像,您可以稍后将其放入您的 windows 对象中.. 我明白了
public class Marco extends JFrame {
private static final long serialVersionUID = 1L;
BufferStrategy bs; //create an strategy for multi-buffering.
public Marco() {
basicFunctions(); //just the clasics setSize,setVisible,etc
createBufferStrategy(2);//jframe extends windows so i can call this method
bs = this.getBufferStrategy();//returns the buffer strategy used by this component
}
@Override
public void paint(Graphics g){
g.drawString("My game",20,40);//some string that I don't know why it does not show
//guess is 'couse g2 overwrittes all the frame..
Graphics2D g2=null;//create a child object of Graphics
try{
g2 = (Graphics2D) bs.getDrawGraphics();//this new object g2,will get the
//buffer of this jframe?
drawWhatEver(g2);//whatever I draw in this method will show in jframe,
//but why??
}finally{
g2.dispose();//clean memory,but how? it cleans the buffer after
//being copied to the jframe?? when did I copy to the jframe??
}
bs.show();//I never put anything on bs, so, why do I need to show its content??
//I think it's a reference to g2, but when did I do this reference??
}
private void drawWhatEver(Graphics2D g2){
g2.fillRect(20, 50, 20, 20);//now.. this I can show..
}
}
I don't know.. I've been studying this for a long time now.. and with no luck at all.. I don't know.. maybe it's all there, and it's really clear and simple, and I'm just too stupid to see it..
我不知道.. 我已经研究了很长时间了.. 一点运气都没有.. 我不知道.. 也许一切都在那里,而且非常清晰和简单,而且我'我只是太笨了看不到它..
Thanks for all the help.. :)
感谢所有的帮助.. :)
回答by Ted Hopp
Here's how it works:
这是它的工作原理:
- The
JFrame
constructs aBufferStrategy
when you callcreateBufferStrategy(2);
. TheBufferStrategy
knows that it belongs to that specific instance ofJFrame
. You are retrieving it and storing it in thebs
field. - When it comes time to draw your stuff, you are retrieving a
Graphics2D
frombs
. ThisGraphics2D
object is tied to one of the internal buffers owned bybs
. As you draw, everything goes into that buffer. - When you finally call
bs.show()
,bs
will cause the buffer that you just drew to become the current buffer for theJFrame
. It knows how to do this because (see point 1) it knows whatJFrame
it is in service to.
- 该
JFrame
构造了一个BufferStrategy
当你调用createBufferStrategy(2);
。TheBufferStrategy
知道它属于JFrame
. 您正在检索它并将其存储在bs
现场。 - 当需要绘制你的东西时,你正在
Graphics2D
从bs
. 此Graphics2D
对象绑定到 拥有的内部缓冲区之一bs
。当你画画时,一切都会进入那个缓冲区。 - 当您最终调用 时
bs.show()
,bs
将导致您刚刚绘制的缓冲区成为 的当前缓冲区JFrame
。它知道如何做到这一点,因为(参见第 1 点)它知道它的用途JFrame
。
That's all that's going on.
这就是正在发生的一切。
By way of comment to your code...you should change your drawing routine a bit. Instead of this:
通过对您的代码进行评论的方式...您应该稍微更改您的绘图程序。取而代之的是:
try{
g2 = (Graphics2D) bs.getDrawGraphics();
drawWhatEver(g2);
} finally {
g2.dispose();
}
bs.show();
you should have a loop like this:
你应该有一个这样的循环:
do {
try{
g2 = (Graphics2D) bs.getDrawGraphics();
drawWhatEver(g2);
} finally {
g2.dispose();
}
bs.show();
} while (bs.contentsLost());
That will safeguard against lost buffer frames, which, according to the docs, can happen occasionally.
这将防止丢失缓冲区帧,根据文档,这种情况偶尔会发生。