我正在研究受XNA启发的游戏框架。几乎每天我都会发现这个或那个新问题。今天,它将从窗口模式切换到全屏模式并返回。它的要点是:在全屏和窗口模式之间切换失败
如果我以窗口模式启动游戏,它工作正常。当我全屏工作的时候也很好。从全屏切换回来之后出现错误,屏幕上没有画任何东西。再次全屏再次正常工作。我只是在窗口模式下才能解决这个问题,除非游戏开始。
我设置窗口是这样的:
public void setW(){
jFrame.setVisible(false);
if(graphicDevice.getFullScreenWindow() != null && jFrame.isDisplayable())graphicDevice.getFullScreenWindow().dispose();
graphicDevice.setFullScreenWindow(null);
jFrame.setResizable(false);
jFrame.setUndecorated(false);
jFrame.getContentPane().setPreferredSize(new Dimension(width, height));
jFrame.setVisible(true);
jFrame.pack();
graphics = (Graphics2D)jFrame.getContentPane().getGraphics();
fullScreen = false;
}
全屏设置这样
public void setFS(){
jFrame.setVisible(false);
if(jFrame.isDisplayable())jFrame.dispose();
jFrame.setResizable(false);
jFrame.setUndecorated(true);
graphicDevice.setFullScreenWindow(jFrame);
graphicDevice.setDisplayMode(new DisplayMode(width, height, 32, 60));
graphics = (Graphics2D)jFrame.getContentPane().getGraphics(); // graphicDevice.getFullScreenWindow().getGraphics(); does the same thing
fullScreen = true;
}
然后我用这个方法来绘制... deviceManager.getGraphics()画。 ..(实际上我使用中介bufferImage)我使用游戏循环,所以这种情况不断发生。
public Graphics2D getGraphics(){
return graphics;
}
现在,如果我用这个方法:
public Graphics2D getGraphics(){
if(fullScreen)
return (Graphics2D)graphics;
else
return (Graphics2D)jFrame.getContentPane().getGraphics();
}
我敢肯定,我做错了什么。这个窗口模式的作品,我知道。为什么我从全屏返回时会变成梨形。窗户保持灰色,没有任何东西被涂在上面。
但是如果我创建这样的方法:
public void assignGraphics(){
graphics = (Graphics2D)jFrame.getContentPane().getGraphics()
}
,过一会儿给它(一个游戏周期通过)它解决了这个问题。这就是第二种模式切换方法的原因,因为它在每个循环中从JFrame获取图形。
开始以来这个问题,对这个问题做得很一点,我这里的东西是它的真正症结所在:为什么我不能让显卡在同一周期产生一个窗口,我离开全屏?