Forums Register Login

card game

+Pie Number of slices to send: Send
Hi all, I'm working on a Cribbage game (and have been for a while now!). I've got the majority of the game logic done, but what's really frustrating me is the GUI. I'm using Swing (probably not very well), and I'm not sure what the best way is for the user to click on cards - such as for discarding, playing a card during the "pegging" phase, etc.
Currently, I've got a very crude GUI that displays the players cards with a JRadioButton underneath each card. The user clicks the radiobuttons of the cards he/she wants to discard, then clicks a "discard" button. This works, but it looks a little amateurish. I've seen other card games where if you click the card, it actually moves. (some type of animation, I guess?) I'd even settle for the ability to just click the card and have the card "highlighted" in some fashion before clicking the discard button, rather than using radio buttons for the selection process.
Is there a class or classes in the Java library that I should take a peek at for this type of thing, or is there no easy way to do this?

I'm open to any suggestions!! thanks!
+Pie Number of slices to send: Send
Wait couldn't you use a JButton for the cards, and put the Image on the JButton, then when it is clicked it can move the card or highlight the card.

Mark
+Pie Number of slices to send: Send
First read about icons at http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html

The icon can be placed on the constructor of JLabel so you don't have to worry about the image loading etc. The JLabel is then placed on the gameboard that has some kind of layout (or you can do it yourself with custom painting).

Then your program (the gameboard or the main gameclass) must implement MouseMotionListener.

Check out the Java API http://java.sun.com/j2se/1.4.2/docs/api/
at class MouseMotionListener, there's a method MouseDragged. MouseMotionListener can listen to JLabels that are dragged on the gameboard.

The MouseMoved method can be used for "rollovers" => highlighting your cards when the mouse passes over them.

Hope this was of any help.
+Pie Number of slices to send: Send
Saara,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!
+Pie Number of slices to send: Send
Thanks for the help guys, I think I might try a combination of both ideas to see which I like best.
Currently, I'm displaying the cards as an ImageIcon within a JLabel. From what Saara said, I think I could use the MouseListener interface to capture when a user clicks on the JLabel, which could then run something like:
cardLabel1.setEnabled(false);

which would "grey out" the card - making it appear as though the user selected it to be discarded. If I can't get that to work well, though, then I may try the JButton approach and just put the ImageIcon inside the button.
I may have more questions after I try implementing these approaches, so you may hear back from me! Thanks again!
+Pie Number of slices to send: Send
 

Originally posted by Dirk Schreckmann:
Saara,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!



Changed. Thanks for the tip.
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1234 times.
Similar Threads
Graphics vs. JLabel??
image actions
My arrays are getting destroyed??!
problem w/ MouseListener and JLabel
implementation question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:42:48.