Re: Question about Full screen exclusive mode
+----+
|\ /|
| \/ |
| /\ |
|/ \|
+----+
OK I prepared such a version.
- Test.java 66 lines
- ScreenManager.java 73 lines
Hope it will work :)
------------------- 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 );
test.drawLoop();
}
catch( Throwable t )
{
t.printStackTrace( System.err );
}
finally
{
test.screen.restoreScreen();
test.screen.setFullScreen( test.oldMode );
test.screen.restoreScreen();
}
}
private void drawLoop() {
long start = System.currentTimeMillis(),
current = start;
while( current < start + 10000 )
{
current = System.currentTimeMillis();
java.awt.Graphics2D graphics = this.screen.getGraphics();
draw( graphics );
this.screen.update();
try
{
Thread.sleep( 25 );
}catch( InterruptedException ex) { /**/ }
}
}
private void draw( java.awt.Graphics2D g ) {
g.setPaint( java.awt.Color.blue );
g.fillRect(0, 0, testedMode.getWidth(), testedMode.getHeight() );
g.setPaint( java.awt.Color.white );
g.setFont( new java.awt.Font("Dialog", java.awt.Font.BOLD, 20) );
g.drawString( "Current resolution: "
+testedMode.getWidth()+"x"+testedMode.getHeight(), 50, 100);
g.drawRect(0, 0, testedMode.getWidth()-1, testedMode.getHeight()-1 );
g.draw( new java.awt.geom.Line2D.Double( 0,0,testedMode.getWidth()-1,
testedMode.getHeight()-1 ) );
g.draw( new java.awt.geom.Line2D.Double( testedMode.getWidth()-1, 0,
0, testedMode.getHeight()-1 ) );
}
}
------------------- Test.java --------------------------
.... and ...
------------------- ScreenManager.java --------------------------
package temp;
import java.awt.*;
import java.awt.image.BufferStrategy;
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 );
}
public Graphics2D getGraphics() {
Window window = device.getFullScreenWindow();
if (window != null) {
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else {
return null;
}
}
public void update() {
Window window = device.getFullScreenWindow();
if (window != null) {
BufferStrategy strategy = window.getBufferStrategy();
if (!strategy.contentsLost()) {
strategy.show();
}
}
}
}
------------------- ScreenManager.java --------------------------