Java中的FullScreenMode

时间:2011-11-21 22:56:05

标签: java

我一直在关注David Brackeen撰写的“用Java开发游戏”一书,在第2章中他教授如何使用全屏模式。我遇到的问题是,当我去运行他的源代码时,我得到的只是一个白色屏幕。我花了好几个小时,我相信它与Windows 7有关。

谢谢你的时间!

import java.awt.*;

import javax.swing.JFrame;

/**
The SimpleScreenManager class manages initializing and
displaying full screen graphics modes.
*/
public class SimpleScreenManager {

private GraphicsDevice device;

/**
    Creates a new SimpleScreenManager object.
*/
public SimpleScreenManager() {
    GraphicsEnvironment environment =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    device = environment.getDefaultScreenDevice();

}


/**
    Enters full screen mode and changes the display mode.
*/
public void setFullScreen(DisplayMode displayMode,
    JFrame window)
{
    window.setUndecorated(true);
    window.setResizable(false);

    device.setFullScreenWindow(window);

    if (displayMode != null &&
        device.isDisplayChangeSupported())
    {
        try {
            device.setDisplayMode(displayMode);
        }
        catch (IllegalArgumentException ex) {
            // ignore - illegal mode for this device
        }
    }
}


/**
    Returns the window currently used in full screen mode.
*/
public Window getFullScreenWindow() {
    return device.getFullScreenWindow();
}


/**
    Restores the screen's display mode.
*/
public void restoreScreen() {
    Window window = device.getFullScreenWindow();
    if (window != null) {
        window.dispose();
    }
    device.setFullScreenWindow(null);
}

public static void main(String args[])
{
    SimpleScreenManager t = new SimpleScreenManager();
    try {

        t.setFullScreen(new DisplayMode(1920, 1080, 64, 0), new JFrame());
        Thread.sleep(2000);
    }
     catch (InterruptedException ex) { }
    finally {
        t.restoreScreen();
    }
}

}

1 个答案:

答案 0 :(得分:0)

切换!肘节...

private void toggleDecoration(boolean decorated) {
    dispose();
    if (decorated) {
        //save last bounds and its extended state
        lastState = getExtendedState();
        lastBounds = getBounds();
        try{
            setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        }
        catch(Exception ev){
            setBounds(getGraphicsConfiguration().getDevice().getDefaultConfiguration().getBounds());
            ev.printStackTrace();
        }
    }
    else {
        //restore last bounds and its extended state
        setBounds(lastBounds);
        setExtendedState(lastState);
    }
    setUndecorated(decorated);
    setVisible(true);
}