publicint[][] getIntsFromTextFile(InputStreaminput) throwsIOException {// 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 = newArrayList<int[]>();// A bit try-finally stuff, so we can close the reader properly.BufferedReaderreader = null;try {// We create a BufferedReader by putting InputStreamReader-// glue between the Reader and the FileInputStream.reader = newBufferedReader(newInputStreamReader(input));

// This variable counts the y rows.inty = 0;// This variable will hold every newly read lineStringline = 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 = newint[yList.size()][];for (inti = 0; i < ints.length; i++) {ints[i] = yList.get(i); }returnints; }

publicint[] parseString(Stringstr) {// We can create a int[], since we know// the length of the incoming string,// which is for example: "00192773716749".int[] array = newint[str.length()];

// We loop through each character in the string// and create an integer from the returned char// with Integer.parseInt(...);for (inti = 0; i < str.length(); i++) {charc = 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); }returnarray; }

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:

etc and then in java i can read each number, and load paint an image in accordance to the number, eg: 1 would be dirt, 2 would be grass etcthen i could use this to paint my map to the screen.however i do not know how to read each number and interpret there position to correspond to where i want them for example the first 1 in the first line would be painted at (0,0) and the first one on the second line would be painted at (0,32) etc

publicint[][] getIntsFromTextFile(Filefile) throwsIOException {// 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 = newArrayList<int[]>();// A bit try-finally stuff, so we can close the reader properly.BufferedReaderreader = null;try {// We create a BufferedReader by putting InputStreamReader-// glue between the Reader and the FileInputStream.reader = newBufferedReader(newInputStreamReader(newFileInputStream(file)));

// This variable counts the y rows.inty = 0;// This variable will hold every newly read lineStringline = 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 = newint[yList.size()][];for (inti = 0; i < ints.length; i++) {ints[i] = yList.get(i); }returnints;}

publicint[] parseString(Stringstr) {// We can create a int[], since we know// the length of the incoming string,// which is for example: "00192773716749".int[] array = newint[str.length()];

// We loop through each character in the string// and create an integer from the returned char// with Integer.parseInt(...);for (inti = 0; i < str.length(); i++) {charc = 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); }}

@matheus: so now, considering that we know how many rows there are file and we read each character one at a time, is there a way to interpret the row and "column" of each char? you probably already have it coded but I'm an amateur at java.@ SHC will that give both row and "column"?

@matheus: so now, considering that we know how many rows there are file and we read each character one at a time, is there a way to interpret the row and "column" of each char? you probably already have it coded but I'm an amateur at java.@ SHC will that give both row and "column"?

My code is not really the best, because it's actually like DOM and SAX for xml, but that's another story. Short: It reads all data and then the data is parsed, instead of parsing all the data right away.

Anyways.

You'd need to create tiles from the int[][] array you got. You basically got an array of numbers reaching from 0 to 9, describing what tiles to put.So you'd just iterate through the array and create the tiles as you want them (you could also just use the array as an array of tile ID's and don't use classes for tiles at all, but yeah. Thats another story as well...)

Thanks Matheus, i literally spent ages trying to work out how to use it so now i'll try this and if it works - then thank you so much, this has been my problem since i started game programming in java!

Are u using a "tile-editor"?If one line in the txt is one complete line of tiles, you could use .readline() ->after that u split it at the " " and parse it in the int[x (increasing)][y] -> next line int[x (increasing)][y+1]

If you one line isn't one complete line of tiles, you could just read the whole file ->after that u split it at the " " and parse it in the int

[y] until the width is reached.

-> next line int[x (increasing)][y+1]

Am I wrong or are you not able to create tiles with the id 10?

In addition you could write a tile-mapeditor on your own, it's not that hard.To save the map afterwards, I would serialize it, especially if you have entities on it, it makes it much easier.best regards

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org