java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 100

标签 java indexoutofboundsexception paintcomponent tiles

我们正在尝试创建一个带有图 block (10 x 10) 的游戏板。然而,当运行下面的类时,我们不断收到 ArrayIndexOutOfBoundsException。由于某种原因,当在另一个设备上运行相同的类时,不会给出此错误。

Board.java

public class Board extends JComponent {

    public Board() {

    }


    public static String[] gameElements = new String[100];


    String[][] Map = new String[10][10];
    int positionX = 50;
    int positionY = 50;
    int i = 0;
    String currentLevel = "1";

    @Override
    public void paintComponent(Graphics g) {
        loadLevel();
        for (int y = 0; y < Map.length; y++) {
            for (int x = 0; x < Map.length; x++) {
                new Tile(x, y).paintComponent(g);
                Map[y][x] = gameElements[i];
                g.drawString(Map[y][x], positionY, positionX);
                positionY = positionY + 50;
                System.out.print("[" + Map[y][x] + "]");
                i++;

            }
            positionY = 50;
            positionX = positionX + 50;
            System.out.println();

        }
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setSize(600, 600);
        frame.setTitle("SleutelBarricade");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent chart = new Board();
        frame.add(chart);

        frame.setVisible(true);


    }

    public void readTextFile(String fileName) {
        try {
            FileReader fileReader = new FileReader(fileName + ".txt");
            BufferedReader buffer = new BufferedReader(fileReader);
            String splitBy = ",";
            String line = buffer.readLine();

            for (int i = 0; i < gameElements.length; i++) {
                gameElements = line.split(splitBy);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadLevel() {
        readTextFile(currentLevel);

    }

}

部分异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 100
    at Main.GameBoard.Board.paintComponent(Board.java:45)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)

最佳答案

在双嵌套循环开始之前,您永远不会重置i。将变量移动到内部或将其重置为 0

@Override
public void paintComponent(Graphics g) {
    int i = 0;
    loadLevel();

    for (int y = 0; y < Map.length; y++) {
        for (int x = 0; x < Map.length; x++) {
            new Tile(x, y).paintComponent(g);
            Map[y][x] = gameElements[i];
            g.drawString(Map[y][x], positionY, positionX);
            positionY = positionY + 50;
            System.out.print("[" + Map[y][x] + "]");
            i++;

        }

        positionY = 50;
        positionX = positionX + 50;
        System.out.println();
    }
}
<小时/>

我还建议您重新组织您的类(class)以使其更易于阅读。您不必严格遵循这一点,但尝试将类似的事物分组。这将使事情更加突出,并帮助其他人更轻松地遵循您的计划。

有了这个简单的技巧,杂散的int i = 0(实例变量)就很容易被发现。

public class Board extends JComponent {
    // I. Static variables
    public static String[] gameElements = new String[100];

    // II. Instance variables (These should be private or protected)
    private String[][] map = new String[10][10];
    private int positionX = 50;
    private int positionY = 50;
    private int i = 0; // <------------------------- Hey, you don't belong here!
    private String currentLevel = "1";

    // III. Getter/Setters
    public String[] getMap() {
        return map;
    }
    public void setMap(String[] map) {
        this.map = map;
    }

    // IV. Constructor
    public Board() { }

    // V. Overrides
    @Override
    public void paintComponent(Graphics g) { }

    // VI. Custom Instance Methods
    public void readTextFile(String fileName) { }

    public void loadLevel() { }

    // VII. Main Method
    public static void main(String[] args) { }
}

关于java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36475253/

相关文章:

android - SimpleCursorAdapter 中的图像

Java游戏。子弹不会直接从 Guzzle 飞出

java - java swing中带有阴影和圆角边缘的图像

JavaFX 绑定(bind) : bind a dynamic list of boolean properties

java - 如何在 JEE Web 应用程序的 View 部分中以加密形式显示用户 ID?

java - Spring webflux : how to use custom jackson ObjectWriter in ResponseEntityResultHandler?

java - 在 jtext 区域中打印 unicode 字词时出现 ArrayIndexOutofBoundException。 (马拉雅拉姆语词)

java - (Android)如何捕获应用程序主线程终止(如果将其置于后台)

java - 线程 "main"中出现异常

java - KeyListener 无法与 PaintComponent 一起使用