This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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
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. 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 ]