Results 1 to 9 of 9
Thread: Displaying a hand of cards
- 12-13-2012, 10:46 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
Displaying a hand of cards
I can currently display a card given it's suit and rank, and I have a hand class which I can add cards too, but am wondering how to go about displaying the hand?
Card:
hand:Java Code:enum suit { HEARTS, CLUBS, DIAMONDS, SPADES; } enum rank { Ace(1), Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(10), Queen(10), King(10); public final int Ranknum; rank (int nRank) { Ranknum = nRank; } int getRank() { return Ranknum; } } public class Card { protected rank Rank; protected suit Suit; public Card (rank theRank, suit theSuit) { Rank = theRank; Suit = theSuit; } public String toString ( ) { return ( Rank + " of " + Suit ); } public rank getRank( ) { return Rank; } public suit getSuit ( ) { return Suit; } }
Displayable Card:Java Code:public class Hand { private Card theCards[ ]; private int numCards; private static final int max = 5; public Hand ( ) { theCards = new Card [max]; numCards = 0; } public void addCard( Card aCard ) { if (numCards < max) theCards[numCards++] = aCard; } public int handValue() { int totalHandValue = 0; for (int i = 0; i < numCards; i++) { totalHandValue += theCards[i].getRank().Ranknum; } return totalHandValue; } @Override public String toString ( ) { String s = ""; for (int i = 0; i < numCards; ++i) { s += "\n" + theCards[i]; } return s; } }
Displayable Hand:Java Code:import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; public class DisplayableCard extends Card implements Displayable { private int cardNo; public DisplayableCard(rank theRank, suit theSuit) { super(theRank, theSuit); } @Override public void display(Graphics g, int x, int y) { Image image; if (Suit == suit.CLUBS) { cardNo = getRank().ordinal(); } else if (Suit == suit.SPADES) { cardNo = getRank().ordinal() + 13; } else if (Suit == suit.HEARTS) { cardNo = getRank().ordinal() + 26; } else { cardNo = getRank().ordinal() + 39; } String filename = "\\images\\classic-cards\\" + cardNo + ".png"; image = new ImageIcon(getClass().getResource(filename)).getImage(); g.drawImage(image, x, y, null); } }
card game panelJava Code:import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; public class DisplayableHand extends Hand implements Displayable { public DisplayableHand() { } @Override public void display(Graphics g, int x, int y) { Image image; String filename = "\\images\\classic-cards\\?????.png"; image = new ImageIcon(getClass().getResource(filename)).getImage(); g.drawImage(image, x, y, null); } }
mainJava Code:import javax.swing.*; import java.awt.*; public class CardGamePanel extends JPanel{ private Displayable theItem; private int x, y; public void paint(Graphics g) { if (theItem != null) theItem.display(g, x, y); } public void setItem(Displayable item, int x, int y) { theItem = item; this.x = x; this.y = x; } }
I've added 3 cards to the hand, none of which are actually being displayed.Java Code:import java.awt.*; import javax.swing.*; public class GUI extends JFrame { private CardGamePanel cg1 = new CardGamePanel(); private DisplayableHand dh = new DisplayableHand(); private DisplayableCard dc = new DisplayableCard(rank.Eight, suit.HEARTS); DisplayableCard dc1 = new DisplayableCard(rank.Ace, suit.CLUBS); DisplayableCard dc2 = new DisplayableCard(rank.Six, suit.CLUBS); DisplayableCard dc3 = new DisplayableCard(rank.Queen, suit.HEARTS); public GUI() { setTitle("Card Game GUI"); dh = new DisplayableHand(); //dc = new DisplayableCard(rank.Ace, suit.CLUBS); dh.addCard(dc1); dh.addCard(dc2); dh.addCard(dc3); cg1.setItem(dh,200,200); //cg1.setItem(dc,300,300); this.add(cg1); setSize(600, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); // This will paint the entire frame } public static void main(String[] args) { new GUI(); } }
I can display a card using displayable card as shown in main.
Thank you for any helpLast edited by ralph; 12-13-2012 at 10:49 PM.
- 12-13-2012, 11:46 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Displaying a hand of cards
Maybe because your filesystem has trouble finding "\\images\\classic-cards\\?????.png" ?
I like likes!.gif)
- 12-14-2012, 12:18 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
- 12-14-2012, 12:22 AM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Displaying a hand of cards
Well the only "display()" method executed is the one where you put a placeholder that the application cannot find, right?
So where is the mystery in not showing anything? ;)I like likes!.gif)
- 12-14-2012, 12:56 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
Re: Displaying a hand of cards
Ok, maybe I'm going about asking this all wrong. Basically, in my Card class:
display the card: DisplayableCard(rank.Ace, suit.CLUBS);Java Code:public void display(Graphics g, int x, int y) { Image image; if (Suit == suit.CLUBS) { cardNo = getRank().ordinal(); } else if (Suit == suit.SPADES) { cardNo = getRank().ordinal() + 13; } else if (Suit == suit.HEARTS) { cardNo = getRank().ordinal() + 26; } else { cardNo = getRank().ordinal() + 39; } String filename = "\\images\\classic-cards\\" + cardNo + ".png"; image = new ImageIcon(getClass().getResource(filename)).getImage(); g.drawImage(image, x, y, null); }
(Commented out due to that not being the issue, but it works if I uncomment)
Now that displays any card with the correct Rank and Suit passed in.
My Displayablehand class takes in 3 cards.
DisplayableCard dc1 = new DisplayableCard(rank.Ace, suit.CLUBS);
DisplayableCard dc2 = new DisplayableCard(rank.Six, suit.CLUBS);
DisplayableCard dc3 = new DisplayableCard(rank.Queen, suit.HEARTS);
dh.addCard(dc1);
dh.addCard(dc2);
dh.addCard(dc3);
However, my problem is that I'm not sure how to draw these 3 cards within the Displayablehand class, like I did with the display() method of card.
- 12-14-2012, 01:06 AM #6
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Displaying a hand of cards
IF your "Card" class would have a "display()" method you actually could call it for every card in your hand... sadly it does not.
So if you make it one and override it in DisplayableCard you could call it in a loop that loops through all your cards in your hand.I like likes!.gif)
- 12-14-2012, 01:16 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
Re: Displaying a hand of cards
doh! I just made the array of cards in hand protected so I could look through them and draw them as if they were separate cards, works perfectly :3
displayHand
Thank you for the response Sierra! :)Java Code:public void display(Graphics g, int x, int y) { for (int i = 0; i < numCards; ++i) { Image image; if (theCards[i].Suit == suit.CLUBS) { cardNo = theCards[i].getRank().ordinal(); } else if (theCards[i].Suit == suit.SPADES) { cardNo = theCards[i].getRank().ordinal() + 13; } else if (theCards[i].Suit == suit.HEARTS) { cardNo = theCards[i].getRank().ordinal() + 26; } else { cardNo = theCards[i].getRank().ordinal() + 39; } String filename = "\\images\\classic-cards\\" + cardNo + ".png"; image = new ImageIcon(getClass().getResource(filename)).getImage(); g.drawImage(image, x+(i*35), y, null); } }
-
Re: Displaying a hand of cards
If this were my application, I don't think that I'd use inheritance as much. I'd have DisplayableCard not inherit from Card but contain a Card object as its model, and the same for DisplayableHand.
- 12-14-2012, 02:07 AM #9
Member
- Join Date
- Nov 2012
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Hi, any chance of a hand?
By JohnAllenWIT in forum New To JavaReplies: 2Last Post: 11-10-2011, 08:54 PM -
not looking for a hand out just a hand up
By cloutier172 in forum New To JavaReplies: 3Last Post: 10-04-2011, 03:38 AM -
Really Would use a hand
By mollentze in forum New To JavaReplies: 7Last Post: 07-03-2008, 02:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks