Results 1 to 2 of 2
Thread: Creating a Card Game in Java
- 05-05-2009, 05:35 PM #1Member
- Join Date
- Mar 2009
- Rep Power
Creating a Card Game in Java
I am thinking of creating a simple card game. It would look like Solitaire on windows.
The problem is that I don't know where to start.
I don't have any specific questions yet but I am sure I will have tons when I get started.
The few not so specific questions are:
How to draw a card?
Should a card be an empty object with a loaded Image?
How to load an image on the empty object?
How to set the position in the frame where the card is drawn?
How to stack object on another?
How to make it move and drop?
All I need is a bit of directional help. If you could just give me the link to the tutorial that covers any of this questions I would be glad to go through them.
Thanks in advance.
- 05-05-2009, 05:55 PM #2Senior Member
- Join Date
- Nov 2008
- Rep Power
I'd start by worrying about your data structures, then when you've got that, make the GUI work around it.
You're going to need classes to represent things that you have in a solitaire game: a Card, a Pile (which contains a list of Cards), and a Game, which consists of lots of Piles. I'd suggest each Pile has a position, but then the position of each card is just calculated based on the Pile's position and the position of the card within the "list" of cards.
Personally, I'd keep the GUI and the data fairly separate. So you'll have a class, say, SolitaireComponent, which will extend JComponent and do the actual drawing/GUI handling for a particular Game. Its paintComponent() method will cycle through the Piles, and paint them (possibly then cycling through the individual Cards in that pile). For actually drawing, look at the methods on Graphics (which is passed to the paintComponent() method): you'll see things for drawing strings, images etc at particular positions.
For implementing move/drop, make your SolitaireComponent implement a MouseListener and MouseMotionListener. When the player clicks and you get a mousePressed event, you'll have some method that "finds the card given a particular x and y mouse position", then removes that card from its pile and makes it the "currently dragging card". You'll change its position as you get mouseDragged() events until you get a mouseReleased event, at which point you'll see if the currently dragging card can be placed on whatever pile is under coordinate where the player releases the mouse...
To do all this, you need to start researching in particular Java Swing for the UI and Java 2D for the drawing stuff.Neil Coffey
Javamex - Java tutorials and performance info
- By javafox in forum New To JavaReplies: 4Last Post: 03-14-2009, 03:53 PM
- By GonzaloP in forum NetworkingReplies: 0Last Post: 12-28-2008, 06:37 PM
- By bobmasta5 in forum New To JavaReplies: 6Last Post: 12-09-2008, 05:45 PM
- By M77 in forum Reviews / AdvertisingReplies: 10Last Post: 05-28-2008, 08:25 AM
- By Phobos0001 in forum New To JavaReplies: 1Last Post: 02-12-2008, 04:35 PM