0
votes

Mac OSX Netbeans JAVA

Goal: BlackJack program... Im trying to get an imageIcon of a Playing Card to display in a JLabel

Logic: I have created some CARD objects with a method to return the imageIcon associated with it. In my main GUI class it works if i create the new imageIcon specifying the file location -

    private void newGame(){

    String temp1, temp2, temp3, temp4;

    card1 = hand.dealHand();
    card2 = hand.dealHand();
    card3 = hand.dealHand();
    card4 = hand.dealHand();

    image1 = new ImageIcon();
    image1 = card1.getImage();


    //Creates DeckImage and Logo as JLabel and adds it to userPanel

    //image1 = new ImageIcon("/Users/philhunter/NetBeansProjects/PractingProgramming/src/Resources/1.png");


    card1Label = new JLabel(image1, JLabel.LEFT);
    userPanel.add(card1Label);

    card1Label.setText("");

}

The commented out line works fine and displays the imageIcon image but when I use the card1.getImage() method then the image does not display. The method is simply -

public ImageIcon getImage(){
    return this.image;
}

Also, in case you need it, here is the method that creates the CARD's from the DECK class -

private ImageIcon C1,C2, ... ,C52;
private ImageIcon[] imageArray= { C1,C2,...,C52 };
C1 = new ImageIcon("/Users/philhunter/NetBeansProjects/PractingProgramming/src/Resources/1.png");
...
C52 = new ImageIcon("/Users/philhunter/NetBeansProjects/PractingProgramming/src/Resources/52.png");

int SUITS = suit.length;
    int RANKS = rank.length;
    int N = SUITS * RANKS;

    //Creates a deck of 52 CARD objects
    theDeck = new CARD[N];
    for (int i = 0; i < RANKS; i++) {
        for (int j = 0; j < SUITS; j++) {
            //deck[SUITS*i + j] = rank[i] + " of " + suit[j];
            card = new CARD(suit[j], rank[i], value[i], imageArray[SUITS*i + j]);
            theDeck[SUITS*i + j] = card;
        }
    }

So my question is why does the card imageIcon not display? (I am getting no error messages)

1
I suppose imageArray[SUITS*i + j] is an array containing ImageIcons. Are you sure it is initialized properly? And what is that C1 variable for? - svz
Yes, it is an array containing ImageIcon's. The C1 variable is an ImageIcon in the DECK class, it is shown above. - Phil Hunter
I edited the DECK class above to show how I initialised the Array.. - Phil Hunter
You should check your CARD constructor then. Are you shure that the label in the constructor is initialized properly and is added to the userPanel? Try to draw a label with some text, not with an image. This may help you locate the mistake. - svz
And another advice is to replace your C1 .. C52 instantiation with a for loop. It will save you a lot of code. And an arrayList instead of an array would be more convinient. - svz

1 Answers

0
votes

Ok, so I found the problem. I hadn't initialized the array imageArray[] with the imageIcons properly. Silly mistake but I thought I would leave this up in case other people make this silly mistake too. :)