I’ve been working on a game and it’s the first time I’ve worked with anything full screen before. Up until now I never cared if I had to stick my game into a JFrame but this is for the GameDev Four Elements Contest and I figured full screen would make a better impression. (I’ve never entered any contest at all before.)
So I started to set up the Game class somewhat like this:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Game extends JApplet implements KeyListener{
public static boolean forceWindow=false;
ImageIcon mainmenu=new ImageIcon("Title Screen.jpg");
Image buff; Graphics backg;
static GraphicsEnvironment gfxenv;
static GraphicsDevice screen;
public static final DisplayMode[] wishList={};
public static DisplayMode originalMode;
public void keyPressed(KeyEvent e){
screen.setDisplayMode(originalMode);
screen.setFullScreenWindow(null);
System.exit(0);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void init(){
buff=createImage(this.getSize().width,this.getSize().height);
backg=buff.getGraphics();
setFocusable(true);
}
public void paint(Graphics g){
mainmenu.paintIcon(this,g,0,0);
}
public static void main(String[] args){
gfxenv=GraphicsEnvironment.getLocalGraphicsEnvironment();
screen=gfxenv.getDefaultScreenDevice();
JFrame frame=new JFrame("My Supercool Game");
frame.setUndecorated(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
originalMode=screen.getDisplayMode(); //****** this line gives me problems as well
if(args.length>0&&args[0].equals("window")) forceWindow=true;
if(screen.isFullScreenSupported()&&screen.isDisplayChangeSupported()&&!forceWindow){
screen.setFullScreenWindow(frame);
}
else{
frame.setUndecorated(false);
frame.setSize(1024,768);
Insets i=frame.getInsets();
frame.setSize(i.left+1024+i.right,i.top+768+i.bottom);
}
Game g=new Game();
frame.getContentPane().add(g);
frame.show();
g.init(); g.start();
}
}
So I ran something similar to this and it got me to full screen, but then it was stuck. I tried Alt+Esc, I tried Ctrl+Alt+Del, I tried pressing the Escape key. Nothing at all worked. I had to hold the power button on my computer to get it to shut off, thereby ending the program. It won’t even respond when I force windowed mode. (However, then it’s a JFrame which gives me that nice X in the corner.)
Also, you may have noticed the line I marked that gave me problems. This line compiles fine, but gives me the exception below and then ends when I try running it.
Exception in thread "main" java.lang.InternalError: Could not get display mode
at sun.awt.Win32GraphicsDevice.getCurrentDisplayMode(Native Method)
at sun.awt.Win32GraphicsDevice.getDisplayMode(Unknown Source)
at Game.main(Game.java:35)
I’m not sure how to work around this, but I’ve heard that internal errors are “very rare, and there’s usually nothing you can do about them when they pop up.” What I’m trying to do is save the user’s current display mode so that it can be restored later. This is what’s messing the whole thing up.
