0

I should begin by stating I am a relative beginner to both java and programming in general. I have been making a program loosely based off an old program I followed an online tutorial for, and for whatever reason, it searches the correct path but does not seem to be able to bring that image into a variable. code for the class is inserted below:

package tile;


import main.SimPanel;
import main.Utility;

import java.awt.Graphics2D;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;

import javax.imageio.ImageIO;

public class TileManager {

    SimPanel simPanel;
    public Tile[] tile;
    public int mapTileNum[][];
    public TileManager(SimPanel simPanel){
        
        this.simPanel = simPanel;
        
        tile = new Tile[20];
        mapTileNum = new int[simPanel.maxWorldColumn][simPanel.maxWorldRow];
        
        getTileImage();
        loadMap("res/map/worldmap.txt");
        
    }
    
    public void getTileImage() {
        
        setup(2, "floor", false);
        setup(1, "wall", true);
        setup(3, "start", false);
        setup(0, "end", true);
        
    }
    
    public void setup(int index, String imageName, boolean collision) {
        
        Utility utility = new Utility();
        try {
            tile[index] = new Tile();
            tile[index].image = ImageIO.read(getClass().getResourceAsStream("/backgroundTiles/"+ imageName +".png")); //example: /backgroundTiles/floor.png
            tile[index].image = utility.scaleImage(tile[index].image, simPanel.visibleTileSize, simPanel.visibleTileSize);
            //tile[index].collision = collision;
        } catch (IOException e){
            System.out.println("something went wrong");
            e.printStackTrace();
        }
        
    }
    
    public void loadMap(String path) {
        try {
            InputStream inputStream = getClass().getResourceAsStream(path);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            int column = 0;
            int row = 0;
            
            while (column < simPanel.maxWorldColumn && row < simPanel.maxWorldRow) {
                String line = bufferedReader.readLine();
                while (column < simPanel.maxWorldColumn) {
                    String numbers[] = line.split(" ");
                    int num1 = Integer.parseInt(numbers[column]);
                    mapTileNum[column][row] = num1;
                    column++;
                }
                if (column == simPanel.maxWorldColumn) {
                    column = 0;
                    row++;
                }
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
    
    public void draw(Graphics2D graphics2d) {
        int worldColumn = 0;
        int worldRow = 0; 
        
        while (worldColumn < simPanel.maxWorldColumn && worldRow < simPanel.maxWorldRow) {
            int tileNumber = mapTileNum[worldColumn][worldRow];
            int worldX = worldColumn*simPanel.visibleTileSize;
            int worldY = worldRow*simPanel.visibleTileSize;
            
            graphics2d.drawImage(tile[tileNumber].image, worldX, worldY, null);
            
            worldColumn++;
            if (worldColumn == simPanel.maxWorldColumn) {
                worldColumn = 0;
                worldRow++;
            }
            
        }
    }
}

once again I apologise for my incompetence and splurgy code but this is the best I can do. Any suggestions would be appreciated.

The issue occurs in the line tile[index].image = ImageIO.read(getClass().getResourceAsStream("/backgroundTiles/"+ imageName +".png"));

I have tried every single method for writing the filepath I could find. Tried changing the location, tried just about everything I can think of. for some reason it all throws the same issue.

I really hope I'm not just being dumb.

0 Answers0