I am making an old traditional board game played on a standard 64 squares square board. The game is not so well known (yet ! ;o)
but my question is about strategy for inputting moves. I have made japplet with 64 jbuttons in square. what is a good strategy for captuaring
square location ? I am thinking of making my own class which has a jbutton and two numbers describing row and column, but this seems inelegant
and not so object oriented. What is good way ?
THANKS !!
That's what I did for a chess game -- extended JButton to add my own data including row,col. It worked out fine.
You'll also be able to add in which piece if any is on that button/square, what color the square is, etc. if that's the way you want to go.
Hmmm. Or perhaps that should all be contained in a 'SquareData' class referenced by each Button.
What does the rest of our community have to say about this?
A class extending JButton is one possibility. I might go for a class extending JPanel that adds one JButton to its content pane. Both approaches are feasible, though.
I went ahead and made my own button. now, this button has different states and i give it an icon depending on its state (like fx this could be a rook icon if i was making chess game)
I have implemented a MouseListener and I listen for MouseClicked. Everything works ok except i get a "flickering" when buttons appearances are changed. this seems to be beacause
when just pressing and keeping the mouse button down, the default lightblue color of my button derived from JButton appears. I have overridden the mousePressed() method to do nothing, but this doesnt help. Any pointers ? Also a thin square appears around my icon when a button has just been pressed, i tried setFocusable(false) but no change. That swing library is huge !!
Can you post the part of your code where you are switching icons? Are you using setIcon() and setPressedIcon()?
Also check out JButton#setFocusPainted() instead of setFocusable()