I have looked for days and days, but could not find a solution to my problem.
How do you tile a map in java?
When i worked with XNA the array.length gave me more or less an x and y, but java does not. If someone can post either source code or instructions on how to do this it would be extremely helpful.
Thanks
~Scyth
EDIT: Solved, for others this is the code:
Class Variables Needed:
public static final int BLACK = 0;
public static final int GRASS = 1;
public static final int Magenta = 2;
int[][] tiles;
InputStream level1 = this.getClass().getResourceAsStream("level.txt");
Constructor must have this code:
public Game()
{
// Reads the text file
try {
tiles = getIntsFromTextFile(level1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Your Render Methods:
public void render(Graphics2D g) {
renderTiles(g);
}
public void renderTiles(Graphics2D g) {
// Iterate through all tiles:
for (int y = 0; y < tiles.length; y++) {
for (int x = 0; x < tiles[y].length; x++) {
renderTile(x, y, tiles[y][x], g);
}
}
}
public void renderTile(int x, int y, int tileID, Graphics2D g) {
switch (tileID) {
// don't forget the breaks! :)
case BLACK: g.setColor(Color.black); g.drawRect(x * 32, y * 32, 32, 32); break;
case GRASS: g.setColor(Color.green); g.drawRect(x * 32, y * 32, 32, 32); break;
case MAGENTA: g.setColor(Color.MAGENTA); g.drawRect(x * 32, y * 32, 32, 32); break;
}
}
Your methods to read the text file:
public int[][] getIntsFromTextFile(InputStream input) throws IOException {
// Create a list of rows. It needs to be resizable, because
// we don't know the number of rows the text file has in the
// beginning.
ArrayList<int[]> yList = new ArrayList<int[]>();
// A bit try-finally stuff, so we can close the reader properly.
BufferedReader reader = null;
try {
// We create a BufferedReader by putting InputStreamReader-
// glue between the Reader and the FileInputStream.
reader = new BufferedReader(new InputStreamReader(input));
// This variable counts the y rows.
int y = 0;
// This variable will hold every newly read line
String line = null;
// This reads a line and puts it into "line".
// line is then tested against null, because when
// the line is null, then we reached the end of the
// file.
while ((line = reader.readLine()) != null) {
// We parse the string into a int[] and put it
// into the list of rows.
yList.add(parseString(line));
}
} finally {
// "finally" we close the reader ;)
if (reader != null) {
reader.close();
}
}
// This now creates an int[][] out of the ArrayList<int[]>.
// This can be done, since we know the number of rows
// now. You can see ArrayList's as a resizable array.
int[][] ints = new int[yList.size()][];
for (int i = 0; i < ints.length; i++) {
ints[i] = yList.get(i);
}
return ints;
}
public int[] parseString(String str) {
// We can create a int[], since we know
// the length of the incoming string,
// which is for example: "00192773716749".
int[] array = new int[str.length()];
// We loop through each character in the string
// and create an integer from the returned char
// with Integer.parseInt(...);
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
// I say "" + c to create a string
// out of the char. I'm not sure if
// it's needed. You could try it without.
array[i] = Integer.parseInt("" + c);
}
return array;
}
Voila! You’re done, all you have to do is create and edit a level.txt file and it will display the corresponding tiles in the correct position, if our tile are not 32x32 then you should change *32 in each of the cases:
public void renderTile(int x, int y, int tileID, Graphics2D g) {
switch (tileID) {
// don't forget the breaks! :)
case BLACK: g.setColor(Color.black); g.drawRect(x * 32, y * 32, 32, 32); break;
case GRASS: g.setColor(Color.green); g.drawRect(x * 32, y * 32, 32, 32); break;
case MAGENTA: g.setColor(Color.MAGENTA); g.drawRect(x * 32, y * 32, 32, 32); break;
}
}