Re: Question about Full screen exclusive mode
I tryied to prepare working SSCCE version
and made it as shorter as i could.
I hope it will work ;]
There are 2 files
- Test( with main method ) 42 lines
- ScreenManager 52 lines
----------------- Test.java ------------------
public class Test
{
ScreenManager screen = null;
java.awt.DisplayMode oldMode = null;
java.awt.DisplayMode testedMode = null;
private Test()
{
screen = new ScreenManager();
}
public static void main( String[] args ) {
Test.Run( 1024, 768, 32, 70);
Test.Run( 1280, 960, 32, 70);
}
public static void Run( int width, int height, int bitDepth, int
refreshRate )
{
Test test = new Test();;
test.testedMode = new java.awt.DisplayMode( width, height, bitDepth,
refreshRate );
test.oldMode = test.screen.getCurrentDisplayMode();
try
{
test.screen.setFullScreen( test.testedMode );
try
{
Thread.sleep( 5000 );
}catch( InterruptedException ex ) { /*...*/ }
}
catch( Throwable t )
{
t.printStackTrace( System.err );
}
finally
{
test.screen.restoreScreen();
test.screen.setFullScreen( test.oldMode );
test.screen.restoreScreen();
}
}
}
----------------- Test.java ---------------------------
and...
----------------- ScreenManager.java ------------------
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.*;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
public class ScreenManager {
private GraphicsDevice device;
private DisplayMode basicMode;
public ScreenManager() {
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
basicMode = device.getDisplayMode();
}
public DisplayMode getCurrentDisplayMode() {
return device.getDisplayMode();
}
public void setFullScreen(DisplayMode displayMode) {
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false);
device.setFullScreenWindow(frame);
if (displayMode != null &&
device.isDisplayChangeSupported())
{
try {
device.setDisplayMode(displayMode);
}
catch (IllegalArgumentException ex) { }
}
frame.createBufferStrategy(2);
}
public Window getFullScreenWindow() {
return device.getFullScreenWindow();
}
public void restoreScreen() {
device.setDisplayMode( basicMode );
Window window = device.getFullScreenWindow();
if (window != null) {
window.dispose();
}
device.setFullScreenWindow( null );
}
}
----------------- ScreenManager.java ------------------