Is there an error in this Java book? or am I missing something? (DisplayMode)

怎甘沉沦 提交于 2019-12-02 10:34:50

问题


The following code is written the exact same way as in a Java book I am reading

package main;

 import java.awt.Color;
 import java.awt.DisplayMode;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;

 import javax.swing.JFrame;

 public class FullScreenTest extends JFrame {
public static void main(String[] args) {
    DisplayMode displayMode;
    if (args.length == 3) {
        displayMode = new DisplayMode(Integer.parseInt(args[0]),
                Integer.parseInt(args[1]),        Integer.parseInt(args[2]),
                DisplayMode.REFRESH_RATE_UNKNOWN);

    } else {
        displayMode = new DisplayMode(800, 600, 16,
                DisplayMode.REFRESH_RATE_UNKNOWN);
    }
    FullScreenTest test = new FullScreenTest();
    test.run(displayMode);
}

private static final long DEMO_TIME = 1000;

public void run(DisplayMode displayMode) {
    setBackground(Color.blue);
    setForeground(Color.white);
    setFont(new Font("Dialog", Font.PLAIN, 24));

    SimpleScreenManager screen = new SimpleScreenManager();
    try {
        screen.setFullScreen(displayMode, this);
        try {
            Thread.sleep(DEMO_TIME);
        } catch (Exception e) {
        }

    } finally {
        screen.restoreScreen();
    }
}

public void paint(Graphics g) {

    g.drawString("Hello World!", 20, 50);
}
 }

When I run this, I get a black screen for 5 seconds, and that's it.

However when I change the following line:

displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);

to this:

 displayMode = new DisplayMode(600, 800, 16, DisplayMode.REFRESH_RATE_UNKNOWN);

it works perfectly.

Why is this happening? I don't quite understand.

Thanks,

-Steve


 package main;

 import java.awt.DisplayMode;
 import java.awt.GraphicsDevice;
 import java.awt.GraphicsEnvironment;
 import java.awt.Window;

 import javax.swing.JFrame;

 public class SimpleScreenManager {

private GraphicsDevice device;

public SimpleScreenManager() {
    GraphicsEnvironment environment = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    device = environment.getDefaultScreenDevice();
}

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 (Exception e) {

        }

    }
}

public Window getFullScreenWindow() {
    return device.getFullScreenWindow();
}

public void restoreScreen() {
    Window window = device.getFullScreenWindow();
    if (window != null) {
        window.dispose();
    }
    device.setFullScreenWindow(null);
}
 }

回答1:


DisplayMode has to do with Java "Full Screen Exclusive Mode" functions, which are documented here:

  • http://docs.oracle.com/javase/tutorial/extra/fullscreen/displaymode.html

  • http://docs.oracle.com/javase/7/docs/api/java/awt/DisplayMode.html

I would encourage you to try some of the sample programs here:

  • http://docs.oracle.com/javase/tutorial/extra/fullscreen/example.html

The bottom line is:

1) not all OS's or display devices are necessarily supported by this API

2) All combinations of arbitrary DisplayMode settings aren't necessarily supported if the device itself doesn't support them



来源:https://stackoverflow.com/questions/18028749/is-there-an-error-in-this-java-book-or-am-i-missing-something-displaymode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!