Setting tile size with BufferedImage

I’m starting to work on a tile-based game engine. I plan on having sets of tiles in an image so that the engine can open the image and get all the tiles it needs quickly. The javadocs for BufferedImage list quite a few methods for getting Tile properties, but none for setting them. Do such methods exist? If so, where are they?

Thanks for any help,
–UJ
–ximwix.net

I’d be tempted to avoid the internal tiling methods in the BufferedImage, when I used one single BufferedImage with many subsections for tiles I got really bad performance. I think caused by the fact that because its so much larger the automatic image/caching doesn’t work nearly as well (if at all).

Instead you’ll probably want a single tileset image on which you load in and chop up at runtime into lots of smaller BufferedImages, one for each tile (not using BufferedImage.getSubImage because thats not a true copy). Then discard your original tileset image.

If you’re looking for a nice way of storing lots of different sized tiles in one image then theres a good description of using control pixels in the Isometric game programming book :slight_smile:

Well, I spent some time looking at tutorials and slapping sample code together. My plan for this was to have a tileset that was 60x20, containg three 20x20 tiles. I wanted to open that image (tileset.gif) and make three bufferedImages out of it (tile1, tile2, tile3). Then I wanted to make a window, size 60x60, holding nine 20x20 tiles. I got code to compile, but it doesn’t yield a window at all. Here is the code:


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.*;

public class Tilethingy extends Frame implements Runnable {
  BufferedImage tile1;
  BufferedImage tile2;
  BufferedImage tile3;

  Image offScreenImage;
  Graphics offScreenGraphics;

  Thread animationThread;


  public static void main( String args[] ) {
    new Tilethingy();
  }

  public void Tilethingy() {
    //
    //create a bufferedimage tileset from the file "tileset.gif"
    //
    Image tilesetimage = this.getToolkit().getImage("tileset.gif");
    BufferedImage tileset = new BufferedImage(tilesetimage.getWidth(this), tilesetimage.getHeight(this), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = tileset.createGraphics();
    g2d.drawImage(tilesetimage, 0,0, this);
    
    //
    //now that there is a tileset, extract the tiles into individual bufferedimages
    //
    //tile 1
    Raster temp = tileset.getData( new Rectangle( 0, 0, 20, 20 ) );
    tile1 = new BufferedImage( 20, 20, BufferedImage.TYPE_INT_RGB );
    tile1.setData( temp );

    //tile 2
    temp = tileset.getData( new Rectangle( 20, 0, 20, 20 ) );
    tile2 = new BufferedImage( 20, 20, BufferedImage.TYPE_INT_RGB );
    tile2.setData( temp );

    //tile 3
    temp = tileset.getData( new Rectangle( 40, 0, 20, 20 ) );
    tile3 = new BufferedImage( 20, 20, BufferedImage.TYPE_INT_RGB );
    tile3.setData( temp );
    
    //
    //set up the frame and display it
    //
    //Display the frame
    setSize( 60, 60 );
    setVisible( true );
    setTitle( "broken");

    //start the thread for some reason
    animationThread = new Thread(this);
    animationThread.start();
  
    //Window closer
    this.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    } );
  }

  public void run() {
    while ( true ) {
      repaint();
    }
  }

  public void paint( Graphics g ) {
    //
  }

  public void update( Graphics g ) {
    if ( offScreenGraphics == null ) {
      offScreenImage = createImage( 60, 60 );
      offScreenGraphics = offScreenImage.getGraphics();
    }

    //row 1
    offScreenGraphics.drawImage( tile1, 0, 0, this );
    offScreenGraphics.drawImage( tile2, 20, 0, this );
    offScreenGraphics.drawImage( tile3, 40, 0, this );

    //row 2
    offScreenGraphics.drawImage( tile3, 0, 20, this );
    offScreenGraphics.drawImage( tile2, 20, 20, this );
    offScreenGraphics.drawImage( tile1, 40, 20, this );

    //row 3
    offScreenGraphics.drawImage( tile2, 0, 40, this );
    offScreenGraphics.drawImage( tile2, 20, 40, this );
    offScreenGraphics.drawImage( tile2, 40, 40, this );

    if ( offScreenImage != null ) {
      g.drawImage( offScreenImage, 0, 0, this );
    }
  }
}

It’s obviously pretty messy since there was a lot of copy/paste/edit from tutorials, but I want to know if there’s just one or two things wrong with it or if there’s a lot wrong with it and I should just scrap it altogether.

All help is appreciated,
–UJ
–ximwix.net