1

I am trying to make a simple game in which you walk and pick up sticks. I track all graphical output and animations using a 2D pixel array. During the clear(), in which I set all pixel values to 0, it gives me an ArrayIndexOutOfBoundsException

Output:

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)

Main Code:

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

How would I go about fixing this Exception?

3 Answers 3

3

You have two types. First, in your for loop on line 104 the incrementer should be i2++ not i++. It should look like this:

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

You also have the same problem in your clear() method at the bottom of your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. I was copying and pasting the nested for loops and I guess I just made a silly mistake. I'll be watching out for that now.
0

You've got a typo in your inner for-loop. You should increment i2 instead of i.

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

Comments

0

You have made two mistakes in your program actually. because of doing just copy and paste the code

First One is private void clear() Method.

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

Second one is just after the render() method.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.