2013-08-29 75 views
0

我有一个宽度为512px的图像。这段代码会抛出getSubImage外部光栅

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

要更新counterXcounterY为时已晚。

你必须检查是否counterX >= 512,并最终增加counterY和重置你叫getSubImagecounterX之前。

代码,如在您的文章中将首先呼叫getSubImage(512, 0, 32, 32),然后测试是否counterX == 512(但测试从未达到)。尝试打印您传入的实际值,然后您会看到错误。

+0

谢谢!我没有意识到它是在512(这是图像的末尾) – Loligans

+0

我认为这非常烦人,在这种情况下异常消息不包含x和宽度的实际值,它将更容易调试.. – haraldK