2012-05-24 121 views
0

我試圖使用像上看到另一個問題,一些代碼回答:https://stackoverflow.com/a/621849/1044984光柵格式異常(JAVA)

在使用此我得到以下錯誤:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster 
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source) 
    at java.awt.image.BufferedImage.getSubimage(Unknown Source) 
    at main.Grid.paintComponent(Grid.java:111) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JLayeredPane.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
    at javax.swing.RepaintManager.paint(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
    at java.awt.Container.paint(Unknown Source) 
    at java.awt.Window.paint(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) 
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

下面是代碼與此錯誤:

try { 

      tileSheetBig = ImageIO.read(new File("sprites/tiles.png")); 
      charSheetBig = ImageIO.read(new File("sprites/player.png")); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     final int tileWidth = 64; 
     final int tileHeight = 64; 
     final int tileRows = 1; 
     final int tileCols = 11; 
     tileSheet = new BufferedImage[tileRows * tileCols]; 

     for (int i = 0; i < tileRows; i++) { 
      for (int j = 0; j < tileCols; j++) { 
       tileSheet[(i * tileCols) + j] = tileSheetBig.getSubimage(i 
         * tileWidth, j * tileHeight, tileWidth, tileHeight); 
      } 
     } 

     final int charWidth = 16; 
     final int charHeight = 23; 
     final int charRows = 2; 
     final int charCols = 3; 
     charSheet = new BufferedImage[charRows * charCols]; 

     for (int i = 0; i < charRows; i++) { 
      for (int j = 0; j < charCols; j++) { 
       charSheet[(i * charCols) + j] = charSheetBig.getSubimage(i 
         * charWidth, j * charHeight, charWidth, charHeight); 
      } 
     } 

由於沒有多少是從設在回答我看不到這個問題可能是什麼的代碼改變。我試圖谷歌的錯誤,但沒有很多答案與我的問題有關。

回答

1

RasterFormatExceptionthrown通過getSubImage()當由[X,Y:X +寬度,Y +高度]指定的區域不在BufferedImage區域內含有。

檢查您的tiles.png圖片至少是704x64像素(寬* cols,高*行),同樣,player.png至少爲48x46像素。

編輯: 對不起,我沒有注意到它乍一看; player.png必須是32x69 px和tiles.png 64x704 px

編輯2: 這會修復您的代碼而不編輯精靈;對瓷磚做同樣的工作

final int charWidth = 64; 
final int charHeight = 64; 
final int charCols = 11; 
final int charRows = 1; 
for (int i = 0; i < charCols; i++) { 
    for (int j = 0; j < charRows; j++) { 
     charSheet[i * charRows + j] = charSheetBig 
      .getSubimage(i * charWidth, j * charHeight, charWidth, charHeight); 
    } 
} 
+0

tiles.png是704乘64px。 player.png是48乘46像素。 – ComputerLocus

+0

請參閱我的編輯;你使用index * i *從0到一個名爲* charRows *的變量,但實際上你使用它來選擇x軸sprite(所以對於列);反之亦然* y *指數。這同樣適用於tiles.png –

+0

所以你說我的圖像需要是垂直的而不是水平的,而player.png甚至有不同的尺寸呢? – ComputerLocus