How do I take input from full screen mode?

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.

Well, first thing is to update your graphcis drivers if you havent donme so lately as this smells like a driver problem.

Why does your class extend JApplet when your game isn’t an applet? That should be unnecessary.

You have to add a line like

frame.addKeyListener(game);

in there somewhere if you want the keyPressed method to ever get called. KeyListeners have to be registered with the Components they’re listening to via the addKeyListener method.

You could try having code like the following at the end of main:

Thread.sleep(5000); //sleep for 5 seconds
System.exit();

That will cause the program to wait for 5 seconds and then exit. This way, the program will be terminate even without the KeyListener.

What operating system do you have? You should get a stable operating system such as Windows XP Professional, Windows 2000, or Linux. Then Alt-Ctrl-Del (or whatever the Linux alternative is) should work.

By the way, since you had to shut off your computer manually while the operating system was still running, you should run ScanDisk (sometimes called CheckDisk) to make sure your hard drive doesn’t have any bad sectors.

Yeah, I know I kinda need a new machine. I was thinking of buying a laptop and souping it up. Right now I’m running (okay, don’t cringe) Windows ME, on an HP Pavilion computer with 383 MB of RAM. The RAM isn’t bad but the OS is. ::slight_smile:

Oh, and also I extended JApplet because it’s what I’m used to doing. I really don’t know much else and JApplet makes it so easy to initialize everything.