Subscribe to Dogstopper's Code Mania        RSS Feed

Full Screen Java Applications

Icon 2 Comments
Have you ever wanted to create full-screen games in Java? Well, you're about to find out. Just a note that the code that I will show you today does NOT work in Applets. If you want to make a full-screen application to be accessible on the web, make it a Java Web Start.

Today, we will encounter 3 classes you probably have not used before:

Let's start with the class outline...
public class FullscreenFrame extends JFrame {

	// Set the resolution and make accessible to entire game	
	public static final int FRAMEWIDTH = 800;
	public static final int FRAMEHEIGHT = 600;
        // Set up the Graphics objects
	private GraphicsDevice graphicsDevice;
	private DisplayMode old;
	public FullscreenFrame(GraphicsDevice gd) {
	public static void main(String[] args) {
		// ... Send the screen GraphicsDevice to the constructor

	public void cleanAndQuit() {
		// Called when the game is done and original mode restored.

The first thing we must do is to get the local GraphicsEnvironment object to manipulate the screen directly. Next, using that object, you can grab the first screen device.

	public static void main(String[] args) {
                // Get the graphics environment
		GraphicsEnvironment ge = 
                // From that, grab the screen(s)
	        GraphicsDevice[] devices = 
            // Now just call the JFrame with the first screen.
	    new FullscreenFrame(devices[0]);

Now we get into the fullscreen part, so you will want to do a couple of things:
1) Save the user's graphics settings from before the fullscreen.
2) Check if fullscreen is supported. If it is, then go to step 3, otherwise, make a normal, non-fullscreen JFrame.
3) Set the user's screen to fullscreen and set the GraphicsMode as a certain resolution, with the original mode's bit-depth and refresh rate (16 bit stays at 16, 32 at 32...).

Ready to see it in code? You bet!
	public FullscreenFrame(GraphicsDevice gd) {
                // set gd as object object graphicsDevice so it can be used in other methods,
		graphicsDevice = gd;

                // The old mode (saves original resolution, bit depth, and refresh rate)
		old = gd.getDisplayMode();
                // Add panels or other components you want/need here...

                // If the computer supports fullscreen,
		if (graphicsDevice.isFullScreenSupported()) {
                    // Then take away the borders and make it non-resizable
                    // This is the new display mode...As you can see, it 
                    // takes our global resolution and makes that the new screen 
                    // resolution. We keep the same bit depth and refresh rate
                    // So there are no problems arising from that.
		    DisplayMode dm = new DisplayMode(FRAMEWIDTH, FRAMEHEIGHT, old.getBitDepth(), old.getRefreshRate());
                    // Set this window to fullscreen

                    // Give our new graphics mode to our screen.
                // If can't go fullscreen...
                else { 
                        // Print a friendly message...
			System.out.println("Your graphics card sucks!");

                        // In reality, you would want to set up a 
                        // non-fullscreen JFrame to use instead.

                // The normal methods...

And now, one of the most critical parts is the clean-up method...You have to restore the original graphics mode back to the OS, otherwise it will remain 800x600 when the application closes...
	public void cleanAndQuit() {
                // This call actually makes it non-fullscreen and disassociates
                // GraphicsModes that were attatched to it, so now it is safe to exit.

                // Exit!

There you go! Now you know how to create fullscreen apps. Enjoy!

2 Comments On This Entry

Page 1 of 1


13 March 2010 - 02:49 PM
Never tried to do it that way. I think that my way is faster, but not as clean maybe

	public static void main(String[] args) {
		JFrame frame = new JFrame();

then you just add stuff to the frame line normal, and exiting it is done within program. I tend to do it be using right click as default close operation when I use this method.

Just thought I'd share an alternative way of doing it.


13 March 2010 - 06:19 PM
That is the hacky way of doing that and actually is not truly fullscreen. My way actually changes the resolution and such. Also, your way has the pesky "Start" menu bar at the bottom. My way doesn't.
Page 1 of 1

January 2021

101112131415 16

Recent Entries

Search My Blog

Recent Comments

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)