Which components/layout

Hey

I am trying to convert a game to java. But i cant find the right components and layout to use.
I trie with a jframe and internalframes but i cant use that any longer becaus i now use
GraphicsDevice and displaymode to set it fullscreen and change the screenresolution

Here’s a screen: http://www.openttd.org/images/screens/1.png

Can anyone help?

thx

Looks like a tile based game.

I wouldnt use Swing at all (or any AWT widgets either). Id go Full Screen Exclusive AWT with active rendering r OGL and in either case just draw the dialogs as graphics each frame.

i had first made a version with xith3d as well but my tilemap looks al screwy and bad compared to my java2d version
I made a post about it here http://www.java-gaming.org/forums/index.php?topic=11992.0
Or do you meen something else with Full Screen Exclusive AWT with active rendering r OGL?

Also which type of images should i use? image, bufferedimage or volatileimage?

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;
		}
	}
} 

i calculated the time it took to draw all tiles and i came to 344 ms :o :o
This is so incredibile much :-[

Why are you repainting the frame when you setIgnoreRepaint(true) ? That should have no effect.

Don’t create new Point objects every time. You don’t use them for anything.

As for the performance, what are you using Thread pools for? Doing something with a bus!?

Anyway, Transport Tycoon is a really nice game. Be sure to fix the money overflow bug when building bridges more expensive than 32 bits :slight_smile:

the repaint is gone
my bus moves with the help of a thread (the code of bus isnt pasted
I now drew the bi first on a volatileImage which speed things up but now the iamges arent tranparent anymore

you can test it with this http://users.skynet.be/fa006997/Test.jar

i use tranparency.translucent option

You shouldn’t use a Thread to manage a bus. It’s extremely much more demanding for the system, and the synchronization will cause trouble. Most games have a loop which updates the sprites (such as the bus) and graphics in turn. Multiple threads are useful for when you want to listen for network input, for example, and when you want heavy calculations to not stop your GUI from working. They are not efficient when you have to update everything continuously anyway.

but if i use them i can set different sleeptimes to simulate the different speeds of the vehicles

That is definitly a good way to do it. Make all your calculations time based. Then you can just set a velocity for the vehicles and it will be more accurate. hread.sleep() has no real guaruntees about how it works except that it will sleep for at least the amount of time specified. The more vehicles you get the more screwed up it will become.

It aint the vehicles who are the problem its the map
I display around 900 tiles on 1 screen so he has to repaint trhem every frame which makes it very slow :s

Stop abusing Threads! And you can minimize the drawing of tiles if you make a validation/invalidation system so tiles are only rendered once unless something is moving exactly at that location.

I have to redraw my screen every frame because i usefullscreen mode

You don’t have to redraw every frame.

But it shouldnt be hurting you either if you are doing it right. I did 50 animated characters on a free-scrolling tiled map at something better then 24fps ona laptop without any tuning when FullScreen and active rednering first became available. if anything its improved since then.

I’d profile and find out where your time is going.

but i use double buffer so every thime i show it my frame gets cleared so i have to draw it all over again no?