Making texture tiles

Hey guys, new to the forums here and pretty new to java, I just finished my first college course on java and am really wanting to expand my knowledge so i thought the best way of doing it was to create a 2d game!
First I’ll link you my code for my problem and then I’ll explain.
http://pastie.org/8996412

Anyways, heres my situation Currently I’m working on making a backround that is made up of 16x16 pixel tiles on line 16 you can see that my “backround” or “map” will be 16 tiles x 14 tiles.

On lines 11 and 12 I am importing png files to a seperate array called “texture”

What I am wanting to do is to set this up so when I type in a value in my 2d array than it will put whatever texture in that space on the “map”.

Example:
map[0][0]= 0; //this will put a dirt texture at 0,0
map[1][1]= 1; //this will put a grass texture at 1,1

I guess another way of explaining it would be that I am trying to load all the tiles I could possibly use in the texture array on line 9 and being able to “build” my map using those textures in my 2d array.

I would reccomend making a class that represents a Tile (e.g. class Tile). You can give this tile a width, height, x, y, texture and special properties. Then you have a render method in this class that draws the texture onto the screen you want it on.

Your map (e.g. class Map) then consists of a width, height, special properties and then a list of tiles. For each Tile in that list you make sure you render it (whenever and however you’d like)

It might not be clear, I’m very bad at explaining things ;D (At least I try)

It sounds like you know what you want to do so I don’t really get what you are asking…
But here is an example of a way to render a tiled map:


for(int x = 0; x < map.length; x++)
   for(int y = 0; y < map[0].length; y++)
      {
           int id = map[x][y];
           Image img = texture[id];
           g.drawImage(img, x*16, y*16);
       }

This will iterate over all the values in your 2d array and draw the corresponding image…
Like you say yourself you should just have an unique id for each type of tile and then use that to draw with

Hope that helped :slight_smile:

First of all, use code tags. You can see a # icon when writing a post which will put code in this nice code format.

Second of all, you’re not asking a question. You’re asking us to make you a basic game.

The only answer to your question is that you need to iterate through your array and render each tile.


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Image;

public class Backround {
	
BufferedImage [] texture = new BufferedImage[2];{
try {
	texture[0]=ImageIO.read(new File("dirt.png"));
	texture[1]=ImageIO.read(new File("grass.png"));
} catch (IOException e) {e.printStackTrace();}
}

int [][] map = new int[16][14];{
}
}

Sorry about the confusion, still kinda new to all this. My problem /question is that I’m not sure how to make it so I can just put values inside my map array that refer to my texture array so that I can use those images.
the only problem is that my map array only accepts integers. So I need to make it so that I can refer to the different elements of the texture array so that I can place that tile/texture on the given coordinate.

I’m not sure if that clears it up or not.

I’m not too sure what you are asking but I think that this is what you mean.

for(int i= 0; i < map.length; i++) // Iterate through all your map elements
{
     for(int j = 0; j < map[0].length; j++)
     {
          g.drawImage(texture[map[i][j]], x, y); //Draw the texture on the map. e.g. map[0][0] = 2; so you are drawing it like texture[2]
     }
}

So your map would hold integers of what textures you want to draw.

You will probably want a class to hold all your information like hwinwuzhere suggested. Then you would change your map to hold that tile class instead of Integers.


// In your main class
public Tile[][] map = new Tile[16][14];

public void paint(Graphics g)
{
     for(int i = 0; i < map.length; i++)
          for(int j = 0; j < map[0].length; j++)
               map[i][j].draw(g, textures);
}

// In another file
public class Tile
{
     private int id, x, y;

     public Tile(int id, int x, int y)
     {
          this.id = id;
          this.x = x;
          this.y = y;
     }

     public void draw(Graphics g, BufferedImage[] textures)
     {
          g.drawImage(textures[id], x, y);
     }
}

I hope this helps. :slight_smile:

I think this is exactly what I’m looking for, thank you so much! (: and thanks to everyone else!

You might want to catch that exception in case those files are moved.

Are you referring to that image loading? Because he’s already printing a stack trace.