尝试加载大于 4 的地图时出现错误

I am getting an Error when trying to load a map bigger than 4

错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4
    at stockman.mapping.Map.loadMap(Map.java:67)
    at stockman.mapping.Map.<init>(Map.java:23)
    at stockman.gamestate.Level1State.init(Level1State.java:21)
    at stockman.gamestate.GameState.<init>(GameState.java:18)
    at stockman.gamestate.Level1State.<init>(Level1State.java:15)
    at stockman.gamestate.MenuState.keyPressed(MenuState.java:83)
    at stockman.gamestate.GameStateManager.keyPressed(GameStateManager.java:25)
    at stockman.main.GamePanel.keyPressed(GamePanel.java:95)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
    at java.awt.Component.processKeyEvent(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我的地图代码:

public class Map {

    private String path;
    private int height, width;

    private Block[][] blocks;

    public Map(String loadPath)
    {

        path = loadPath;

        loadMap();

    }


    public void draw(Graphics g){

        for(int i = 0; i < blocks.length; i++){

            for(int j = 0; j < blocks[0].length; j++){

                blocks[j][i].draw(g);

            }
        }

    }

    public Block[][] getBlocks(){

        return blocks;

    }

    public void loadMap(){

        InputStream is = this.getClass().getResourceAsStream(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        try{

            width = Integer.parseInt(br.readLine());
            height = Integer.parseInt(br.readLine());

            blocks = new Block[height][width];

            for(int y = 0; y < height; y++){

            String line = br.readLine();

            String[] tokens = line.split("\s+");

                for(int x = 0; x < width; x++){

                    blocks[x][y]= new Block(x * Block.blockSize ,y * Block.blockSize, Integer.parseInt(tokens[x]));

                }

            }


        }catch(NumberFormatException | IOException e){

            e.printStackTrace();

        }

    }
}

我的map1.map代码:

4
4
1 1 1 1 
1 1 1 1 
1 1 1 1 
1 1 1 1 

我无法将地图设置为大于 4,当我尝试将地图设置得更大时,出现错误

这是我的街区Class:

public class 块扩展矩形 {

private static final long serialVersionUID = 1L;

public static final int blockSize = 64;
private int id;

public Block(int x, int y, int id) {
    setBounds(x, y, blockSize, blockSize);
    this.id = id;
}

public void tick() {

}

public void draw(Graphics g) {
    g.setColor(Color.BLACK);

    if(id != 0){

    g.fillRect(x - (int)GameState.xOffset, y - (int)GameState.yOffset, width, height);
    }
}

//getters and setters

public void setID(int id){

    this.id = id;

}

public int getID(){

    return id;
}

}

如果有人能帮助我,我将不胜感激,我很擅长编程,但是这个让我完全难住了!

     blocks = new Block[height][width];

    for(int y = 0; y < height; y++){

    String line = br.readLine();

    String[] tokens = line.split("\s+");

        for(int x = 0; x < width; x++){

            blocks[x][y]= new Block(x * Block.blockSize ,y * Block.blockSize, Integer.parseInt(tokens[x]));

        }

    }

看看你是如何用 blocks[height][width] 设置数组然后给它们分配 blocks[x][y](其中 x 可以预测地循环直到 width 和 y height)这是另一种方式大约。这就是你破坏数组索引的原因。

出于同样的原因,您的绘制函数看起来也很可疑。