获取当前显示模式始终为null

时间:2014-02-24 05:33:41

标签: java swing awt fullscreen retina-display

我正在尝试从用户视频卡获取当前显示模式,以便我的游戏可以全屏播放。但是,每次我寻找兼容的显示模式时,我的方法都会返回null,这当然意味着我无法在屏幕上绘制任何内容,因为它没有任何内容可以绘制。所以我的问题是如何修复此方法,以便它返回适当的显示模式?我在Macbook Pro 15“Retina上,这让我相信它可能是我的电脑。我打印了我的屏幕的宽度和高度:

 `Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();`

其中输出1440宽度,900高度。我的一个兼容模式是1440x900,所以我不太清楚为什么它不起作用。这是相关的代码(每次打印坏模式):

//Find the first displaymode that is supported by the video card
public DisplayMode findFirstCompatibleDisplayMode(DisplayMode allDisplayModes[])
{
    DisplayMode goodDisplayModes[] = video_card.getDisplayModes();

    for (int i = 0; i < allDisplayModes.length; i++)
        for (int j = 0; j < goodDisplayModes.length; j++)
            if (displayModesMatch(allDisplayModes[i], goodDisplayModes[j]))
            {
                System.out.println("good mode");
                return goodDisplayModes[j];
            }

    System.out.println("bad mode");
    return null;
}

参数allDisplayModes []被初始化并传递,如下所示:

private static DisplayMode possibleDisplayModes[] = {
    new DisplayMode(1440, 900, 32, 0),
    new DisplayMode(1440, 900, 24, 0), new DisplayMode(1440, 900, 16, 0),
    new DisplayMode(800, 600, 24, 0), new DisplayMode(800, 600, 16, 0),
    new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 24, 0),
    new DisplayMode(640, 480, 16, 0)};
}

displayModesMatch()方法:

//Return trues when the video cards supported display mode is found
public boolean displayModesMatch(DisplayMode firstMode, DisplayMode secondMode)
{
    //Return true if both modes have the same resolution, bit depth, and refresh rate
    return firstMode.getWidth() == secondMode.getWidth() 
        && firstMode.getHeight() == secondMode.getHeight()
        && firstMode.getBitDepth() == secondMode.getBitDepth()
        && firstMode.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI
        && secondMode.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI
        && firstMode.getRefreshRate() == secondMode.getRefreshRate()
        && firstMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN
        && secondMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN;
}

0 个答案:

没有答案