Here is my code:
package com.Main.tile;
import com.Main.GamePanel;
import com.Main.UtilityTool;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TileManager {
GamePanel gp;
public Tile[] tile;
public int mapTileNum[] [];
public TileManager(GamePanel gp){
this.gp = gp;
tile = new Tile[10];
mapTileNum = new int [gp.maxWorldCol] [gp.maxWorldRow];
getTileImage();
loadMap("/maps/world01.txt");
}
public void getTileImage(){
setup(0, "grass", false);
setup(0, "wall", true);
setup(0, "water01", true);
setup(0, "earth", false);
setup(0, "tree", true);
setup(0, "sand", false);
}
public void setup(int index, String imageName, boolean collision){
UtilityTool uTool = new UtilityTool();
try {
tile[index] = new Tile();
tile[index].image = ImageIO.read(getClass().getResourceAsStream("/tiles/"+imageName+".png"));
tile[index].image = uTool.scaleImage(tile[index].image, gp.tileSize, gp.tileSize);
tile[index].collision = collision;
} catch (IOException e){
e.printStackTrace();
}
}
public void loadMap(String filePath){
try{
InputStream is = getClass().getResourceAsStream(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int col = 0;
int row = 0;
while(col < gp.maxWorldCol && row < gp.maxWorldRow){
String line = br.readLine();
while(col < gp.maxWorldCol){
String numbers[] = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[col] [row] = num;
col++;
}
if(col == gp.maxWorldCol){
col = 0;
row++;
}
}
br.close();
}catch(Exception e){
}
}
public void draw(Graphics2D g2){
int worldCol = 0;
int worldRow = 0;
while(worldCol < gp.maxWorldCol && worldRow < gp.maxWorldRow){
int tileNum = mapTileNum[worldCol] [worldRow];
int worldX = worldCol * gp.tileSize;
int worldY = worldRow * gp.tileSize;
double screenX = worldX - gp.player.worldX + gp.player.screenX;
double screenY = worldY - gp.player.worldY + gp.player.screenY;
if(worldX + gp.tileSize > gp.player.worldX - gp.player.screenX &&
worldX - gp.tileSize < gp.player.worldX + gp.player.screenX &&
worldY + gp.tileSize > gp.player.worldY - gp.player.screenY &&
worldY - gp.tileSize < gp.player.worldY + gp.player.screenY) {
g2.drawImage(tile[tileNum].image, (int)screenX, (int)screenY, null);
}
worldCol++;
//x += gp.tileSize; DISABLE FOR STABILITY
if(worldCol == gp.maxWorldCol){
worldCol = 0;
//x = 0;
worldRow++;
//y += gp.tileSize;
}
}
}
}
and it always shows the error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)
at com.Main.tile.TileManager.setup(TileManager.java:44)
at com.Main.tile.TileManager.getTileImage(TileManager.java:29)
at com.Main.tile.TileManager.<init>(TileManager.java:24)
at com.Main.GamePanel.<init>(GamePanel.java:30)
at com.Main.Main.main(Main.java:13)