下面我有我正在研究的spritesheet类,我希望将spritesheet png拆分为ImageIcons数组,以便在我的其他类中使用。我的问题是,当我编译下面的代码时,我得到一个“(y + height)在Raster之外”,这似乎源于我的.getSubimage()调用。
我知道调用的参数是
参数:
//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
我在另一个类中使用这个spritesheet类的方式就是这样......
ImageLoader loader = new ImageLoader();
BufferedImage anim = loader.load("/images/spritesheet.png");
spritesheet spriter = new spritesheet(anim, 2, 10, 70, 70);
ImageIcon[] animArr = spriter.getSubs();
从我的多次检查看来,一切都符合要求。我的图像尺寸为700x140,每个subImage为70x70正方形。我的j变量从0到9,而我的i变量从0到1变化。这应该给我(0,0,70,70)的子图像一直到(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
}
}
如果有人能指出我出错的地方,我将不胜感激。谢谢大家的时间。
答案 0 :(得分:0)
你忘记了围绕第二个for循环的花括号{}