The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > The Screen  [8 examples]

e602. 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);
    }

 Related Examples
e598. Getting the Screen Size
e599. Centering a Frame, Window, or Dialog on the Screen
e600. Getting the Number of Screens
e601. Enabling Full-Screen Mode
e603. Getting the Available Screen Sizes, Refresh Rates, and Number of Colors
e604. Getting the Current Screen Refresh Rate and Number of Colors
e605. Setting the Screen Size, Refresh Rate, or Number of Colors

See also: Colors    Components    Containers    Cursors    Drawing    Events    Focus    Frames    GridBagLayout    Images    Shapes    Simulating Events    Text   


© 2002 Addison-Wesley.