Okay so I am trying to draw a tiled map in Java. I am currently doing the following:
[]Loading mapData from a text file (level1.dat) which is then stored into a 2D Integer Array
[]Draw a BufferedImage dependant on the map data
[*]Draw the BufferedImage to my JFrame.
I have four classes:
[]ArrayToImage - Class whereby I convert the Array to the Image
[]MapToArray - Class whereby I convert the level1.dat into the 2D Array
[]Main - Class that instantiates the MainFrame
[]MainFrame - JFrame to display the Image
I think the problem is in MainFrame (Perhaps I am drawing the Image wrong?) or in ArrayToImage as the double for loop might not be working properly?
Here is the code I have worked on so far:
ArrayToImage
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
public class ArrayToImage {
Image image;
//ignore this part
private Image wall = null;
private Image table = null;
private Image floor = null;
int x,y,temp=0;
//nothing here yet
public void loadImages() {
wall = Toolkit.getDefaultToolkit().getImage("Tree.bmp");
table = Toolkit.getDefaultToolkit().getImage("Icicle.bmp");
floor = Toolkit.getDefaultToolkit().getImage("floor.bmp");
}
public ArrayToImage(){
loadImages();
}
//This function will return an BufferedImage with all the tiles drawn on it
public BufferedImage getMap(int[][] map){
loadImages();
BufferedImage bufferedImage = new BufferedImage(map.length*4, (map[0].length)*15, BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.green);
g2d.fillRect(0, 0, map.length*4, (map[0].length)*15);
for(int height= 0; height<map[0].length; height++){
for(int length = 0; length<map.length; length++) {
switch(map[length][height]){
case 0:
g2d.drawImage(floor, length*15, height*15, null);
break;
case 1:
g2d.drawImage(wall, length*15, height*15, null);
break;
case 2:
g2d.drawImage(table, length*15, height*15, null);
break;
}
}
}
g2d.finalize();
g2d.dispose();
return bufferedImage;
}
}
MapToArray
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
public class MapToArray {
private final String fileName;
public int length;
public int height;
//simple constructor, Sets the map
MapToArray(String fileName) {
this.fileName = fileName;
}
//returns the 2D array of the map in INT[] []
public int[] [] getArray() throws IOException {
ArrayList<String> array = new ArrayList<String>();
length = getLength();
height = getHeight();
LineNumberReader reader = new LineNumberReader (new FileReader(fileName));
for (int i = 0; i < height; i++) {
array.add(reader.readLine());
}
if (array.size() == 0) {
System.out.println("Empty map");
}
int[][] numbers = new int [length][height]; //reserve space in memory
int r= 0;
//now that we have the size of our array and the data, we fill it in
for (String s : array) {
toArray(s, r, numbers);
r++;
}
return numbers; //return map
}
//returns the map's Length
private int getLength() throws IOException {
int countChar = 0;
try {
LineNumberReader reader = new LineNumberReader (new FileReader(fileName));
String lineRead = reader.readLine();
int length = 0;
while (countChar < lineRead.length()) {
if (!(lineRead.charAt(countChar) == ',')) {
length++;
}
countChar++;
}
reader.close();
return length;
} catch (IOException e) {
throw e;
}
}
//returns height of map (number of lines)
public int getHeight() throws IOException {
int height = 0;
try {
LineNumberReader reader = new LineNumberReader(new FileReader(fileName));
String lineRead;
while ((lineRead = reader.readLine()) != null) {
}
//loops to last line of file}
height = reader.getLineNumber();
reader.close();
return height;
} catch (IOException e) {
throw e;
}
}
//This function will fill an entire row with the numbers from String s.
private void toArray(String s, int row, int[][] numbers) {
String match = ",";
String[] veld = s.split(match);
for(int i = 0; i<veld.length;i++)
veld[i] = veld[i].replaceAll(" ","");
for (int i = 0; i < veld.length; i++) {
try {
numbers[i][row] = Integer.parseInt(veld[i]);
} catch (NumberFormatException e) {
System.out.println("Not a valid map!");
}
}
}
}
MainFrame
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class MainFrame extends JFrame{
int[][] array_map1;
MapToArray map1;
Image bckground_1;
ArrayToImage draw = new ArrayToImage();
public void init() throws IOException {
map1 = new MapToArray("level1.dat"); //sets the current map
array_map1 = map1.getArray(); // grabs the map data and stores it in a 2D integer Array.
bckground_1 = draw.getMap(array_map1); //draws the "Map" by creating a BufferedImage dependant on the 2D Array.
}
MainFrame() throws IOException {
setSize(640,480);
setTitle("MapLoad Demo");
init();
setVisible(true);
}
public void paint (Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bckground_1, 0, 0 ,null);
}
}
Main
public class Main {
public static void main(String[] args) throws IOException {
new MainFrame();
}
}
The problem is when I run main.java ; A 15 * 15 Image loads in the top right (only 1 tile?) so I think It might not be looping properly in ArrayToImage.java. The problem can be seen in the screenshot below:
[spoiler]
http://s18.postimage.org/fk63ftzyt/map_Demo_Screen.jpg
[/spoiler]
I also think that all the Images are loading correctly as I painted one Image.
I am pretty sure that I am drawing the images incorrectly in MainFrame.java
Any help is appreciated, Thanks
v0rtex
P.S.: Here is the map Data for level1.dat
1,1,1,1,1,
1,0,0,0,1,
1,0,2,0,1,
1,0,0,0,1,
1,1,1,1,1,
Credit goes to elamre to whom provided most of the code through this tutorial.