Log in / create account | Login with OpenID
DocForge
Programmer's Wiki

Full Screen in AWT

From DocForge

Small tool written to acquire a full-screen Frame in Java AWT.

/*
 *  FullScreen.java - FullScreen in AWT Automater Class
 *  Copyright (C) 2007  Chris Miller
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see http://www.gnu.org/licenses
 *  or write to the Free Software Foundation,Inc., 51 Franklin Street,
 *  Fifth Floor, Boston, MA 02110-1301  USA
 */
 
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
 
/**
 * A simple class of static methods which are designed to automate the
 * process of creating a full-screen Java environment and changing the
 * DisplayMode to the highest supportable mode automatically.
 */
public class FullScreen {
	/** Let no-one instantiate this class */
	private FullScreen() { }
 
	private static DisplayMode[] SupportedModes;
	private static GraphicsConfiguration gc;
	private static GraphicsDevice gd;
	private static Frame Screen;
 
	private static boolean hasBeenSetUp=false;
 
 
	/**
	 * Returns all the supported DisplayModes.  This method will
	 * force full-screen to be turned on - use only if you already
	 * have a full-screen window!
	 */
	public static DisplayMode[] getDisplayModes() {
		setUpForDisplayFiltering();
		return SupportedModes;
	}
 
	private static void setUpForDisplayFiltering() {
		if(hasBeenSetUp) return;
		Screen=new Frame();
		Screen.setUndecorated(true);
		Screen.setResizable(false);
		gc=Screen.getGraphicsConfiguration();
		gd=gc.getDevice();
		/*     Full screen Mode Checking */
		if(!gd.isFullScreenSupported())
			Screen=null;
		// Get the display modes
		SupportedModes=gd.getDisplayModes();
		hasBeenSetUp=true;
	}
 
	private static activateFullScreenMode() {
		gd.setFullScreenWindow(Screen);
		/*     Screen Resolution Change Checking */
		if(!gd.isDisplayChangeSupported())
			Screen=null;
	}
 
	private static DisplayMode getLargestDisplayMode() {
		setUpForDisplayFiltering();
		int iLargestMode=0;
		for(int i=0; i<SupportedModes.length; i++) {
			if(	(SupportedModes[i].getHeight()
					>= SupportedModes[iLargestMode].getHeight()
					||
				SupportedModes[i].getWidth()
					> SupportedModes[iLargestMode].getWidth())
					&&
				SupportedModes[i].getRefreshRate()
					>= SupportedModes[iLargestMode].getRefreshRate()
				) iLargestMode=i;
		}
		return SupportedModes[iLargestMode];
	}
 
	/**
	 * Returns a full-screen Frame and switches to the largest display
	 * size with the largest refresh rate that the current system can support.
	 */
	public static Frame getLargestDisplaySize() {
		setUpForDisplayFiltering();
		activateFullScreenMode();
		gd.setDisplayMode(getLargestDisplayMode());
		return Screen;
	}
 
	/**
	 * Returns a full-screen Frame and switches to the largest display
	 * size with the greatest refresh rate if and only if the display
	 * dimensions, refresh rate, and bit depth are greater than  or
	 * equal to the supplied constraints, or <b>null</b> if no such
	 * display mode exists on the current system.
	 */
	public static Frame getLargestDisplaySize(
			int width,
			int height,
			int refreshRate,
			int bitDepth) {
		setUpForDisplayFiltering();
		activateFullScreenMode();
		DisplayMode dm=getLargestDisplayMode();
		if(dm.getWidth()<=width){
			gd.setFullScreenWindow(null);
			return null;
		}
		if(dm.getHeight()<=height) {
			gd.setFullScreenWindow(null);
			return null;
		}
		if(dm.getRefreshRate()<=refreshRate) {
			gd.setFullScreenWindow(null);
			return null;
		}
		if(dm.getBitDepth()<=bitDepth) {
			gd.setFullScreenWindow(null);
			return null;
		}
		gd.setDisplayMode(dm);
		return Screen;
	}
 
	/**
	 * Exits full-screen mode.
	 */
	public static void exitFullScreenMode() {
		gc.setFullScreenWindow(null);
		Screen.setUndecorated(false);
		Screen.setResizable(true);
	}
}