public void setFullscreen(boolean fullscreen) throws Exception{
		DisplayMode targetDisplayMode = null;
		if (fullscreen) {
			tempSize = getDisplaySize();
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		    DisplayMode[] modes = Display.getAvailableDisplayModes();
		    int freq = 0;
	 
		    for (int i=0;i<modes.length;i++) {
		        DisplayMode current = modes[i];
	
			if ((current.getWidth() == screenSize.width) && (current.getHeight() == screenSize.height)) {
			    if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
			        if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
				    targetDisplayMode = current;
				    freq = targetDisplayMode.getFrequency();
	                        }
	                    }
			    if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
	                        (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
	                            targetDisplayMode = current;
	                            break;
	                    }
	                }
	            }
	        } else {
	            targetDisplayMode = new DisplayMode(tempSize.width,tempSize.height);
	        }
	 
	        if (targetDisplayMode == null) {
	            throw new Exception("No valid display mode for this computer.");
	        }
	        Display.setDisplayMode(targetDisplayMode);
	        Display.setFullscreen(fullscreen);
	}