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)
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. 🙂