Ich bin irgendwie neu in Java. Ich möchte ein Spiel machen. Nach vieler Forschung, kann ich nicht verstehen, wie bufferstrategy funktioniert .. weiß, dass ich die Grundlagen .. es schafft ein Off-Screen-Bild, das Sie später in Ihr Fenster setzen können Objekt .. Ich habe diesenVerstehen BufferStrategy
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..
}
}
Ich weiß es nicht .. Ich habe das schon lange studiert .. und mit keinem Glück ... Ich weiß nicht .. vielleicht ist alles da, und es ist wirklich klar und einfach, und ich ist einfach zu dumm, es zu sehen ..
Vielen Dank für all die Hilfe .. :)
Warum müssen wir dispose() bevor wir show() den Puffer haben? Ich gehe davon aus, dass das Grafikobjekt nur Grafikfunktionen hat, aber wenn es nicht benötigt wird, geben wir seine Werkzeuge und Ressourcen frei und zeigen dann unser gepuffertes Bild an. –
@someFolk - Es muss nicht in dieser Reihenfolge getan werden; Der Aufruf von 'bs.show()' könnte in den 'try'-Block verschoben werden. Es gibt jedoch keinen besonderen Grund dafür, und es empfiehlt sich, Systemressourcen freizugeben, sobald sie nicht benötigt werden. –