d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Minesweeper Game
12Next
Add Reply New Topic New Poll
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 12:48pm
I reposted here because it's the general forum.

So I'm using Java to make a minesweeper game and I'm having some trouble with the grid.
I decided to make a 2D array of buttons.

I'm getting an outOfBoundsException with:
JButton [] [] grid = new JButton [rows] [columns];

rows and columns change depending on the difficulty.
But it doesn't matter if I choose easy, medium, or hard because the run-time error pops up after I choose what difficulty.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 12 2012 01:14pm
i believe arrays need a constant element number.
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 01:18pm
Quote (AbDuCt @ Jun 12 2012 03:14pm)
i believe arrays need a constant element number.


Well rows and columns never change. Say I choose easy, it's 10 rows and 10 columns.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jun 12 2012 01:41pm
What he means is that you need to initialize your 2d array with either integer numbers, or variables whose integer values have already been set.

Would also be useful if you actually posted some info about the error itself.

This post was edited by irimi on Jun 12 2012 01:41pm
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 01:54pm
Code
       JButton [] [] grid = new JButton [rows] [columns];        //creates 2D array of buttons
       JPanel gamePanel = new JPanel(new GridLayout(rows, columns));
       
       //GridLayout mineGrid = new GridLayout(rows, columns)
       
       
       //creates the grid of buttons
       for (int i = 0; i < rows - 1; i++)
       {
           for (int j = 0; j < columns - 1; j++)
           {
               grid [rows] [columns] = new JButton();
               grid [rows] [columns].addMouseListener(this);
               gamePanel.add(grid [rows] [columns]);
               
           }
       }
       newFrame.add(gamePanel);
       newFrame.pack();




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

line 16 being the one I singled out.

Also I'm trying to put the buttons in with GridLayout, but as you can see it's commented out because I'm not too sure about how to use it.

This post was edited by MachoMonkey89 on Jun 12 2012 02:03pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jun 12 2012 02:01pm
put your code in code brackets

Code
like this


also, look at your for loop. what do you think you're doing? what are you actually doing?
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 02:14pm
Quote (irimi @ Jun 12 2012 04:01pm)
also, look at your for loop.  what do you think you're doing?  what are you actually doing?


Well I think I'm instantiating a 2D array with buttons, allowing them to be clicked, then adding them each to the gamePanel, then add that to the frame.

And that's what I think I'm actually doing.

This post was edited by MachoMonkey89 on Jun 12 2012 02:14pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jun 12 2012 02:18pm
No, I'm not asking about what you're doing logically. I'm asking about what you're doing in the code.

Or put in another way:

Manually run the for loop either in your head or a sheet of paper. Keep track of the values of all variables (including temporary ones, like i and j in this example) as you go along. It should become incredibly obvious what your mistake very early on when you do this.

This post was edited by irimi on Jun 12 2012 02:19pm
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 02:22pm
Quote (irimi @ Jun 12 2012 04:18pm)
No, I'm not asking about what you're doing logically.  I'm asking about what you're doing in the code.

Or put in another way:

Manually run the for loop either in your head or a sheet of paper.  Keep track of the values of all variables (including temporary ones, like i and j in this example) as you go along.  It should become incredibly obvious what your mistake very early on when you do this.


lmfao, I'm not using my loop variables.
Thanks dude.
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Jun 12 2012 02:23pm
Quote (MachoMonkey89 @ Jun 12 2012 04:48pm)
I reposted here because it's the general forum.

So I'm using Java to make a minesweeper game and I'm having some trouble with the grid.
I decided to make a 2D array of buttons.

I'm getting an outOfBoundsException with:
JButton [] [] grid = new JButton [rows] [columns];

rows and columns change depending on the difficulty.
But it doesn't matter if I choose easy, medium, or hard because the run-time error pops up after I choose what difficulty.


Should post on Java forum, as you've akready said, this is a general forum...
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll