Loading a sprite sheet

Alright so I’m having a little bit of trouble getting my sprite sheet loader to work.


SSL = new SpriteSheetLoader(32, 32, 5//rows, 1//columns);



package com.akrillix.client;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SpriteSheetLoader {
	BufferedImage spriteSheet = ImageIO.read(new File("src/spriteSheet.png"));	
	
	int width;
	int height;
	int rows;
	int columns;
	BufferedImage[] sprites = new BufferedImage[rows * columns];
	
	public SpriteSheetLoader(int width, int height, int rows, int columns) throws IOException {
		this.width = width;
		this.height = height;
		this.rows = rows;
		this.columns = columns;
		
		for(int i = 0; i < rows; i++) {
			for(int j = 0; j < columns; j++) {
				sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width, j * height, width, height);
			}
		}
	}
	public void paint(Graphics g) {
		//g.drawImage(sprites[1], 100, 100, null);
	}
}

The error.


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at com.akrillix.client.SpriteSheetLoader.<init>(SpriteSheetLoader.java:26)
	at com.akrillix.client.GameWorld.<init>(GameWorld.java:25)
	at com.akrillix.client.Launcher.main(Launcher.java:12)

This is my sprite sheet.

When do you load the image? A loader would probably be more effective if it did that, and it is probably the reason that it is not working.

Loading the sprite sheet.

BufferedImage spriteSheet = ImageIO.read(new File("src/spriteSheet.png"));  

What do you mean by a loader?

package com.akrillix.client;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SpriteSheetLoader {
   BufferedImage spriteSheet = ImageIO.read(new File("src/spriteSheet.png"));   
   
   int width;
   int height;
   int rows;
   int columns;
   BufferedImage[] sprites = new BufferedImage[rows * columns];
   
   public SpriteSheetLoader(int width, int height, int rows, int columns) throws IOException {
      this.width = width;
      this.height = height;
      this.rows = rows;
      this.columns = columns;
      
      for(int i = 0; i < rows; i++) {
         for(int j = 0; j < columns; j++) {
            sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width, j * height, width, height);
         }
      }
   }
   public void paint(Graphics g) {
      //g.drawImage(sprites[1], 100, 100, null);
   }
}

I think you should change:
BufferedImage[] sprites = new BufferedImage[rows * columns];
for:
BufferedImage[] sprites;

And then in the constructor add:
sprites = new BufferedImage[rows * columns];

Because when you say BufferedImage[] sprites = new BufferedImage[rows * columns];, rows and columns are 0. Then you change their values in the constructor, but sprite is already created with 0 for rown and colums, I think that’s the problem :slight_smile:

Thanks alex. That was it. What a silly 45 minute mistake =/.

Yey! I’m glad I help :slight_smile:

If you learn to use a debugger, it is only too easy to spot these kind of things yourself. Just set a breakpoint in the constructor on the line that breaks and investigate the state of the object; it should be pretty obvious when member variables are 0 when they really shouldn’t be :slight_smile:

I’m still doing my best at learning, thanks for all the help.