我正在做一個關於吉他英雄克隆的項目。當我運行我的遊戲時(只是概念)發生錯誤:java.lang.IndexOutOfBoundsException
。我需要關於java.lang.IndexOutOfBoundsException的幫助 - ArrayList
package tapnstrum2;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.swing.*;
public class String1 extends JFrame implements ActionListener {
Image S1;
GNode G;
PlayIG P;
int m =0;
public String1(){
ImageIcon S1i = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01.png");
S1 = S1i.getImage();
}
public Image getImage(){
return S1;
}
public void keyPressed(KeyEvent e) throws FileNotFoundException, InterruptedException{
ArrayList GNodes = P.getGNodes();
GNode GN = (GNode) GNodes.get(m);
int key = e.getKeyCode();
ImageIcon S1iExact = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01_Exact.png");
ImageIcon S1iMissed = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01_Missed.png");
if (key == KeyEvent.VK_F1){
for (int i=0; i< GNodes.size(); i++){
if (GN.getY()+47>=380 && GN.getY()+47<=420)
S1 = S1iExact.getImage();
else
S1 = S1iMissed.getImage();
}
}
m++;
if (m>=GN.size())
m=0;
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
ImageIcon S1i = new ImageIcon("D:\\JavaProject\\Graphic-library\\buttons\\String01.png");
if (key == KeyEvent.VK_F1){
S1 = S1i.getImage();
}
}
@Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
}
我試圖讓圖像的邊界形成已經從一個簡單的文本文件中讀取一個ArrayList只包含0或1的每一行(1個drawImage,0是不是),並檢查邊界。 這個錯誤有索引可能等於我按了多少次F1。雖然我試圖在最後一行中加上m的條件。
更新我的錯誤信息:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at tapnstrum2.String1.keyPressed(String1.java:28)
at tapnstrum2.PlayIG$AL.keyPressed(PlayIG.java:98)
at java.awt.Component.processKeyEvent(Component.java:6483)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
你可以在這裏下載我的項目,如果有什麼有用 https://mega.co.nz/#F!ql80jDKY!fM91p7phB7LfhTs75rSK-Q
@tom沒錯。堅持嘗試/抓住。 – codehitman
@tom喜歡加入下限? – codehitman
@tom啊明白了!索引從0開始,對嗎?所以它應該是n-1。 – codehitman