Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Best way to do this

  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Lightbulb Best way to do this

    I'm making a card game and I'm not trying to find the best way to make the cards.
    I don't know whether to make them rectangles or make them JLabels or what. Also Mouse Listeners. Should each card have it's own mouse listener (does it even make sense to do so?) or what?
    Any suggestions?

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,673
    Rep Power
    5

    Default Re: Best way to do this

    There is no best way, find a way that works - that's good enough.

    I created a memory game once using Swing and I ripped the cards from Windows Patience and simply stored them as images. Then I slapped those images onto a grid of buttons so I could just use the workings of the buttons to be able to easily flip over cards; button listener which exchanges the image icon of the button.

    The key here is that the type of game allowed that to work. I have no idea if for you card game it would work too. I hope now you see how important it is to properly describe what you want to achieve. "A card game" is very non-specific.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Best way to do this

    When I created a small game in java, i didn't use any controls. Instead i drawed everything to an image and then displayed the image.

    I evaluated keystrokes and mouse events inside the window and took care of everything myself.

    That was some more code to write but I had no problems to change the behaviour of controls that I didn't like.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,338
    Blog Entries
    7
    Rep Power
    20

    Default Re: Best way to do this

    I once did it this way: I created 52 JPanels, each displaying a single card face; I could put those JPanels on a CardLayout in another JPanel (the 'table'); I could easily put cards (partly) on top of other cards, move them around etc. by manipulating the JPanels in the CardLayout.

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Best way to do this

    This definitely calls for some prototyping. Consider what you want to do with the cards. Imagine three cards partially overlapping in a column. Then you want to grab all the cards from one card down to the bottom. Or you want to move them back and forth to another column. I would suggest you try several approaches with images, panels or components, or just drawing them in a graphics context. Then see the difficulties associated with each technique. This might also be a good time to set up a card model so you can perhaps switch different methods of doing the cards without affecting the rest of your code.

    Of course, someone may think this is BORING!! :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,673
    Rep Power
    5

    Default Re: Best way to do this

    Quote Originally Posted by jim829 View Post
    Of course, someone may think this is BORING!! :)
    You're a good chap though.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Best way to do this

    OK. I have had my fun.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Posting Permissions

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