Double-Buffering in Full-Screen Mode

Page-flipping, if supported, is the fastest way to copy a back buffer to the screen. This example demonstrates how to implement double-buffering using page flipping in full-screen mode.
// Determine if page flipping is supported GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); BufferCapabilities bufCap = gc.getBufferCapabilities(); boolean page = bufCap.isPageFlipping(); if (page) { // Page flipping is supported } else { // Page flipping is not supported } // Create a window for full-screen mode Frame frame = new Frame(gd.getDefaultConfiguration()); Window win = new Window(frame); // Configure the window so that a mouse click will exit full-screen mode win.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { // Exit full-screen mode GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice(); gd.setFullScreenWindow(null); } }); try { // Enter full-screen mode gd.setFullScreenWindow(win); win.requestFocus(); // Create the back buffer int numBuffers = 2; // Includes front buffer win.createBufferStrategy(numBuffers); // Determine the state of a back buffer after it has been displayed on the screen. // This information is used to optimize performance. For example, if your application // needs to initialize a back buffer with a background color, there is // no need to do so if the flip contents is BACKGROUND. BufferStrategy strategy = win.getBufferStrategy(); bufCap = strategy.getCapabilities(); BufferCapabilities.FlipContents flipContents = bufCap.getFlipContents(); if (flipContents.equals(BufferCapabilities.FlipContents.UNDEFINED)) { // The contents is unknown after a flip } else if (flipContents.equals(BufferCapabilities.FlipContents.BACKGROUND)) { // The contents cleared to the component's background color after a flip } else if (flipContents.equals(BufferCapabilities.FlipContents.PRIOR)) { // The contents is the contents of the front buffer just before the flip } else if (flipContents.equals(BufferCapabilities.FlipContents.COPIED)) { // The contents is identical to the contents just pushed to the // front buffer after a flip } // Draw loop while (true) { // Get screen size int screenWidth = win.getWidth(); int screenHeight = win.getHeight(); // Get graphics context for drawing to the window Graphics g = strategy.getDrawGraphics(); if (!flipContents.equals(BufferCapabilities.FlipContents.BACKGROUND)) { // Clear background g.setColor(Color.white); g.fillRect(0, 0, screenWidth, screenHeight); } // Draw shapes and images... // Done drawing g.dispose(); // Flip the back buffer to the screen strategy.show(); } } catch (Throwable e) { // Process exception... } finally { gd.setFullScreenWindow(null); }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.