# Get coordinates of arraylist

Here's my map class:

``````public class map{ public static final int CLEAR = 0;
public static final ArrayList<Integer> STONE = new ArrayList<Integer>();
public static final int GRASS = 2;
public static final int DIRT = 3;

public static final int WIDTH = 32;
public static final int HEIGHT = 24;

public static final int TILE_SIZE = 25;

// static int[][] map = new int[WIDTH][HEIGHT];
ArrayList<ArrayList<Integer>> map = new ArrayList<ArrayList<Integer>>(WIDTH * HEIGHT);

enum tiles {
air, grass, stone, dirt
}

Image air, grass, stone, dirt;

Random rand = new Random();

public Map() {

/* default map */

/*for(int y = 0; y < WIDTH; y++){
map[y][y] = (rand.nextInt(2));
System.out.println(map[y][y]);
}*/

/*for (int y = 18; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
map[x][y] = STONE;
}

}

for (int y = 18; y < 19; y++) {
for (int x = 0; x < WIDTH; x++) {
map[x][y] = GRASS;
}

}

for (int y = 19; y < 20; y++) {
for (int x = 0; x < WIDTH; x++) {
map[x][y] = DIRT;
}

}*/

for (int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++){
map.set(x * WIDTH + y, STONE);
}
}

try {
init(null, null);
} catch (SlickException e) {
e.printStackTrace();
}
render(null, null, null);

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
air = new Image("res/air.png");
grass = new Image("res/grass.png");
stone = new Image("res/stone.png");
dirt = new Image("res/dirt.png");
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {

for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
switch (map.get(x * WIDTH + y)) {
case CLEAR:
air.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case STONE:
stone.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case GRASS:
grass.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case DIRT:
dirt.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
}
}
}
}

public static boolean blocked(float x, float y) {
return map[(int) x][(int) y] == STONE;
}

public static Rectangle blockBounds(int x, int y) {
return (new Rectangle(x, y, TILE_SIZE, TILE_SIZE));
}
``````

}

Specifically I am looking at this:

``````for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
switch (map.get(x * WIDTH + y).intValue()) {
case CLEAR:
air.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case STONE:
stone.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case GRASS:
grass.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case DIRT:
dirt.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
}
}
}
``````

How can I access the coordinates of my arraylist map and then draw the tiles to the screen? Thanks!

-
Well it looks like what you are doing is totally fine for drawing. but I think the rest of your code is a bit messed up. You declare your map as an ArrayList<ArrayList<int>> rather than just ArrayList<tiles>. It looks like you started with a 2D array, decided to flatten it, then forgot to fix the other code. The obvious example being the "blocked" function. Your code should work if you fix your map object. – Benjamin Danger Johnson Nov 10 '12 at 1:08
Yeah sorry I just converted all the code over from an array and forgot to work on the methods... The render method actually doesn't work at all, the `switch(map.get(x * WIDTH + y).intValue());` does not work at all. – opiop65 Nov 10 '12 at 1:10
can you please update your code? You should change your double arraylist away from storing type Integer (basically int that allows nulls) to a single arraylist that stores the enum type Tile (or I guess tiles in your case) it's going to be hard to find the exact with all these weird mistakes thrown in (obviously the switch function can't use the map if it is value is an arraylist) – Benjamin Danger Johnson Nov 12 '12 at 16:54
Also can you try blowing away all your code and verify that your tile images can draw? Something like "public void render(GameContainer gc, StateBasedGame sbg, Graphics g) { air.draw(0, 0, TILE_SIZE, TILE_SIZE); }" is a sufficient test. – Benjamin Danger Johnson Nov 12 '12 at 16:56
Really confusingly worded. Also would seem more on topic on StackOverflow. – Anko Dec 10 '12 at 18:09

Usually when I work on something like this I make a couple helper functions to covert to and from my map data. This makes it easy to not only place tiles when needed but also gives an easy way to find out what tile the user has clicked on.

so something like this.

``````ArrayList<integer> map = new ArrayList<integer>(WIDTH * HEIGHT);

Sudo Code:
TILE_SIZE = I am assuming your tiles are the same width and height.
FLOOR = should be a standard math function (I don't know java)
MAP_WIDTH = Tiles wide not pixels.

world coords to container index (int x, int y) returns index in array
iy = FLOOR(x / TILE_SIZE)   // notice that I have swaped the x and y
ix = FLOOR(y / TILE_SIZE)   // because of how the data is stored.
return ix * MAP_WIDTH + iy

container index to world coords(index i) returns x and y coordinates POINT(x,y)
y = FLOOR(i / MAP_WIDTH)  // this pulls out the y coord.
x = i - y;                // what is left over is the x.
return POINT(x * TILE_SIZE, y * TILE_SIZE)
``````

hope this helps.

-