问题描述
我是java的新手。我想做一个游戏。经过大量的研究,我无法理解bufferstrategy的工作原理..
我知道基础知识..它创建了一个离屏图像,以后可以放入你的windows对象..我得到了这个
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
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.. :)
推荐答案
以下是它的工作原理:
-
JFrame
构造一个BufferStrategy
当你打电话给createBufferStrategy(2);
。BufferStrategy
知道它属于JFrame
的特定实例。您正在检索它并将其存储在bs
字段中。 - 当需要绘制您的东西时,您正在检索
Graphics2D
来自bs
。此Graphics2D
对象与bs
所拥有的内部缓冲区之一相关联。当您绘制时,一切都进入缓冲区。 - 当您最终调用
bs.show()
时,bs
将导致您刚刚绘制的缓冲区成为JFrame
的当前缓冲区。它知道如何做到这一点,因为(见第1点)它知道它正在服务的JFrame
。
- 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.
这就是所有这些。
通过评论你的代码...你应该稍微改变你的绘图程序。而不是:
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.
这篇关于了解BufferStrategy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!