How does double buffering work?

Hi

I have some problem understanding how double buffering and buffer strategy works. I guess there is 2 of these so called buffers (what is a buffer anyway) and instead of drawing directly to the screen, you draw stuff into the buffer, this buffer is then displayed on the screen. is it possible to manipulate anything that is in this buffer before its displayed on the screen? i guess the buffer is like an image with the size of the screen that it is displayed on, in that case can the image buffer be manipulated like a normal image?

some code that i have used…

creates a fullscreen window


      public void setFullScreenMode(DisplayMode displayMode) {
            final JFrame window = new JFrame();
            window.setUndecorated(true);
            window.setIgnoreRepaint(true);
            window.setResizable(false);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            device.setFullScreenWindow(window);
            if(displayMode != null && device.isDisplayChangeSupported()) {
                  try {
                        device.setDisplayMode(displayMode);
                  }
                  catch(IllegalArgumentException ex) {}
                  }
                  window.setSize(displayMode.getWidth(), displayMode.getHeight());
            }
            try {
                  EventQueue.invokeAndWait(new Runnable() {
                        public void run() {
                              window.createBufferStrategy(2);
                        }
                  });
            }
            catch (InterruptedException ex) {}
            catch (InvocationTargetException  ex) {}
      }

get graphics from buffer strategy


      public Graphics2D getGraphicsContext() {
            Window window = device.getFullScreenWindow();
            if(window != null) {
                  BufferStrategy bs = window.getBufferStrategy();
                  return (Graphics2D)bs.getDrawGraphics();
            }
            return null;
      }

part from the main loop that calls draw function


                  Graphics2D g = display.getGraphicsContext();
draw(g);
g.dispose();
display.update();

draw method, just a test


      private void draw(Graphics g) {
            if(drawBg) {
                  g.drawImage(bg, 0, 0, null);
            }
            
            g.drawImage(sprite,(int)locX,(int)locY,null);
      }

so, how do i get a buffured image that i can manipulate from the bufferstrategy? or is that wrong?

Getting an Image/BufferedImage/VolatileImage and manipulating it is most effectively done if you manipulate it using a Graphics2D object that attaches to it so that you can use all of the methods in the Graphics2D class. Your code to “get graphics from buffer strategy” does exactly that.