• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using swing for board game.

 
Michael Emnon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.

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 !!
 
Tom Tillinghast
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Emnon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. thanks for replies.

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 !!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!