Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default Errors in the code (?)

    Hi everyone!
    I´m working on a snake-game. I´ve just started with it, so there is a lot of stuff to work with. But when the head eats an apple and it is about too get its new coordinates, 'sometimes' i got an error messages like this:

    "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 23
    at Board.random(Board.java:23)
    at Board.actionPerformed(Board.java:98)
    at javax.swing.Timer.fireActionPerformed(Timer.java:2 71)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    "

    Here is the code:

    Main-class:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GregerSnake extends JFrame{
    private Board field = new Board();
    private JButton[] button = new JButton[3];
    private String[] buttonText ={"New Game","Menu","Cancel"};
    private JPanel p, u;
    private JLabel o;
    
        public GregerSnake(){
            setSize(586, 585);
            setTitle("GregerSnake");
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(false);
            setLocationRelativeTo(null);
    
            p = new JPanel();
            u = new JPanel();
            p.setBackground(Color.lightGray);
            u.setBackground(Color.lightGray);
    
            o = new JLabel("GregerSnake");
            o.setFont(new Font("Arial", Font.BOLD, 40));
            u.add(o);
    
            MyListener myL = new MyListener();
    
            for(int i=0;i<button.length;i++){
                button[i] = new JButton();
                button[i].setText(buttonText[i]);
                button[i].addActionListener(myL);
                p.add(button[i]);
            }
    
            Container c = getContentPane();
            c.add(u, "North");
            c.add(field, "Center");
            c.add(p, "South");
    
            field.startGame();
        }
    
        class MyListener implements ActionListener{
            public void actionPerformed(ActionEvent ae){
                field.requestFocus();
                if(ae.getSource() == button[0]){
                    //field.NewGame();
                }
                else if(ae.getSource() == button[1]){
                    //field.Instruction();
                }
                else if(ae.getSource() == button[2]){
                    System.exit(0);
                }
            }
        }
    
        public static void main(String[] args) {
            GregerSnake GregerStart = new GregerSnake();
    	GregerStart.setVisible(true);
        }
    
    }
    and the field:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Board extends JPanel implements ActionListener{
    Timer time = new Timer(150, this);
    private int x, y;       //Ormens huvud
    private int xa, ya;     //Päron placering
    private int steg;       //Steglängden
    private int antal;      //Antal päron
    private int[] xR = new int[25];
    private int[] yR = new int[23];
    private static boolean u, h, v, n;
    
        public Board(){
            setBackground(Color.white);
        }
    
        public void random(){
            System.out.println("Antal ätna "+antal);
            xa = xR[(int)(Math.random() * 25 + 1)];
            System.out.print("X="+xa);
            ya = yR[(int)(Math.random() * 23 + 1)];
            System.out.println(" och Y="+ ya);
        }
    
        public void startGame(){
            time.start();
            x = 140;
            y = 400;
            steg = 20;
            u = false;
            h = false;
            v = false;
            n = false;
    
            for(int i=0; i<xR.length; i++){
                xR[i] = i*20;
            }
            for(int m=0; m<yR.length; m++){
                yR[m] = m*20;
            }
            random();
    
            addKeyListener(new KeyAdapter(){
                public void keyPressed(KeyEvent e){
                    if(e.getKeyCode() == KeyEvent.VK_UP){
                        if(n == false){
                           v = false;
                           h = false;
                           u = true;
                        }
                    }
                    else if(e.getKeyCode() == KeyEvent.VK_DOWN){
                        if(u == false){
                            v = false;
                            h = false;
                            n = true;
                        }
                    }
                    else if(e.getKeyCode() == KeyEvent.VK_LEFT){
                        if(h == false){
                            u = false;
                            n = false;
                            v = true;
                        }
                    }
                    else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                        if(v == false){
                            h = true;
                            u = false;
                            n = false;
                        }
    
                    }
                repaint();
                }
            });
        }
    
        public void actionPerformed(ActionEvent e){
            if(u == true && y-steg >= 0){
                y = y-steg;
            }
            else if(h == true && x+steg <= 560){
                x = x+steg;
            }
            else if(v == true && x-steg >= 0){
                x = x-steg;
            }
            else if(n == true && y+steg < 460){
                y = y+steg;
            }
    
            //Om den äter ett päron
            if(xa == x && ya == y){
                antal +=1;
                random();
            }
            repaint();
        }
    
    
      /*  public void paintComponent(Graphics g){
        g.fillRect(x, y, 20, 20);
    
    }*/
    
        @Override public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillRect(x, y, 20, 20);
    
            g.setColor(Color.green);
            g.fillRect(xa, ya, 20, 20);
    
    
            //Glömm dom nedan
            /*g.setColor(Color.black);
            for(int m=0; m<580; m+=40){
            for(int i=0; i<500; i+=40){
                    g.fillRect(m, i, 20, 20);
                }
            }*/
    
        }
    
    
    }
    Thanks :p

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    arrayIndexOutOfBounds usually means you have a loop going too far. When dealing with arrays remember that an array of size 24 goes from 0-23 so your loop only needs to goto 23. array.length-1 should fix this
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Replies: 7
    Last Post: 08-28-2009, 09:23 AM
  2. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  3. Replies: 3
    Last Post: 03-03-2009, 10:48 PM
  4. HELP ON errors occuring in the code
    By jaiminparikh in forum Advanced Java
    Replies: 6
    Last Post: 02-12-2009, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •