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

New Topic/Question
This topic is locked



MultiQuote






|