Results 1 to 4 of 4
  1. #1
    cleverjester is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9

    I thank you now for even reading my problem. I'm trying to recreate minesweeper with the same game play. I have run into a problem while writing it and can't figure out the answer no matter how much a search or look at my code. When I click on a mine that is not adjacent to any mine an error occurs when trying to click all of the adjacent squares. The whole error message is as following.

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
    at Mines$1.actionPerformed(Mines.java:155)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



    Here is the code.



    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Insets;
    import java.awt.FlowLayout;
    import java.util.Random;

    public class Mines extends JPanel implements ActionListener{

    static Random rgen = new Random();

    static private int mine[][] = new int[10][2];// first number is the number of the mine, second numbers are the number of the cordinates
    static private JButton square[][] = new JButton[9][9];
    public static int i = 0;
    public static int j = 0;



    Java Code:
    	Adjacent adjac = new Adjacent();
    	Adjacent2 adjac2 = new Adjacent2();
    	Adjacent3 adjac3 = new Adjacent3();
    	Adjacent4 adjac4 = new Adjacent4();
    	Adjacent5 adjac5 = new Adjacent5();
    	Adjacent6 adjac6 = new Adjacent6();
    	Adjacent7 adjac7 = new Adjacent7();
    	Adjacent8 adjac8 = new Adjacent8();
    	
    	public static void placeMines(){
    		for (int u = 0; u < 10; u++){
    			//create the mines
    			mine[u][0] = rgen.nextInt(8);
    			mine[u][1] = rgen.nextInt(8);
    		
    			// make sure no mines are in the same square
    			for (int b = 0; b < 10; b++){
    				while (b != u && mine[u][0] == mine[b][0] && mine[u][1] == mine[b][1]){
    					mine[u][0] = rgen.nextInt(9);
    					mine[u][1] = rgen.nextInt(9);	
    				}
    			}
    			
    		}
    	}
    	
    	public Mines(){
    		
    		placeMines();
    		
    		int h=0;
    		int u = 0;
    		Dimension rawr = new Dimension();
    		rawr.setSize(25, 25);
    		for (i = 0; i < 9; i++){
    			for (j = 0; j < 9; j++){
    				h=0;
    				
    				square[i][j] = new JButton("");
    				add(square[i][j]);
    				square[i][j].setPreferredSize(rawr);
    				square[i][j].setMargin(new Insets(0,0,0,0));
    				
    				for(u = 0; u < 10; u++){
    					if (mine[u][0] == i && mine [u][1] == j){
    						square[i][j].addActionListener(this);
    						h = 20;
    					}
    				}
    				if (h < 10){
    					h=0;
    					for(u = 0; u < 10; u++){
    						
    					//find how many mines a adjacent to square if any and if its not a mine
    						if (i-1 == mine[u][0] && j == mine[u][1]){
    							h += 1;
    						}
    						if (i-1 == mine[u][0] && j-1 == mine[u][1]){
    							h += 1;
    						}
    						if (i+1 == mine[u][0] && j == mine[u][1]){
    							h += 1;
    						}
    						if (i+1 == mine[u][0] && j+1 == mine[u][1]){
    							h += 1;
    						}
    						if (i+1 == mine[u][0] && j-1 == mine[u][1]){
    							h += 1;
    						}
    						if (i-1 == mine[u][0] && j+1 == mine[u][1]){
    							h += 1;
    						}
    						if (i == mine[u][0] && j+1 == mine[u][1]){
    							h += 1;
    						}
    						if (i == mine[u][0] && j-1 == mine[u][1]){
    							h += 1;
    						}
    					}
    						
    						// assign action listener based on number of mines
    					if (h==1 && h != 2 && h != 3){
    						square[i][j].addActionListener(adjac);
    					}
    					
    					if (h==2){
    						square[i][j].addActionListener(adjac2);
    					}
    						
    					if ( h == 3){
    						square[i][j].addActionListener(adjac3);
    					}
    					
    					if (h ==4){
    						square[i][j].addActionListener(adjac4);
    					}
    					
    					if (h == 5){
    						square[i][j].addActionListener(adjac5);
    					}
    					
    					if (h == 6){
    						square[i][j].addActionListener(adjac6);
    					}
    					
    					if (h == 7){
    						square[i][j].addActionListener(adjac7);
    					}
    					
    					if (h == 8){
    						square[i][j].addActionListener(adjac8);
    					}
    						// if no mines are adjacent, turn square black and open all of the adjacent squares.
    					if (h == 0){
    						square[i][j].addActionListener(new ActionListener(){
    							final public void actionPerformed(ActionEvent e){
    								JButton source = (JButton)e.getSource();
    								source.setBackground(Color.BLACK);
    									
    								if (i+1 < 10){
    									square[i+1][j].doClick();
    									if (j-1 >= 0){
    										square[i+1][j-1].doClick();
    									}
    									if (j+1 < 10){
    										square[i+1][j+1].doClick();
    									}
    								}
    								if (i != 0){
    									square[i][j].doClick();
    									if (j-1 >= 0){
    										square[i-1][j-1].doClick();
    									}
    									if (j+1 < 10){
    										square[i-1][j+1].doClick();
    									}
    								}
    								if (j-1 >= 0){
    									square[i][j-1].doClick();
    								}
    								if (j+1 < 10){
    									square[i][j+1].doClick();
    								}
    							}
    						});
    					}
    				}
    			}
    		}
    	}
    	public static void createGUI(){
    		// create the JFrame
    		JFrame frame = new JFrame("Minesweeper");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// Making and setting up the content pane
    		JComponent contentPane1 = new Mines();
    		contentPane1.setOpaque(true);
    		frame.setContentPane(contentPane1);
    		frame.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    		frame.pack();
    		frame.setSize(245, 265);
    		frame.setVisible(true);
    	}
    	// if a mine is on the square
    	public void actionPerformed(ActionEvent f){
    		JButton source = (JButton)f.getSource();
    		source.setBackground(Color.RED);
    		
    		
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    	
    		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				createGUI();
    			}
    		});
    	}
    }



    The part giving me trouple is this:




    Java Code:
    if (h == 0){
    						square[i][j].addActionListener(new ActionListener(){
    							final public void actionPerformed(ActionEvent e){
    								JButton source = (JButton)e.getSource();
    								source.setBackground(Color.BLACK);
    									
    								if (i+1 < 10){
    									square[i+1][j].doClick();
    									if (j-1 >= 0){
    										square[i+1][j-1].doClick();
    									}
    									if (j+1 < 10){
    										square[i+1][j+1].doClick();
    									}
    								}
    								if (i != 0){
    									square[i][j].doClick();
    									if (j-1 >= 0){
    										square[i-1][j-1].doClick();
    									}
    									if (j+1 < 10){
    										square[i-1][j+1].doClick();
    									}
    								}
    								if (j-1 >= 0){
    									square[i][j-1].doClick();
    								}
    								if (j+1 < 10){
    									square[i][j+1].doClick();
    								}
    							}
    						});
    					}




    If there is something I did wrong or something I can write in a different way, please tell me. Thank you again, even if you can't help!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by cleverjester View Post
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
    at Mines$1.actionPerformed(Mines.java:155)
    The exception stack trace tells is all: at line 155 in your actiionPerformed( ... ) method you are trying to access an array element number 9 while such element doesn't exist. Check your index values for your array(s) in that method; there's something wrong there.

    kind regards,

    Jos

  3. #3
    cleverjester is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Freezes

    Thank you very much, that solved that problem. But now I have two other problems, one is that because they are variables that are used in other places, my program only clicks the last square. Along with that, it also begins to click adjacent buttons endlessly. Is there a way I can but in a nonchanging variable in the action event or right outside of it so that the action event will clear the adjacent squares of the square clicked? Also is the only way to make a button not be able to be clicked after it has been cleared by making an array for all the buttons such as a boolean so and set it to true when the button is clicked?

  4. #4
    cleverjester is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 04-30-2010, 07:44 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 3
    Last Post: 06-07-2009, 12:14 AM
  4. Replies: 12
    Last Post: 05-26-2009, 01:48 PM
  5. Replies: 10
    Last Post: 01-29-2009, 08:00 AM

Posting Permissions

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