• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Minesweeper Help

 
Colby Snedeker
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my Java programming class we have been working on an individual project where we each make our own copy of minesweeper. I've gotten quite a bit done, but ran into a small problem. I don't know how to find the position of the button that they click in the grid layout.

Basically I have a field of JButtons attached to a JPanel in a grid layout. My problem is that I don't know how to find the (x, y) position of a JButton on the grid when it is clicked. I need these coordinates because they will then be fed to my 2D array of button objects that will actually determine what is done after the JButton is clicked. I've gone through the Actionlistener, GridLayout, and JButton entries on the API, but couldn't find anything to help me. Does anyone out there have an idea of how I could find the (x, y) position in the grid for the JButton that is clicked? Or should I try to do this in a different way?

Thanks in advance for any help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12340
38
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might get a better response in the Swing forum. I'll move it over there, and hopefully you'll get an answer there.
 
Campbell Ritchie
Marshal
Posts: 52510
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a LocatedButtonActionListener class (implements ActionListener).
Create a subclass of JButton which takes the column and row numbers in its constructor.
Give it a LocatedButtonActionListener as a field, and add that to the button as its Listener. This Listener passes its row number and column number to whatever when it is fired.
Set up a JPanel with a GridLayout.
As you iterate through your for-loops to add the buttons, pass i and j to their constructors.


Try that, see whether it works, and whether you can understand it
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Campbell Ritchie's approach will work, it may (or may not) be
more complex then you really need. Alternatives:

1) Instead of subclassing JButton to take col/row numbers in its constructor,
simply use stock JButton and call something like setActionCommand("3x4") on
each button. Your listeners can then call theActionEvent.getActionCommand().
The downside of this is that you have mess with all the String conversions.

2) You could add a different listener to each JButton. An approach I have
used in the past is to have the listener's constructor take col/row numbers.

3) You could cast theActionEvent.getSource() to Component and call its
getX() and getY() methods to get pixel positions within the Container.
I wouldn't recommend this approach, but I guess it's feasible.
 
Campbell Ritchie
Marshal
Posts: 52510
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer Brian's No 2 approach; passing x and y to the Listener's constructor is a nice solution.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heck what I did was to create a "smart" component by composition. It was basically a class that holds a JPanel, a JLabel (actually 2, one to display the mineNeighborCount, and one to display the mine ImageIcon), a JButton, and x and y int position and mineNeighborCount int variables. The JPanel holds the JButton and JLabel in a CardLayout initially displaying the Button. The JLabel holds (initially hidden) the mineNeightborCount. I tried to separate out the logic from the GUI using MVC and listeners. Sure it was a bit more complicated than it had to be, but it was a great lesson in MVC coding and was quite flexible.
 
Campbell Ritchie
Marshal
Posts: 52510
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the same thing in my programming class last year and here's how I solved it: I created custom buttons. Mine were totally custom because I wanted their graphics to be custom, but you could easily extend a JButton or any other kind of button. I gave each button gridX and gridY variables. Then, I used nested for loops to fill the array, at the same time it would set the values of the gridX and gridY variables. I added the same action listener to every button and when the event would fire, I would use the values of the gridX and gridY variables to get the handle of the button that was clicked, then do the necessary processing.

Hope this helps!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!