0

我有一个宽度为 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;
    }
}
4

1 回答 1

1

您正在更新counterX并且counterY为时已晚。

在调用之前,您必须检查是否counterX >= 512并最终增加counterY和重置。counterX getSubImage

代码,如您的帖子中一样,将首先调用getSubImage(512, 0, 32, 32),然后测试 if counterX == 512(但从未达到测试)。尝试打印你传入的实际值,你会发现哪里出了问题。

于 2013-08-29T19:53:25.013 回答