Hey everyone this is my first post and I must say that I like the community that I have seen here and look forward to contributing to other people’s success.
I’m making a 2D tile-based (small scale) rpg and cannot for the life of me figure out why my tilemap is not displaying. I just implemented loading maps from files and I know that before this the map was loading and displaying just fine I believe I have narrowed the possibilities down to my images being null when I go to draw them but I cannot seem to figure out why that is. Here is the code for my entire tilemap class with the null image
import java.util.*;
import java.awt.*;
import java.io.File;
import javax.swing.*;
public class TileMap{
ArrayList<Image> textures = new ArrayList<Image>();
int[][] map;
static int tileWidth = 32;
static int tileHeight = 32;
static int tileMapWidth;
static int tileMapHeight;
public TileMap(){}
public TileMap(int width, int height){
map = new int[height][width];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
map[y][x] = -1;
tileMapWidth = map[0].length;
tileMapHeight = map.length;
}
public TileMap(int[][] newMap){
this.map = newMap.clone();
tileMapWidth = map[0].length;
tileMapHeight = map.length;
}
public static TileMap LoadFromFile(String fileName){
TileMap tileMap;
ArrayList<ArrayList<Integer>> tempLayout = new ArrayList<ArrayList<Integer>>();
ArrayList<String> textureList = new ArrayList<String>();
Scanner input;
File file;
try{
file = new File(fileName);
input = new Scanner(file);
}catch(Exception e){
file = null;
System.out.println("File is null");
input = null;
System.out.println("Input is null");
}
boolean readingTextures = false;
boolean readingLayout = false;
while(input.hasNext()){
String line = input.nextLine().trim();
if (line.isEmpty())
continue;
if (line.contains("[Textures]")){
readingTextures = true;
readingLayout = false;
}
else if (line.contains("[Layout]")){
readingLayout = true;
readingTextures = false;
}
else if (readingTextures){
textureList.add(line);
}
else if (readingLayout){
ArrayList row = new ArrayList();
String[] cells = line.split(" ");
for (String c : cells){
if (!c.isEmpty())
row.add(Integer.parseInt(c));
}
tempLayout.add(row);
}
}
int width = tempLayout.get(1).size();
int height = tempLayout.size();
System.out.println(width);
System.out.println(height);
tileMap = new TileMap(width, height);
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
try{
tileMap.setCellIndex(x, y, tempLayout.get(y).get(x));
}catch(NullPointerException e){
System.out.println("SetCellIndex() has returned a NullPointerException");
}
}
}
String[] textureArray = new String[textureList.size()];
for (int i = 0; i < textureList.size(); i++){
textureArray[i] = textureList.get(i);
}
tileMap.LoadTextures(textureArray);
return tileMap;
}
public void LoadTextures(String ... fileNames){
for (int i = 0; i < fileNames.length; i++){
try{
textures.add(new ImageIcon(Game.SRC_PATH + fileNames[i]).getImage());
}catch(Exception e){
System.out.println("File not added.");
}
}
}
public int getCellIndex(int x, int y){
return map[y][x];
}
public void setCellIndex(int x, int y, int cellIndex){
map[y][x] = cellIndex;
}
public static int getWidthInPixels(){
return tileMapWidth * tileWidth;
}
public static int getHeightInPixels(){
return tileMapHeight * tileHeight;
}
public void Draw(Graphics2D g, Camera camera){
int tileMapWidth = map[0].length;
int tileMapHeight = map.length;
for (int x = 0; x < tileMapWidth; x++){
for (int y = 0; y < tileMapHeight; y++){
int index = map[y][x];
if (index == -1)
continue;
/////////////////////// NULL IMAGE RIGHT HERE \\\\\\\\\\\\\\\\\\\\\\\\\
Image image = textures.get(index);
try{
g.drawImage(image,
x * tileWidth - (int)camera.Position.X,
y * tileHeight - (int)camera.Position.Y,
Color.WHITE,
null);
}catch(NullPointerException n){
System.out.println("Draw Image threw a NullPointerException");
}
}
}
}
}
If anyone can help me spot the problem I would be very greatful. I thank you for taking the time to read over my code and here is a link to my entire project (zip file) with images and the tilemap text file.
https://docs.google.com/leaf?id=0B-7lPg3Z0pepZTU5NDNlNTAtOGNiMS00ZGExLWIxZjAtNWYxZGJlMjQyYTRj&hl=en&authkey=CIW-nLMK
Thanks everyone!
