0
\$\begingroup\$

I'm trying to make a map using a text file, but it only does the first line / column and not the rest.

I know the problem is in getTileImage.

public class TileManger {
    GamePanel gp;
    Tile [] tile;
    int mapTileNum [] [];
    
    public TileManger (GamePanel gp) {
        
        this.gp = gp;
        
        tile = new Tile [10];
        mapTileNum = new int [gp.maxScreencol] [gp.maxScreenrow];
        getTileImage ();
        loadMap();
    };
    
    public void getTileImage() {
        
        try {
            tile[0] = new Tile();
            tile[0] .image = ImageIO.read( getClass ().getResource("/Tiles/grass.png") );
            
            tile[1] = new Tile();
            tile[1] .image = ImageIO.read( getClass ().getResource("/Tiles/wall.png") );
            
            tile[2] = new Tile();
            tile[2] .image = ImageIO.read( getClass ().getResource("/Tiles/water.png") );
            
        }catch (IOException e) {
            e.printStackTrace();
            
        }
    }
        
        public void loadMap () {
            
            try {
                
                InputStream is = getClass ().getResourceAsStream("/maps/Map_02.txt");
                BufferedReader br = new BufferedReader (new InputStreamReader(is));
                
                int col = 0;
                int row = 0;
                
                while(col < gp.maxScreencol && row < gp.maxScreenrow) {
                    
                    String line = br.readLine();{
                    
                        
                        String numbers[] = line.split(" ");
                        
                        int num = Integer.parseInt(numbers[col]);
                        
                        mapTileNum[col] [row] = num;
                        col++;
                    }
                    
                    if(col == gp.maxScreencol) {
                        col = 0;
                        row++;
                    }
                }
                br.close();
                
            }catch (Exception e) {
                
            }
        }
        
        
    public void draw (Graphics2D g2) {
        int col = 0;
        int row = 0;
        int x = 0;
        int y = 0;
        
        while (col < gp.maxScreencol && row < gp.maxScreenrow) {
            
            int tileNum = mapTileNum [col] [row];
            
            
            g2.drawImage(tile[tileNum].image, x, y, gp.tilesize, gp.tilesize, null);
            col++;
            x += gp.tilesize;
            
            if (col == gp.maxScreencol) {
                col = 0;
                x = 0;
                row++;
                y +=gp.tilesize;
                
            }
            
        }
    }

}

Map_02.txt

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 2 2 2 2 2 2 0 0 0 0 1
1 0 0 0 0 2 2 2 2 2 2 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
\$\endgroup\$
3
  • 1
    \$\begingroup\$ What specific evidence tells you the problem is in getTileImage? Do you get an exception when you run it? If so, be sure to edit your question to include the full text of the exception, or whatever other evidence you've collected in your debugging so far. \$\endgroup\$
    – DMGregory
    Aug 6 at 17:42
  • 1
    \$\begingroup\$ There is a way easier way to do this. Instead of having a while that can be error prone (what if your text file is smaller/ bigger than your maxScrollcol?) just check if the br has a next line. Read your file line by line, use the split of the line in another for loop to assign the value to each column in that row. As it is right now, you have an empty {} in your code with no condition loop around and some broken logic \$\endgroup\$
    – Zibelas
    Aug 7 at 16:34
  • \$\begingroup\$ Hey i found that i Forgot to Put "While (col > gp.maxScreenCol)" after "String line = br.readLine();", but still thx for Helping \$\endgroup\$ Aug 7 at 20:01

0

You must log in to answer this question.

Browse other questions tagged .