2014-01-12 60 views
-5

我在java中有一個問題:我有一個方法在類中繪製圖像。 編輯:改變||到& &'s,修正了else,但是現在圖像是紅色的。 BTW:假定網格已被定義。圖像 - 索引出界異常

public BufferedImage scene() { 
    BufferedImage image = new BufferedImage(250, 250, BufferedImage.TYPE_INT_RGB); 
    if (ready) { 
     int tv=0; 
     int tc=0; 
     for (int x = 0; x < 250; x++) { 
      for (int y = 0; y < 250; y++) { 
       int px = (int) ((x + r)/16); 
       int py = (int) (y/16); 
       if (px >= 0 && py >= 0 && px < width && py < height) { 
        image.setRGB(x, y, grid[px][py].image.getRGB(
          (x + r) % 16, (y) % 16)); 
       } else { 
        image.setRGB(x, y, 0xFF0000); 
       } 
      } 
     } 
    } 
    return image; 
} 

但是當我運行的代碼我得到:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 19 
at general.Level.scene(Level.java:47) 
at general.Game.paintComponent(Game.java:34) 
at javax.swing.JComponent.paint(JComponent.java:1037) 
at javax.swing.JComponent._paintImmediately(JComponent.java:5106) 
at javax.swing.JComponent.paintImmediately(JComponent.java:4890) 
at javax.swing.RepaintManager$3.run(RepaintManager.java:814) 
at javax.swing.RepaintManager$3.run(RepaintManager.java:802) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:802) 
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:745) 
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:725) 
at javax.swing.RepaintManager.access$1000(RepaintManager.java:46) 
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1684) 
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:708) 
at java.awt.EventQueue.access$400(EventQueue.java:82) 
at java.awt.EventQueue$2.run(EventQueue.java:669) 
at java.awt.EventQueue$2.run(EventQueue.java:667) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:678) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException 
+0

你能告訴我們超出界限的例外嗎? –

+0

我相當肯定這個錯誤是由*你的*代碼產生的,在第47行的文件'Level.java'。 –

+0

把'||'修改爲'&&'修復了界外例外嗎?如果是這樣,現在在你的問題中有固定的代碼是非常令人困惑的。如果你有一個單獨的問題,那麼你需要創建一個新的問題。 –

回答

1

注:因爲我寫這個答案的問題已被編輯。

此檢查似有不妥:

if (px >= 0 || py >= 0 || px < width || py < height) 

你會想邏輯AND(&&),而不是OR(||),否則它可能是通過pxpy無效值又讓。這可能是什麼導致你的數組索引錯誤(儘管你沒有共享足夠的代碼來確定)。