Here's how it works:
JFrame builds a BufferStrategy when you call createBufferStrategy(2); . BufferStrategy knows that it belongs to this particular JFrame instance. You extract it and save it in the bs field.- When the time comes to draw your stuff, you are extracting
Graphics2D from bs . This Graphics2D object is bound to one of the internal buffers owned by bs . When you draw, everything goes to this buffer. - When you finally call
bs.show() , bs will cause the buffer you just drawn to become the current buffer for the JFrame . He knows how to do this because (see Clause 1) he knows that he is in maintenance of the JFrame .
That is all that happens.
As a comment on your code ... you need to slightly modify your drawing procedure. 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());
This will protect against lost buffer frames, which, according to docs , can occur occasionally.
Ted hopp
source share