1

I have a Card and Deck class. The Deck class features and arraylist of Card objects. I need to draw a random card from the Deck and display in a GUI label. I am trying to create a path to the JPG cards in the "deck" asset folder and for some reason the GUI component does not like the suit and rank value. May be variable scope issue.... Here is what I have inside the event for my button: path.concat(d.drawRandomCard().tostring()).concat(ftype). I have this code working in my non-GUI test class. I am trying to make View work with my models in an OOP way.

showCard.java

    import javax.swing.*;

import java.awt.event.*;
import java.awt.Color;
import java.awt.Image;

import javax.swing.ImageIcon;


public class ShowCards implements ActionListener {
    final static String LABEL_TEXT = "Random Card!!";
    JFrame frame;
    JPanel contentPane;

    JButton drawCard;
    Card c;
    Deck d;
    JLabel dieFace;
    JLabel testing;

    int[] location = new int[2];
    Image image;

    String path = "deck/";
    String ftype = ".jpg";


    public ShowCards(){
        JButton drawCard;
        JLabel dieFace;
        JLabel testing;

            /* Create and set up the frame */
            frame = new JFrame("Roll");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            /* Create a content pane with a BoxLayout and
             empty borders */
            contentPane = new JPanel();
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            contentPane.setBackground(Color.white);
            contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));

            /* Create a label that shows a die face */
            dieFace = new JLabel(new ImageIcon(this.getClass().getResource("deck/h04.jpg")));
            dieFace.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            dieFace.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
            contentPane.add(dieFace);


            /* Create a label for testing */
            testing = new JLabel("Kiley");
            testing.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            testing.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
            contentPane.add(testing);

            /* Create a Roll Die button */
            drawCard = new JButton("Draw Card");
            drawCard.setAlignmentX(JButton.CENTER_ALIGNMENT);
            drawCard.addActionListener(this);
            contentPane.add(drawCard);

            /* Add content pane to frame */
            frame.setContentPane(contentPane);

            /* Size and then display the frame. */
            frame.pack();
            frame.setVisible(true);
        }


        /**
         * Handle a button click
         * pre: none
         * post: A die has been rolled. Matching image shown.
         */
        public void actionPerformed(ActionEvent event) {

            String drawncard = d.store.getCardSuit().toString()+d.store.getCardRank().toString();       


            //dieFace.setIcon(new ImageIcon(this.getClass().getResource("deck/d.drawRandomCard() + ".jpg")));
            //dieFace.setIcon(new ImageIcon(this.getClass().getResource("deck/s13.jpg")));
            //dieFace = new JLabel(new ImageIcon(this.getClass().getResource("deck/" + d.drawRandomCard().toString()+ ".jpg")));
            //dieFace = new JLabel(new ImageIcon(this.getClass().getResource(path.concat(d.store.getCardSuit().toString()).concat(d.store.getCardRank().toString()).concat(ftype))));
            dieFace = new JLabel(new ImageIcon(this.getClass().getResource(path.concat(drawncard).concat(ftype))));

            // parsing  dieFace = new JLabel(new ImageIcon(this.getClass().getResource(path.concat(String.valueOf(d.drawRandomCard().toString())).concat(ftype))));
            //dieFace = new JLabel(new ImageIcon(this.getClass().getResource("deck/s13.jpg")));
            dieFace.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            dieFace.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
            contentPane.add(dieFace);
            /* Add content pane to frame */
            frame.setContentPane(contentPane);

            /* Size and then display the frame. */
            frame.pack();
            frame.setVisible(true);



            /* int newRoll;

            newRoll = (int)(6 * Math.random() + 1);
            if (newRoll == 1) {
                dieFace.setIcon(new ImageIcon("die1.gif"));
            } else if (newRoll == 2) {
                dieFace.setIcon(new ImageIcon("die2.gif"));
            } else if (newRoll == 3) {
                dieFace.setIcon(new ImageIcon("die3.gif"));
            } else if (newRoll == 4) {
                dieFace.setIcon(new ImageIcon("die4.gif"));
            } else if (newRoll == 5) {
                dieFace.setIcon(new ImageIcon("die5.gif"));
            } else if (newRoll == 6) {
                dieFace.setIcon(new ImageIcon("die6.gif"));
            }

            */


        }


    public Image getImage(){
        ImageIcon ii = new ImageIcon(this.getClass().getResource("deck/h04.jpg"));
        image = ii.getImage();
        return image;
    }
    public void setLocation(int x, int y){
        location[0] = x;
        location[1] = y;
    }
    public int [] getLocation(){
        return location;
    }


    /**
     * Create and show the GUI.
     */
    private static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        ShowCards greeting = new ShowCards();

    }


    public static void main(String[] args) {
        /* Methods that create and show a GUI should be
           run from an event-dispatching thread */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });
    }
}

Deck.java

import java.util.Random;
import java.util.ArrayList;

public class Deck 
{

private ArrayList<Card> cards;
String mysuit,myrank;
Card store; // create an object variable to store card drawn

public Deck()
{
    cards = new ArrayList<Card>();


    for(int a =0; a<=3; a++)
    {
        for(int b =0; b<=12;b++)
        {
            cards.add(new Card(a,b));
        }
    }  
}

public Card drawRandomCard()
{
    Random generator = new Random();
    int index = generator.nextInt(cards.size());
    //return cards.remove(index);
    //return cards.get();
    store = cards.get(index);
    mysuit = store.getCardSuit().toString();
    myrank = store.getCardRank().toString();
    return cards.remove(index);




}




public String toString()
{
    String result = "Cards remaining in deck: " + cards;

        return result;

    }    
}

Card.java

 public class Card
{
    //private String suit;
    private int suit, rank; 
    //private int type, value;
    //private String[] cardSuit = {"Clubs", "Spades", "Diamonds", "Hearts"};
    //private String[] cardRank = {"Ace", "King", "Queen", "Jack", "10",
       //                            "9", "8", "7", "6", "5", "4", "3", "2"};

    private String[] cardSuit = {"c", "d", "h", "s"};
    private String[] cardRank = {"01", "02", "03", "04", "05",
                                   "06", "07", "08", "09", "10", "11", "12", "13"};




    //constructor called to fill in Deck object
    public Card(int suit, int rank)
    {
        this.suit = suit;
        this.rank = rank;
    }

    //access data
    public int getSuit()
    {
    return this.suit;
    }
    public int getRank()
    {
        return this.rank;
    }   

    // have to change Card class to get letter + number combos

    //access data
        public String getCardSuit()
        {
        return cardSuit[this.suit];
        }
        public String getCardRank()
        {
            return cardRank[this.rank];
        }   






    /* public String toString(){

        String cardinfo;
        cardinfo = this.suit + " card " + this.rank;
        return cardinfo;

    }  */

     public String toString()
        {
            //String finalCard = cardSuit[this.suit] + " of " + cardRank[this.rank];
         String finalCard = cardSuit[this.suit] + cardRank[this.rank];

            return finalCard;
        }



}// end class

I am trying to build a path to a random card. For example, the 4 of Hearts would be located at path: deck/h04.jpg. I need to unpack or parse the Card object so that I can concatenate : deck/ + h + 04 + .jpg. For some reason the "h04" piece is not working in the GUI component.

Here is the tester class that I used to test Card and Deck. You can see that the path to the resources is working in the non-GUI version:

TestClasses

public class TestClasses {



    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Card c = new Card(3,12);
        System.out.println(c.getRank());
        System.out.println(c.getSuit());
        System.out.println(c);
        Deck d = new Deck();
        System.out.println("Random card:" + d.drawRandomCard());
        System.out.println("Random card rank:"+ d.store.getCardRank());
        System.out.println("Random card suit:"+ d.store.getCardSuit());
        System.out.println("Random card:" + d.drawRandomCard());
        // we need to parse THIS instance of the random card to display it.
        // A new card is removed each time the method is fired!
        System.out.println("Random card:" + d);
        System.out.println("Card Suit:" + c.getCardSuit());
        System.out.println("Card Rank:" + c.getCardRank());
        System.out.println("Random card:" + d.drawRandomCard().toString());
        System.out.println("Random card:" + d.drawRandomCard().getSuit());
        System.out.println("Random card:" + d.drawRandomCard().getRank());
        System.out.println("Random card:" + d.drawRandomCard().getCardSuit()+"Hello");
        System.out.println("Random card:" + d.drawRandomCard().getCardRank()+"09");
        System.out.println("Random card rank:"+ d.store.getCardRank());
        System.out.println("Random card suit:"+ d.store.getCardSuit());

        System.out.println(d);

        String drawncard = d.store.getCardSuit().toString()+d.store.getCardRank().toString();  

        System.out.println("Card pulled from deck:" + drawncard);
        //System.out.println();


    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
HabsFan
  • 11
  • 1
  • 3
  • I suggest that you change your whole approach. I would create my ImageIcons all at the beginning of the program, iterating through all the card files, put them in a collection such as an array list or an array, and then use them as needed. Or better yet, associate an Icon with the appropriate and corresponding Card object. – Hovercraft Full Of Eels Aug 26 '14 at 00:52
  • 3
    Make `cardSuit` and `cardRank` `enum`, this way you can never be out of range... – MadProgrammer Aug 26 '14 at 01:11

0 Answers0