0

Below I have my spritesheet class that I am working on where I want to have a spritesheet png split up into an array of ImageIcons for use in my other classes. My problem is that when I compile the below code I get a "(y + height) is outside of Raster" which appears to be stemming from my .getSubimage() call.

I know that the parameters of the call is

Parameters:

//x - the X coordinate of the upper-left corner of the specified rectangular region

//y - the Y coordinate of the upper-left corner of the specified rectangular region

//w - the width of the specified rectangular region

//h - the height of the specified rectangular region

And the way I am utilizing this spritesheet class in another class is as so...

ImageLoader loader = new ImageLoader();

BufferedImage anim  = loader.load("/images/spritesheet.png");

spritesheet spriter = new spritesheet(anim, 2, 10, 70, 70);

ImageIcon[] animArr = spriter.getSubs();

From my multiple checks it seems that everything is in line. My image size is 700x140 and each subImage is a 70x70 square. My j variable goes from 0-9 while my i variable goes from 0-1. And this should give me subImages from (0,0,70,70) all the way to (630,70,70,70)

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class spritesheet 
{

    private BufferedImage sheet;
    int rows, cols, wid, hei;

    //new spritesheet(anim, 2, 10, 70, 70);
    public spritesheet(BufferedImage sheet, int rows, int cols, int wid, int hei)
    {
        this.sheet = sheet;
        this.rows= rows;
        this.cols= cols;
        this.wid= wid;
        this.hei = hei;
    }

    public BufferedImage crop(int col, int row, int w, int h)
    {
        return sheet.getSubimage(col * wid, row * hei, w, h);
    }

    public ImageIcon[] getSubs()
    {
        ImageIcon[] arr = new ImageIcon[rows * cols];

        for (int i=0; i<rows; i++)
            for (int j=0; j< cols; i++)
            {
                arr[(10*i)+j] = new ImageIcon(crop(j,i,70,70));
            }

        return arr;

        //arr[(10*i)+j]
        //0,0 = 0
        //0,1 = 1
        //0,2 = 2
        //0,3 = 3
        //0,4 = 4
        //0,5 = 5
        //0,6 = 6
        //0,7 = 7
        //0,8 = 8
        //0,9 = 9

        //1,0 = 10
        //1,1 = 11
        //1,2 = 12
        //1,3 = 13
        //1,4 = 14
        //1,5 = 15
        //1,6 = 16
        //1,7 = 17
        //1,8 = 18
        //1,9 = 19






    }
}

If anyone could point out where I am going wrong that would be greatly appreciated. Thank you all for your time.

Guitardeon
  • 73
  • 1
  • 7

1 Answers1

0

You forgot the curly braces {} around the 2nd for loop

steven35
  • 3,747
  • 3
  • 34
  • 48
  • That wouldn't make a difference since the 2nd for loop as a whole in the instruction for the first for loop – Guitardeon Sep 01 '14 at 18:17