Hmm, I am writing a tiled game engine right now (isometric) but logic like this is exactly the same regardless - so I believe I may be able to help you here.
I am not sure if I quite understand your question - but I believe I can give you a solid explanation to how I do it, and it may help you.
First of all, because you seem to be new to programming in general, here are some tips I will throw at you right away that will make your life a lot easier:
(You can skip this if you are just looking for an explanation to your problem, but I highly suggest some time later going over this and looking in to what I am saying here)
Variable names should usually start with a lower-case so you can differentiate them between a class name (which should usually start with an upper-case) and a variable name. Consider you have a class named Tile, you now end up with a declaration that looks like:
Tile Tile = new Tile();
Tile.x = 5; //etc
and ‘Tile’ because ambiguous because ‘x’ could be a static variable you are accessing in Tile, or it could be a member variable you are accessing that belongs to the instance of Tile named Tile (even that is confusing to comprehend!) - while Java resolves this appropriately it is still very confusing to any programming looking at the code.
Member variables usually start with some indication to suggest that they are a member variable that exists in the entire scope of the class. Some people prefer not to do this (and it is somewhat controversial among programmers) but I would say it can’t hurt and it makes your code look cleaner. Most people either prefix member variables with _ or m_
Public member variables are usually avoided just because it’s dangerous to expose these members directly unless the class is very simple (like a Vector, Matrix, Rectangle or Point). Instead people use get\set accessors. Some code (code that performs a dense and large amount of iterations over a time critical period like collision detection) can greatly benefit from directly access variables rather than using accessors - but I would say the rule of thumb should be to use get\set accessors - and then only if your profiler tells you that you can benifit from removing the accessors from the algorithm, do it.
Now on to the actual problem:
Like I said, I don’t quite understand your question, so instead I will explain the process, and you can ask for details if you’d like. This describes a simple tiled map implementation, however the implementation would be different for something more complex (if you want more details on that just ask):
Your map is going to consists of a series of tiles, tiles contain information such as location, image (source image), maybe a rectangle describing what part of that image constitutes the visual depiction of the tile, some basic attributes like isBlocking, and a location. So lets make the problem simpler by creating a Tile class to describe our tile.
public class Tile
{
private Image m_sourceImage;
private Rectangle m_sourceRectangle;
private boolean m_isBlocking;
public Tile(Image srcImage, Rectangle srcRectangle, boolean isBlocking)
{
m_sourceImage = srcImage;
m_sourceRectangle = srcRectangle;
m_isBlocking = isBlocking;
}
public void render(Graphics2D g, int x, int y)
{
//draw our tile at x, y using g
}
public boolean isBlocking()
{
return m_isBlocking;
}
}
Great, now we have a tile class that mostly takes care of itself. Now, lets think about our world.
Our world is going to consists of a series of tiles. We’ll store our tiles in a single-dimensional array.
Here is how your world might look:
(I don’t gurantee that this code works, my algorithm may be wrong, but it will at least give you a head-start.)
public class World
{
//Store our world's dimensions
private int m_worldWidth;
private int m_worldHeight;
//Our world will consists of tiles each of equal dimensions, lets store this information as well.
private int m_tileWidth;
private int m_tileHeight;
//We'll store our tiles here
private Tile[] m_tiles;
//When create our world, it needs some information such as the world and tile dimensions
//We pass this information in through the constructor
public World(int worldWidth, int worldHeight, int tileWidth, int tileHeight)
{
m_worldWidth = worldWidth;
m_worldHeight = worldHeight;
m_tileWidth = tileWidth;
m_tileHeight = tileHeight;
//Create our array.
m_tiles = new Tile[worldWidth * worldHeight];
//Initialize your world's tiles.
for(int i = 0; i < m_tiles.length; i++)
{
m_tiles[i] = new Tile(...);
}
}
public Tile getTile(int iX, int iY)
{
//Calculate the index from the x\y location of the tile.
int tileIndex = iY * m_worldWidth + iX;
if(tileIndex > m_tiles.length)
return null; //You'd want to throw an exception here for code-safety purposes, but we'll just return null for simplicity sake.
return m_tiles[tileIndex];
}
//If you use this, you need to make sure that iX and iY are relative to the corner of the map.
//This allows you to pass a coordinate (i.e, the location of a mouse click) and have it translated to the corresponding tile.
public Tile pick(int iX, int iY)
{
return getTile(iX / m_tileWidth, iY / m_tileHeight);
}
//We call this to draw our tiled map onto the specified graphics object
public void render(Graphics2D g, int offsetX, int offsetY)
{
for(int y = 0; y < m_worldHeight; y++)
{
for(int x = 0; x < m_worldWidth; x++)
{
getTile(x, y).render(g, x * m_tileWidth + offsetX, y * m_tileHeight + offsetY);
}
}
}
}