Results 1 to 2 of 2
  1. #1
    Natrix is offline Member
    Join Date
    Mar 2009
    Rep Power

    Default 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.

  2. #2
    neilcoffey is offline Senior 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.

Similar Threads

  1. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:53 PM
  2. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 06:37 PM
  3. need help creating my Tic Tac Toe game!
    By bobmasta5 in forum New To Java
    Replies: 6
    Last Post: 12-09-2008, 05:45 PM
  4. Looking for team members for creating the applet game.
    By M77 in forum Reviews / Advertising
    Replies: 10
    Last Post: 05-28-2008, 08:25 AM
  5. creating a text based game
    By Phobos0001 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts