Fullscreen mode in Java

hi

I tried to use the GraphicsDevice.isFullscreenSupported() / setFullscreenWindow() methods to make use of fullscreen exclusive mode with a java.awt.Frame. But unfortunately the isFullscreenSupported() returns false on my system even if I know that hardware accelerated fullscreen support is available on my system. I’m using Kubuntu Linux 6, Kernel 2.6.15-27-386 and Java 1.5.7.

Does anybody know, why it returns false while it should return true?

Any help is appreciated. Thanks in advance.

Marvin

Java 1.5 and below do not support full screen exclusive mode on linux.

Thanks for the quick reply :).

So Java 1.6 does?

Marvin

I installed Java-6 and now the method returns true. Yeehaa!

Unfortunately The frame is not displayed in exclusive mode, but the window is just resized to cover the whole screen. Is there anything that must be done except calling gd.setFullscreenWindow( myFrame ) ?

Marvin

I think, I found it. You have to set the DisplayMode, do you? I’m investigating now.

OK. Setting the DisplayMode did the trick ;D.

But one last Question. All DisplayModes have BIT_DEPTH_MULTI as the BPP value. How can I use a specific BPP? I tried to create a new Instance of DisplayMode with my chosen BPP, but the setDisplayMode() method doesn’t accept it. It says:


java.lang.IllegalArgumentException: Invalid display mode
	at sun.awt.X11GraphicsDevice.setDisplayMode(X11GraphicsDevice.java:359)
...

What can I do?

Marvin

I think that, on any given system, you’re stuck with whatever display modes are available.

Did you use GraphicsDevice.getDisplayModes() ?

I don’t know how Linux works, but on Windows this will return each and every display mode separately. ie 800x600x16, 800x600x32, etc etc

A while back I learned from one of the members of the Java 2D team that the reason for this is that X11 doesn’t work the same way as Windows – each individual X window has a “visual” which determines its bit depth and other properties. There is no overall bit depth specified for the entire screen as in Windows.

Thanks for the infos.

hi there, I’m using linux too…
i have exactly the same problem here… unable get real full screen modes …
only the frame gets resized to the size of my actual desktop resolution.

so my desktop is 1280x1024x32
and i would like my game to be 800x600x32
it works fine in window mode… but i would like to go fullscreen…
i could "Runtime.getRuntime().exec(“xrandr --size 2”);
but it’s not the 'nice way :wink:

could you tell me how you did it please?
how can i change resolution from with my game?

tnx :slight_smile:

I got it right.
I think the problem was the color depth parameter, it cannot be set to 16 or 32, but have to be set with “-1” for Linux …weird…
So, since i’m speaking about linux related problem, there’s also the annoying X11 key repeat problem,
that drove me mad, i was wondering why my animation routines were all shaky…
i looked at the code for hours wondering WhY O whY iT DoeSn’t siMpLY WORK O_O ?!
… well the answer was: 'cause x11key repeat is messing up, there’s nothing wrong with the code itself :frowning:
so you can see i take care to test if OS is Linux to disable X11 key repeat… (Runtime.getRuntime().exec(“xset r off”):wink:

here’s a working sample, as of today using ubuntu feisty and java6:
(you can find the rest of the sourcesHere)


import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import java.awt.Color;
import java.io.IOException;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;

public class Game extends Canvas  {

	public MapLoader maper;
	public static Keyb board;
	public static boolean up,down,left,right,space;
	private static int gameWidth=800;
	private static int gameHeight=600;
	//private static DisplayMode BDM = new DisplayMode(800, 600, 16, 0);
	private BufferStrategy bufferStrategy;

	public Game()  {
		if (System.getProperty("os.name").equals("Linux")) {
			try{
				// suppress the X11 key repeat problem
				Runtime.getRuntime().exec("xset r off");
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
		}
		System.out.printf("System is [%s]\n",System.getProperty("os.name"));
		JFrame mainFrame;
		mainFrame = new JFrame("bleh");		
		board = new Keyb();
		DisplayMode displayMode = new DisplayMode(gameWidth, gameHeight, -1,
				DisplayMode.REFRESH_RATE_UNKNOWN);		
		GraphicsDevice device = GraphicsEnvironment.
		getLocalGraphicsEnvironment().getDefaultScreenDevice();
		device.getDefaultConfiguration();
		mainFrame.setResizable(false);
		mainFrame.setUndecorated(true);
		mainFrame.setVisible(true);
		mainFrame.createBufferStrategy(2);
		bufferStrategy = mainFrame.getBufferStrategy();
		device.setFullScreenWindow(mainFrame);			
		if(device.isDisplayChangeSupported()) {
			try {device.setDisplayMode(displayMode);}
			catch(IllegalArgumentException exception) {}			
			mainFrame.setSize(gameWidth, gameHeight);
		} else {shutdown();} 
		validate();
		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				shutdown();
			}
		});
		mainFrame.addKeyListener(board);
		requestFocus();		
		maper= new MapLoader(this);		
		init();			
	}	

	public void init(){

		maper.loadResources();
		maper.setBackground(maper.loadImage("coalescence1280.jpg"));
		maper.loader(1);
		board.up=false;
		board.down=false;
		board.left=false;
		board.right=false;
		dashboard();
	}

	public void gameLoop() {
		long startTime = System.nanoTime();
		boolean gameRunning = true;
		while (gameRunning) {
			long elapsedTime = (System.nanoTime() - startTime) / 1000000; 
			startTime = System.nanoTime();
			Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
			try { Thread.sleep(10); } catch (Exception e) {}	

			for (int i=0;i<elapsedTime / 5;i++) {				
				logic();					
				maper.update((long)i);
				maper.draw(g,MapLoader.newx2Map,gameWidth,536,elapsedTime);
				//dashboard();
				g.dispose();
				bufferStrategy.show();	
			}			
			/*
			if ((elapsedTime % 5) != 0) {				
				logic();					
				maper.update(elapsedTime % 5);
				maper.draw(g,MapLoader.newx2Map,wwidth,wheight,elapsedTime);
				//dashboard();
				g.dispose();
				strategy.show();	
			}	
			 */
		}       
	}

	public void dashboard(){

		Player zontrox = maper.zontrox;
		Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
		g.setColor(Color.black);
		g.fillRect(0,536,800,600);
		g.setColor(Color.white);
		g.drawString("Health",230,563);
		g.drawString("Laser",230,575);
		g.drawString("Booster",230,587);
		g.setPaint(Color.red);
		g.fillRect(290,556,Player.MAX_SHIELDS,5);
		g.setPaint(Color.blue);
		g.fillRect(290+Player.MAX_SHIELDS-zontrox.getShields(),556,zontrox.getShields(),5);
		g.drawImage(maper.loadImage("dashboard.png"),0,536,null);
		//g.dispose();
		//strategy.show();		
	}

	public void logic(){
		playerMover();
		creatureMover();	
	}

	public void playerMover(){
		//Creature player = (Creature)MapLoader.newx2Map.getPlayer();		
		Player zontrox = maper.zontrox;
		float velocityX = 0;
		float velocityY = 0; 
		
		if (board.left) {	
			velocityX-=zontrox.getMaxSpeed();
		}
		if (board.right){
			velocityX+=zontrox.getMaxSpeed();
		}	
		if (board.up){			
			velocityY-=zontrox.getMaxSpeed()*2; 
		}		
		if (board.down){
			velocityY+=zontrox.getMaxSpeed();
		}
		if (board.space){
			maper.lazer();
		}
		if (board.exit){
			shutdown();
		}
		zontrox.setVelocityX(velocityX);
		zontrox.setVelocityY(velocityY);
	}


	public void creatureMover(){		
		Creature gule = (Creature)maper.guleSprite;
		if (gule!=null){		
			if (maper.bump){
				//make creature to go the opposite direction on *BUMP*
				gule.setVelocityX(gule.getVelocityX()*-1);
			}
		}
	}	

	public static void shutdown(){
		if (System.getProperty("os.name").equals("Linux")) {
			try{Runtime.getRuntime().exec("xset r on");}
			catch(IOException ex){ex.printStackTrace();}
			System.exit(0);
		}
	}

	public static void main(String argv[]) {
		Game g =new Game();
		g.gameLoop();	
	}
}


hum, it seems i get random error output.
it doesn’t comes out all the time, and the game doesn’t crash or anything,
but time to time i get this output:

someone can help ?


System is [Linux]
2007/06/22 19:55:12 sun.awt.X11.XToolkit processException
警告: Exception on Toolkit thread
java.lang.NullPointerException
	at java.awt.Component$BltBufferStrategy.revalidate(Component.java:3979)
	at java.awt.Component$BltSubRegionBufferStrategy.validateAndShow(Component.java:4071)
	at javax.swing.BufferStrategyPaintManager.show(BufferStrategyPaintManager.java:249)
	at javax.swing.RepaintManager.show(RepaintManager.java:1212)
	at javax.swing.SwingPaintEventDispatcher.createPaintEvent(SwingPaintEventDispatcher.java:43)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:502)
	at sun.awt.X11.XContentWindow.handleExposeEvent(XContentWindow.java:122)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:497)
	at sun.awt.X11.XBaseWindow.dispatchEvent(XBaseWindow.java:1092)
	at sun.awt.X11.XBaseWindow.dispatchToWindow(XBaseWindow.java:1069)
	at sun.awt.X11.XToolkit.dispatchEvent(XToolkit.java:468)
	at sun.awt.X11.XToolkit.run(XToolkit.java:626)
	at sun.awt.X11.XToolkit.run(XToolkit.java:560)
	at java.lang.Thread.run(Thread.java:619)
2007/06/22 19:55:12 sun.awt.X11.XToolkit processException
警告: Exception on Toolkit thread
java.lang.NullPointerException
	at java.awt.Component$BltBufferStrategy.revalidate(Component.java:3979)
	at java.awt.Component$BltSubRegionBufferStrategy.validateAndShow(Component.java:4071)
	at javax.swing.BufferStrategyPaintManager.show(BufferStrategyPaintManager.java:249)
	at javax.swing.RepaintManager.show(RepaintManager.java:1212)
	at javax.swing.SwingPaintEventDispatcher.createPaintEvent(SwingPaintEventDispatcher.java:43)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:502)
	at sun.awt.X11.XContentWindow.handleExposeEvent(XContentWindow.java:122)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:497)
	at sun.awt.X11.XBaseWindow.dispatchEvent(XBaseWindow.java:1092)
	at sun.awt.X11.XBaseWindow.dispatchToWindow(XBaseWindow.java:1069)
	at sun.awt.X11.XToolkit.dispatchEvent(XToolkit.java:468)
	at sun.awt.X11.XToolkit.run(XToolkit.java:626)
	at sun.awt.X11.XToolkit.run(XToolkit.java:560)
	at java.lang.Thread.run(Thread.java:619)
2007/06/22 19:55:12 sun.awt.X11.XToolkit processException
警告: Exception on Toolkit thread
java.lang.NullPointerException
	at java.awt.Component$BltBufferStrategy.revalidate(Component.java:3979)
	at java.awt.Component$BltSubRegionBufferStrategy.validateAndShow(Component.java:4071)
	at javax.swing.BufferStrategyPaintManager.show(BufferStrategyPaintManager.java:249)
	at javax.swing.RepaintManager.show(RepaintManager.java:1212)
	at javax.swing.SwingPaintEventDispatcher.createPaintEvent(SwingPaintEventDispatcher.java:43)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:502)
	at sun.awt.X11.XContentWindow.handleExposeEvent(XContentWindow.java:122)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:497)
	at sun.awt.X11.XBaseWindow.dispatchEvent(XBaseWindow.java:1092)
	at sun.awt.X11.XBaseWindow.dispatchToWindow(XBaseWindow.java:1069)
	at sun.awt.X11.XToolkit.dispatchEvent(XToolkit.java:468)
	at sun.awt.X11.XToolkit.run(XToolkit.java:626)
	at sun.awt.X11.XToolkit.run(XToolkit.java:560)
	at java.lang.Thread.run(Thread.java:619)
2007/06/22 19:55:12 sun.awt.X11.XToolkit processException
警告: Exception on Toolkit thread
java.lang.NullPointerException
	at java.awt.Component$BltBufferStrategy.revalidate(Component.java:3979)
	at java.awt.Component$BltSubRegionBufferStrategy.validateAndShow(Component.java:4071)
	at javax.swing.BufferStrategyPaintManager.show(BufferStrategyPaintManager.java:249)
	at javax.swing.RepaintManager.show(RepaintManager.java:1212)
	at javax.swing.SwingPaintEventDispatcher.createPaintEvent(SwingPaintEventDispatcher.java:43)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:502)
	at sun.awt.X11.XContentWindow.handleExposeEvent(XContentWindow.java:122)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:497)
	at sun.awt.X11.XBaseWindow.dispatchEvent(XBaseWindow.java:1092)
	at sun.awt.X11.XBaseWindow.dispatchToWindow(XBaseWindow.java:1069)
	at sun.awt.X11.XToolkit.dispatchEvent(XToolkit.java:468)
	at sun.awt.X11.XToolkit.run(XToolkit.java:626)
	at sun.awt.X11.XToolkit.run(XToolkit.java:560)
	at java.lang.Thread.run(Thread.java:619)
2007/06/22 19:55:12 sun.awt.X11.XToolkit processException
警告: Exception on Toolkit thread
java.lang.NullPointerException
	at java.awt.Component$BltBufferStrategy.revalidate(Component.java:3979)
	at java.awt.Component$BltSubRegionBufferStrategy.validateAndShow(Component.java:4071)
	at javax.swing.BufferStrategyPaintManager.show(BufferStrategyPaintManager.java:249)
	at javax.swing.RepaintManager.show(RepaintManager.java:1212)
	at javax.swing.SwingPaintEventDispatcher.createPaintEvent(SwingPaintEventDispatcher.java:43)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:502)
	at sun.awt.X11.XContentWindow.handleExposeEvent(XContentWindow.java:122)
	at sun.awt.X11.XWindow.handleExposeEvent(XWindow.java:497)
	at sun.awt.X11.XBaseWindow.dispatchEvent(XBaseWindow.java:1092)
	at sun.awt.X11.XBaseWindow.dispatchToWindow(XBaseWindow.java:1069)
	at sun.awt.X11.XToolkit.dispatchEvent(XToolkit.java:468)
	at sun.awt.X11.XToolkit.run(XToolkit.java:626)
	at sun.awt.X11.XToolkit.run(XToolkit.java:560)
	at java.lang.Thread.run(Thread.java:619)