Results 1 to 9 of 9
  1. #1
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default 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:
    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; }
    
    }
    hand:
    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 Card:
    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);
        }
    }
    Displayable Hand:
    Java 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);
        }
    }
    card game panel
    Java 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;
        }
    }
    main
    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've added 3 cards to the hand, none of which are actually being displayed.

    I can display a card using displayable card as shown in main.

    Thank you for any help
    Last edited by ralph; 12-13-2012 at 10:49 PM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Displaying a hand of cards

    Maybe because your filesystem has trouble finding "\\images\\classic-cards\\?????.png" ?
    I like likes!

  3. #3
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Displaying a hand of cards

    Quote Originally Posted by Sierra View Post
    Maybe because your filesystem has trouble finding "\\images\\classic-cards\\?????.png" ?
    I put '?????' as a place-holder as I'm not sure how to get the card passed in from main, and draw it in my hand class (one at a time until all cards in the hand have been drawn)

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

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

  5. #5
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Displaying a hand of cards

    Ok, maybe I'm going about asking this all wrong. Basically, in my Card class:

    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);
        }
    display the card: DisplayableCard(rank.Ace, suit.CLUBS);

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

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

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

  7. #7
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default 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
    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);
            }
        }
    Thank you for the response Sierra! :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

  9. #9
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Displaying a hand of cards

    Quote Originally Posted by Fubarable View Post
    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.
    I'm with you on that. It's part of an assignment so I had to have these classes inherit this way with specific implementation - sadly it was not my choice :P

Similar Threads

  1. Hi, any chance of a hand?
    By JohnAllenWIT in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 08:54 PM
  2. not looking for a hand out just a hand up
    By cloutier172 in forum New To Java
    Replies: 3
    Last Post: 10-04-2011, 03:38 AM
  3. Really Would use a hand
    By mollentze in forum New To Java
    Replies: 7
    Last Post: 07-03-2008, 02:30 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
  •