Java ArrayIndexOutOfBounds game

advertisements

How do I fix this error? I am trying to import a "sub- sprite sheet" into my game but am getting an arrayIndexOutOFBounds error

Source code and errors:

I'm trying to implement a 'sub-spritesheet" and I have no idea why i'm getting this out of bounds error. Here are the screens of the class where all errors occur.

package com.apcompsci.game.graphics;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
private String path;
public final int SIZE;
public final int WIDTH,HEIGHT;
public int [] pixels;

public static SpriteSheet tiles = new SpriteSheet("/textures/sheets/spritesheet.png",256);
public static SpriteSheet spawn_level = new SpriteSheet("/textures/sheets/spwan_level.png",48);
public static SpriteSheet projectile_DemiGod = new SpriteSheet("/textures/sheets/projectiles/DemiGod.png",48);
public static SpriteSheet player = new SpriteSheet("/textures/sheets/player_sheet.png",95,129);
public static SpriteSheet player_down = new SpriteSheet(player, 0, 0, 1,3,32);

public SpriteSheet(SpriteSheet sheet, int x, int y, int width, int height, int spriteSize)
{
    int xx =  x*spriteSize;
    int yy = y*spriteSize;
    int w = width*spriteSize;
    int h = height *spriteSize;
    if(width == height) SIZE = width;
    else SIZE = -1;
    WIDTH = w;
    HEIGHT = h;
    pixels = new int[w*h];
    for(int y0 = 0; y0<h; y0++)
    {
        int yp = yy + y0;
        for(int x0 = 0; x0<w; x0++)
        {
            int xp = xx + x0;
            pixels[(x0+y0)*w] = sheet.pixels[(xp+yp)*sheet.WIDTH];
        }
    }

}

public SpriteSheet(String path, int size)
{
    this.path = path;
    SIZE = size;
    pixels = new int[SIZE*SIZE];
    WIDTH = size;
    HEIGHT = size;
    load();
}

public SpriteSheet(String path, int width, int height)
{
    this.path = path;
    SIZE = -1;
    WIDTH = width;
    HEIGHT = height;
    pixels = new int[SIZE*SIZE];
    load();
}

private void load()
{
    try {
        BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
        int w = image.getWidth();
        int h = image.getHeight();
        image.getRGB(0,0,w,h,pixels,0,w);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.apcompsci.game.graphics.Sprite.<clinit>(Sprite.java:11)
    at com.apcompsci.game.entity.mob.spider.<init>(spider.java:11)
    at com.apcompsci.game.level.SpawnLevel.loadLevel(SpawnLevel.java:36)
    at com.apcompsci.game.level.Level.<init>(Level.java:31)
    at com.apcompsci.game.level.SpawnLevel.<init>(SpawnLevel.java:16)
    at com.apcompsci.game.level.Level.<clinit>(Level.java:19)
    at com.apcompsci.game.Game.<init>(Game.java:49)
    at com.apcompsci.game.Game.main(Game.java:169)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at java.awt.image.BufferedImage.getRGB(Unknown Source)
    at com.apcompsci.game.graphics.SpriteSheet.load(SpriteSheet.java:69)
    at com.apcompsci.game.graphics.SpriteSheet.<init>(SpriteSheet.java:60)
    at com.apcompsci.game.graphics.SpriteSheet.<clinit>(SpriteSheet.java:17)
    ... 8 more


my guess is that the following line in BufferedImage is failing

rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x,
                                y,
                                data));

rgbArray is the array passed in to the function. It needs to be quite big but the function will initialise it for you if you pass in null.

Try replacing

image.getRGB(0,0,w,h,pixels,0,w);

with

pixels = image.getRGB(0,0,w,h,null,0,w);