| Author |
Cards
|
Lesole Mphinyane
Ranch Hand
Joined: Oct 07, 2006
Posts: 42
|
|
|
how do i model a card deck so that when a button "remove" is pressed, a card is removed from the top of the deck and when the button "return" is pressed, the removed card is retuned to the bottom of the deck and the card face being displayed on the GUI interface... Please help
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Sounds like you are looking for a CardLayout. (Also, this is not an advanced question, so I'll move it to a more appropriate forum)
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
how do i model a card deck so that when a button "remove" is pressed, a card is removed from the top of the deck and when the button "return" is pressed, the removed card is retuned to the bottom of the deck Keep your cards in either an array or a Collection such as ArrayList. Collections have helpful methods for things like shuffeling and adding/removing elements. One drawback is having to deal with generics (j2se 1.5+). With arrays you have to do more work. Let's say we have the cards stored in an array and that the deck has 52 cards. Then decide if the "top" of the deck is the last card in the array (index 51) or the first card(index 0). These are arbitrary design decisions that you get to make; you can always go back and change them if you decide on a better way. If the cards are face–down and the last card is at index 51 then to remove the top card might be Card topCard = cards[51]. To put this card on the bottom of this deck loop through the array, shift all cards in the range of index 0 to index 50 up one index [1 - 51]. Then assign topCard to index 0 in the array. Or you can use the System.arraycopy method to copy the cards into a new array at the "new indices", assign the topCard to index 0 and assign the cards array reference the value of the temp/copied array. By contrast, the ArrayList api has methods to remove and add objects at any index in the collection - easy. and the card face being displayed on the GUI interface Depends on how you want to put your gui together. You can draw the card images on a JPanel in the paintComponent method with drawImage(images[n], x, y, this) or you can use JLabels (set in a layout) to show the images, changing the images with label.setIcon(new ImageIcon(images[n])). edit: formatting [ March 26, 2007: Message edited by: Craig Wood ]
|
 |
 |
|
|
subject: Cards
|
|
|