栅格外的getSubImage

时间:2013-08-29 19:38:05

标签: exception bufferedimage raster

我的图片宽度为512px。这段代码将抛出

RasterFormatException   (x+width) is outside Raster 

我不明白我做错了什么,当我检查光栅尺寸时它说的是512

private void automaticStaticSpriteLoader(String loadedName, String imgLoc, BufferedImage[] biArray, int numberOfSpritesToLoad, int numberOfSpritesInImage, int percentComplete){
    try {
        temporaryBigImg = ImageIO.read(getClass().getResource(imgLoc + ".png"));
    } catch (IOException e) {
        System.out.println(classNumber + " Error Loading Sprite Images. Shutting Down.");
        e.printStackTrace();
    }
    for(int i = 0; i<numberOfSpritesToLoad;i++){
        biArray[i] = temporaryBigImg.getSubimage(counterX, counterY, 32, 32);
        System.out.println("CounterX = " + counterX + " CounterY = " + counterY + " Current Index = " + i);
        if(counterX == 512){
            counterY += 32;
            counterX = -32;
        }
        counterX+=32;
    }
}

1 个答案:

答案 0 :(得分:1)

您要及时更新counterXcounterY

您必须先检查counterX >= 512并最终增加counterY并重置counterX ,然后再调用getSubImage

您的帖子中的代码将首先调用getSubImage(512, 0, 32, 32),然后测试是否counterX == 512(但是从未达到测试)。尝试打印传入的实际值,您将看到错误。