4 Replies - 233 Views - Last Post: 15 November 2012 - 08:26 AM Rate Topic: -----

#1 Scrufeh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-November 12

Tutorial: How to make a fullscreen window(properly)

Posted 15 November 2012 - 03:30 AM



This is the most compatible and reliable way to setup a fullscreen window.

package main;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class Screen 
{
	static JFrame frame = new JFrame();
	static GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
	static GraphicsDevice screen = graphicsEnvironment.getDefaultScreenDevice();
	static DisplayMode[] compatible = screen.getDisplayModes(); 
	static DisplayMode[] workable = 
		{                                              //Displaymodes that we allow
			new DisplayMode(1680,1050,32,0),
			new DisplayMode(1680,1050,24,0),
			new DisplayMode(1680,1050,16,0),
			new DisplayMode(1440,900,32,0),
			new DisplayMode(1440,900,24,0),
			new DisplayMode(1440,900,16,0),
			new DisplayMode(1280,800,32,0),
			new DisplayMode(1280,800,24,0),
			new DisplayMode(1280,800,16,0),
			new DisplayMode(800,600,32,0),
			new DisplayMode(800,600,24,0),
			new DisplayMode(800,600,16,0),
			new DisplayMode(640,480,32,0),
			new DisplayMode(640,480,24,0),
			new DisplayMode(640,480,16,0)
		};
	static DisplayMode displayMode;

	public static boolean frameWorkable()            //comparing what we want and what the monitor can handle
	{
		boolean isWorkable = false;
		
		for(int x=0 ; x<workable.length ; x++)
		{
			for(int y=0 ; y<compatible.length ; y++)
			{
				if(workable[x] == compatible[y])
				{
					isWorkable = true;
					displayMode = workable[x];
				}
				else
				{
					isWorkable = false;
				}
			}
		}
		
		return isWorkable;
	}
	
	public static void initFrame()   //called to start the frame
	{
		if(frameWorkable())
		{
			frame.setResizable(false);
			frame.setUndecorated(true);
			screen.setFullScreenWindow(frame);
			
			if(displayMode != null && screen.isDisplayChangeSupported())
			{
				try
				{
					screen.setDisplayMode(displayMode);
				}
				catch(Exception E){}
				
				frame.createBufferStrategy(2);
			}
		}
	}
	
	public static void updateFrame()
	{
		Window w = screen.getFullScreenWindow();
		if(w != null)
		{
			BufferStrategy s = w.getBufferStrategy();
			if(!s.contentsLost())
			{
				s.show();
			}
		}
	}
	
	public static void disposeFrame()
	{
		Window w = screen.getFullScreenWindow();
		if(w != null)
		{
			w.dispose();
		}
		screen.setFullScreenWindow(null);
	}
}   



This will check the computer it runs on and create a frame suitable to the computers maximum specifications.
This code also double buffers, which means no flashing screen, sets to a fullscreen window in compliance with the monitor, updates and disposes of the frame.

This is only a basic window but adding your own methods to edit what the screen should display depends on your project so i cannot help in this tutorial.

Till next time -Scrufeh

Is This A Good Question/Topic? 0
  • +

Replies To: Tutorial: How to make a fullscreen window(properly)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tutorial: How to make a fullscreen window(properly)

Posted 15 November 2012 - 07:06 AM

Honestly... not elegant at all, and not OO at all (why using Java ?)... sorry :(

Why all those static variables you shouldn't have a single one static variable
Why building display mode based on your wishes rather than on the monitor possibilities ?
Why having a JFrame declared (static again) when you class Screen can simply extends JFrame ?
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: Tutorial: How to make a fullscreen window(properly)

Posted 15 November 2012 - 08:06 AM

Not only what pbl said, but if I am not mistaken, settind the display mode like that is still incompatible with Linux based operating systems.
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Tutorial: How to make a fullscreen window(properly)

Posted 15 November 2012 - 08:15 AM

If you want to submit a tutorial, then please do so properly in the Java Tutorials section so that it can be properly reviewed before being made available.

Closed.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Tutorial: How to make a fullscreen window(properly)

Posted 15 November 2012 - 08:26 AM

View Postfarrell2k, on 15 November 2012 - 11:06 AM, said:

if I am not mistaken, settind the display mode like that is still incompatible with Linux based operating systems.


Not a Unix espert but if this is true, we are far from

Quote

This is the most compatible

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1