i have made somehing with full screen excluseive awt mode but it runs so slow :s
This is my code so far
package ttgj;
import java.awt.*;
import java.awt.image.VolatileImage;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test
{
/*
* Attributes
*/
private JFrame mainFrame;
private BufferStrategy bufferStrategy;
private BufferedImage bi;
private int ScreenWidth, ScreenHeight;
private boolean Running = true;
// A variable used with render()
private int i = 0;
//map
private Tile[][] tiles = new Tile[25][25];
//a unit that moves
private Bus b;
// Our main method simply invokes an instance of our application. We pass it a boolean
// argument that is TRUE if we want full screen exclusive mode, or false if we want a
// window.
public static void main( String[] args )
{
Test test = new Test( true );
System.exit( 0 );
}
// This is an array of the desired full screen graphics modes, in the order we most
// prefer them. If we request full screen mode, and this system supports it, then the
// first entry here that matches a supported mode will be used.
private static DisplayMode[] PreferredModes = new DisplayMode[]
{
new DisplayMode( 1024, 768, 32, 0 ),
new DisplayMode( 1024, 768, 16, 0 ),
new DisplayMode( 1024, 768, 8, 0 )
};
// This method finds the best matching full screen mode from the array above that is
// supported on this system.
private DisplayMode bestmode( GraphicsDevice gd )
{
// Get the list of supported modes on this system
DisplayMode[] modes = gd.getDisplayModes();
//For every mode we'd prefer to use...
for( int j = 0; j < PreferredModes.length; j++ )
// ...we check it against each mode the system supports...
for( int i = 0; i < modes.length; i++ )
// ...and if we find a matching entry we return it.
if( modes[i].getWidth() == PreferredModes[j].getWidth()
&& modes[i].getHeight() == PreferredModes[j].getHeight()
&& modes[i].getBitDepth() == PreferredModes[j].getBitDepth() )
return PreferredModes[j];
// ...we couldn't find a matching mode, return null.
return null;
}
// Our appliclation
public Test( boolean fs )
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
DisplayMode dm = bestmode( gd );
//If we can't have one of our preferred display modes, or the system does not support
//full screen exclusive mode, then we'll stick with a window.
if( dm == null || gd.isFullScreenSupported() == false ) fs = false;
// Create our main frame and set attributes common to both FS and windowed modes.
mainFrame = new JFrame( gc );
mainFrame.setLayout( null );
mainFrame.setIgnoreRepaint( true );
// If full screen is wanted (and possible), set it up.
if( fs )
{
mainFrame.setUndecorated( true ); // No window decorations
gd.setFullScreenWindow( mainFrame ); // Create a full screen window
gd.setDisplayMode( dm ); // Change to our preferred mode
ScreenWidth = dm.getWidth(); // Get the screen dimensions
ScreenHeight = dm.getHeight();
}
// Otherwise we use a window with a fixed size
else
{
ScreenWidth = 1024; // Default window size
ScreenHeight = 768;
mainFrame.setSize( new Dimension( ScreenWidth, ScreenHeight ) );
mainFrame.setVisible(true); // Show it
}
// Set up our buffer strategy (with double buffering)
mainFrame.createBufferStrategy( 3 );
bufferStrategy = mainFrame.getBufferStrategy();
//load the images
bi = null;
try {
bi = ImageIO.read( new java.io.File("Images\\tile.png"));
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
//create a bus
b = new Bus ( new Point (32,16), new Point (20*65,20*16));
ExecutorService threadExecutor = Executors.newCachedThreadPool();
threadExecutor.execute (b);
threadExecutor.shutdown();
//create the map
makeTiles ();
// Main game loop
while( Running )
{
// Call our render function to draw everything.
render((Graphics2D) bufferStrategy.getDrawGraphics());
//Sleep
try {
Thread.sleep( 100 );
} catch( InterruptedException e ) {}
}
}
// This method is called once every frame to display the current state of our game.
private void render(Graphics2D g2d )
{
mainFrame.repaint();
// Clear the drawing buffer to white
g2d.setColor( Color.black );
g2d.fillRect( 0, 0, ScreenWidth, ScreenHeight );
//draw Tiles
for (int i = 0; i < tiles.length; i++){
for (int j = 0; j < tiles[i].length; j++) {
Point pos = new Point(tiles[i][j].getX(), tiles[i][j].getY());
g2d.drawImage(bi, pos.x, pos.y,mainFrame);
}
}
//draw bus
g2d.drawImage(b.getAppearance(), b.getPosition().x, b.getPosition().y, mainFrame);
// We'll stop after 4000 frames.
i++;
if( i > 4000 ) Running = false;
bufferStrategy.show();
g2d.dispose();
}
private void makeTiles ()
{
int maxTileW = 70, mapW = maxTileW * 25;
int x = (mapW - maxTileW) /2;
int y = 0;
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++)
tiles[i][j] = new Tile (x + j * 32, y + j * 16);
x -= 32;
y += 16;
}
}
}