2

Pardon my lack of knowledge and maybe improper terminology, as I'm new to Java. Here is a simplified version of my code so far:

import javax.swing.ImageIcon;

public class Cards {
    static ImageIcon CA = new ImageIcon("classic-cards/1.png");
}

Also in another class where playerCard[] is an array of JLabels:

String suit = "C";
String rank = "A";
playerCard[playerTurn].setIcon("Cards." + suit + rank);

Obviously setIcon does not use the String as an argument and therefore this will not work. How can I get this to work? Since this is a deck of cards suit and rank will not always be "C" and "A" but I did this for simplification.

3
  • Did you check if it's possible to create icon (image) on the fly with the text you want to set? Commented Apr 19, 2013 at 3:46
  • please explain a little bit more that in setIcon("Cards.CA") it would be the resultant string.So what is "Cards.CA"? Do you want to place here name of image like "currentCrad.png" Commented Apr 19, 2013 at 3:50
  • Cards.CA would be the object representing an Ace of Clubs. JLabel.setIcon(Cards.CA) would work fine. But I need to use a string variable because I will not be only dealing with 1 card. I hope this helps. Commented Apr 19, 2013 at 4:07

2 Answers 2

4

Create a Map that contains the String and the Icon.

// Create the Map
HashMap<String, Icon> map = new HashMap<String, Icon>();
...
// Add data to the Map
map.put("Cards.CA", CA);
...
//  Access the Map by your key
setIcon(map.get("Cards." + suit + rank));
Sign up to request clarification or add additional context in comments.

5 Comments

What do the arguments in the "map.put("CA", CA);" part represent? Trying to learn how to do this real quick. Do I have to do this for each individual card (52 in a deck)?
@user2297639 here camic means that map.put("CA", new ImageIcon("Cards." + suit + rank));.Try to study the handling of maps
Yes you have to do it for all cards in the deck. The "CA" is how you identify the card. In your example you used suit + rank ("C" + "A"). The variable CA represents the Icon. That is what you called it in your example.
I updated my example to use "Cards.CA". There is really no reason to include the "Cards." part since that does not uniquely identify the card. The "CA" is the unique identifier.
I'm getting a bit confused with the constant changes back and forth. I can't remember how you originally wrote it or if I got it working. But now the CA in the map.put("Cards.CA", CA) "cannot be resolved to a variable"
1

As JLabel icon always get the Icon object so you can set the name of icon here and then pass it to your setIcon.There is no overloaded method of JLable#setIcon(String).It has only one method which is JLable#setIcon(Icon).Please Try this

  Icon icon = new ImageIcon("Cards." + suit + rank);
     //here could be any resource path and name like "/foo/bar/baz.png"
    playerCard[playerTurn].setIcon(icon);

4 Comments

When I try this I get the error "Cannot make a static reference to the non-static method getClass() from the type Object" I don't quite know what that means, but the JLabels are static. I don't know if that helps.
This way I am getting blank images rather than my saved pics of cards.
Actually I have an idea. I might rename all the card files I have and not even use the Cards class. That way I can just use new ImageIcon("classic-cards/" + suit + rank + ".png"). I feel this would work, no?
exactly.Yes this will surly work and I hope classic-cards/ is a directory where you have images

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.