• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

card game

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Alex McCormick
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic