my dog learned polymorphism*
The moose likes Game Development and the fly likes card game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "card game" Watch "card game" New topic
Author

card game

Alex McCormick
Ranch Hand

Joined: Mar 08, 2003
Posts: 31
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!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
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.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Alex McCormick
Ranch Hand

Joined: Mar 08, 2003
Posts: 31
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!
Saara Pakarinen
Greenhorn

Joined: Sep 05, 2004
Posts: 7
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: card game