我在尝试加载大于4的地图时遇到错误

时间:2015-02-15 22:23:14

标签: java dictionary

错误:

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.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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,当我尝试将地图变得更大时,我得到了错误

这是我的块类:

public class Block extends Rectangle {

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;
}

}

如果有人能帮助我,我会非常感激,我非常擅长编程,但这个让我完全难过!

1 个答案:

答案 0 :(得分:0)

     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]));

        }

    }

了解如何使用块[height] [width]设置数组然后分配块[x] [y](其中x可预测地循环直到宽度和y高度)这是另一种方式周围。这就是你为什么要吹阵列索引的原因。

出于同样的原因,您的绘图功能看起来也是可疑的。