getSubimage() 方法中的越界异常
Out of bounds exception in getSubimage() method
我编写了一小段代码将 spritesheet 拆分成单独的图像...
private BufferedImage sheet, dirt, grass, rock, tree, water;
int width = 64, height = 64;
public void split() {
dirt = sheet.getSubimage(0,0,width,height);
grass = sheet.getSubimage(width,0,width*2,height);
rock = sheet.getSubimage(width*2,0,width*3,height);
tree = sheet.getSubimage(0,height,width,height*2);
water = sheet.getSubimage(width,height,width*2,height*2);
}
现在,前两个(泥土和草地)进展顺利。然而,问题在于岩石种植线。由于某种原因,它会抛出异常...
”
线程 "Thread-0" java.awt.image.RasterFormatException 中的异常:(x + width) 在 Raster
之外
在 sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1245)
"
很明显,问题与 x 值 "out of bounds" 有关。但是 x 值是 (width * 2),所以 128pix,正好在这张图片 (192x128) 的范围内,我附上它作为证据。
我还稍微更改了代码以裁剪岩石的 x 值为 1,但我仍然遇到问题,与对另一个缓冲图像使用相同的尺寸相同。
如有不妥,请见谅post,我是第一次。
提前致谢
回复我的评论。
所以您走在正确的道路上,但不太了解 getSubimage()
的工作原理。
文档说
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
您正确设置了 x
和 y
值,但是您错误地设置了 width
和 height
值。
由于您是从点 (x,y)
开始的,因此您不需要像现在这样补偿 width
和 height
,而是按原样使用它们。
所以,您的代码将是
public void split() {
dirt = sheet.getSubimage(0,0,width,height);
grass = sheet.getSubimage(width,0,width,height);
rock = sheet.getSubimage(width*2,0,width,height);
tree = sheet.getSubimage(0,height,width,height);
water = sheet.getSubimage(width,height,width,height);
}
我编写了一小段代码将 spritesheet 拆分成单独的图像...
private BufferedImage sheet, dirt, grass, rock, tree, water;
int width = 64, height = 64;
public void split() {
dirt = sheet.getSubimage(0,0,width,height);
grass = sheet.getSubimage(width,0,width*2,height);
rock = sheet.getSubimage(width*2,0,width*3,height);
tree = sheet.getSubimage(0,height,width,height*2);
water = sheet.getSubimage(width,height,width*2,height*2);
}
现在,前两个(泥土和草地)进展顺利。然而,问题在于岩石种植线。由于某种原因,它会抛出异常...
”
线程 "Thread-0" java.awt.image.RasterFormatException 中的异常:(x + width) 在 Raster
之外
在 sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1245)
"
很明显,问题与 x 值 "out of bounds" 有关。但是 x 值是 (width * 2),所以 128pix,正好在这张图片 (192x128) 的范围内,我附上它作为证据。
我还稍微更改了代码以裁剪岩石的 x 值为 1,但我仍然遇到问题,与对另一个缓冲图像使用相同的尺寸相同。
如有不妥,请见谅post,我是第一次。
提前致谢
回复我的评论。
所以您走在正确的道路上,但不太了解 getSubimage()
的工作原理。
文档说
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
您正确设置了 x
和 y
值,但是您错误地设置了 width
和 height
值。
由于您是从点 (x,y)
开始的,因此您不需要像现在这样补偿 width
和 height
,而是按原样使用它们。
所以,您的代码将是
public void split() {
dirt = sheet.getSubimage(0,0,width,height);
grass = sheet.getSubimage(width,0,width,height);
rock = sheet.getSubimage(width*2,0,width,height);
tree = sheet.getSubimage(0,height,width,height);
water = sheet.getSubimage(width,height,width,height);
}