2D像素阵列上的java.lang.ArrayIndexOutOfBoundsException

时间:2016-04-09 04:05:42

标签: java arrays

我正在尝试制作一个简单的游戏,你可以走路并拿起棍棒。我使用2D像素阵列跟踪所有图形输出和动画。在clear()期间,我将所有像素值设置为0,它为我提供了ArrayIndexOutOfBoundsException

输出:

Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 225
at com.game.sticks.Game.render(Game.java:105)
at com.game.sticks.Game.run(Game.java:82)
at java.lang.Thread.run(Unknown Source)

主要代码:

package com.game.sticks;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import com.game.sticks.Graphics.Screen;
import com.game.sticks.Player.Player;

public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;

public static int width = 400;
public static int height = width / 16 * 9;
public static int scale = 3;
public static String title = "Pickn' Sticks";

boolean running = false;
JFrame frame = new JFrame();
Thread thread;

private Stick stick;
private Player player;

public int[][] pixels = new int[width][height];

public BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

public Game() {
}

Game(int i) {
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);

}

public synchronized void start() {

    thread = new Thread(this,"Display");
    stick = new Stick();
    player = new Player();

    running = true;

    thread.start();

}

public synchronized void stop() {
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    Game game = new Game(1);

    game.frame.setVisible(true);
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setLocationRelativeTo(null);
    game.frame.setResizable(false);
    game.frame.setTitle(title);

    game.start();
}

public void run() {
    setFocusable(true);
    while (running) {
        update();
        render();
    }
    stop();
}

public void update() {
    player.update();
    System.out.println("updating");

}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    clear();

    for(int i = 0;i<width;i++) {
        for(int i2 = 0;i2<height;i++) {
            pixels[i][i2] = -4325345;
        }
    }

    for(int x = 0;x < width;x++) {
        for(int y = 0;y < height;y++) {
            image.setRGB(x,y,pixels[x][y]);
        }
    }

    System.out.println("rendering");

    Graphics g = bs.getDrawGraphics();
    g = bs.getDrawGraphics();
    g.setColor(Color.green);
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
}

private void clear() {
    for(int i = 0; i<width; i++) {
        for(int i2 = 0; i2<height; i++) {
            pixels[i][i2] = 0;
        }
    }
}
}

我将如何修复此例外?

3 个答案:

答案 0 :(得分:3)

您有两种类型。首先,在第104行的for循环中,增量器应该是i2 ++而不是i ++。它应该是这样的:

for(int i = 0;i<width;i++) {
    for(int i2 = 0;i2<height;i2++) { //The change is from i++ to i2++
        pixels[i][i2] = -4325345;
    }
}

您的代码底部的clear()方法也存在同样的问题。

答案 1 :(得分:0)

你内心的for-loop中有一个拼写错误。你应该增加i2而不是i。

private void clear() {
    for(int i = 0; i<width; i++) {
        for(int i2 = 0; i2<height; i2++) {
            pixels[i][i2] = 0;
        }
    }
}

答案 2 :(得分:0)

你实际上在你的程序中犯了两个错误。因为只是复制并粘贴代码

  

第一个是private void clear()方法。

private void clear() {
    for(int i = 0;i<width;i++) {
        for(int i2 = 0;i2<height;i++) {
            pixels[i][i2] = 0;
        }
    }
}
  

第二个就在render()方法之后。

for(int i = 0;i<width;i++) {
        for(int i2 = 0;i2<height;i++) {
            pixels[i][i2] = -4325345;
        }
    }