accessing elements in a 2d ArrayList
well, im almost done with my map editor except for one pretty big thing... i am having problems with drawing on the 2d ArrayList.
i start out by declaring and instantiating the 2d arraylist.
Code :
private ArrayList<ArrayList<Integer>> tileMap;
public TileMap(int rows, int columns){
for(int r=0;r<rows;r++){
tileMap.add(new ArrayList<Integer>());
for(int c=0;c<columns;c++)
tileMap.get(r).add(-1);
}
i added a getTile(row,col) and a setTile(row,col,tile) method to make the rest of the code look cleaner
Code :
public void setTile(int row,int col,int number){
if(row>=0 && col>=0 && row<rows && col<columns)
tileMap.get(row).set(col,number);
}
public int getTile(int row,int col){
return tileMap.get(row).get(col);
}
then everything gets messed up when it draws. ive been trying very hard to figure it out but am having some difficulty.
Code :
public void mouseDragged(MouseEvent e){
if(getTile(e.getY()/tileSet.tileHeight(),e.getX()/tileSet.tileWidth())!=tilePane.getCurrentTile())//if not already currentTile
setTile(e.getY()/tileSet.tileHeight(),e.getX()/tileSet.tileWidth(),tilePane.getCurrentTile());//make it currentTile
}
the x and y cords are flipped when drawing, but i feel that everything should be working as is.
http://i189.photobucket.com/albums/z.../mapeditor.jpg
Re: accessing elements in a 2d ArrayList
maybe you are passing row and coloumn the wrong way around?
Re: accessing elements in a 2d ArrayList
thats what i thought but it seemed right, ill check again