1

I'm trying to add the proper images to each card object, but I just can't seem to get it to work. I tried to incorporate it to my fillDeck() method, but not sure what the best way to do it, would be.

Here's what I got:

public void fillDeck() {
    Iterator suitIterator = CardSuit.VALUES.iterator();
    while(suitIterator.hasNext()) {
        CardSuit suit = (CardSuit) suitIterator.next();
        Iterator rankIterator = CardValue.VALUES.iterator();

    while(rankIterator.hasNext()) {
        CardValue value = (CardValue) rankIterator.next();  

        /* This is the problem area :L */
        String imageFile = imageLocation + Card.getImageFilename(suit, value);
        ImageIcon image = new ImageIcon(getImage(getCodeBase(), imageFile));
       /* --------------------------- */

        Card card = new Card(suit, value, image);
        addCard(card);
    }}}

Image location is initialized as: private final String imageLocation = "cardImages/";

and getImageFilename is as follows:

public static String getImageFilename( CardSuit suit, CardValue value ) {
    return suit.getSuitAcronym() + value.getValueAcronym() + ".png";
}

I was checking out imageIO, but not quite sure on how that could be implemented into this. Also here's my file tree, could be that I have the images folder in the wrong place: https://i.stack.imgur.com/Jt6po.png

So yeah, sorry for the long post, hopefully someone can provide some insight on how I should go about this or if you can spot some mistakes.

liquardo
  • 91
  • 1
  • 10

1 Answers1

1

To load an image in Java:

BufferedImage image = null;

try{
    image = ImageIO.read(new File("location.png"));
} catch(Exception e){e.printStackTrace();}
John
  • 3,769
  • 6
  • 30
  • 49
  • Now it at least fills the deck, but the images are not found. It gives a javax.imageio.IIOException: Can't read input file! Does that mean the the image path is wrong or is it something else? – liquardo Apr 09 '15 at 21:50
  • `while(rankIterator.hasNext()) { CardValue value = (CardValue) rankIterator.next(); String imageFile = imageLocation + Card.getImageFilename(suit, value); try { testImage = ImageIO.read(new File(imageFile)); } catch(Exception e) { e.printStackTrace(); } Card card = new Card(suit, value, testImage); addCard(card); }` – liquardo Apr 09 '15 at 21:55
  • @FroZenL1Qu1d you can't format code in comments. If you need code, consider editing into your question. – ChrisF Apr 09 '15 at 22:28
  • Alrighty, not sure what to edit in the question, I just can't get this to work for the life of me. That "duplicate" question was of no use, and after googling for 4+ hours, still nothing. – liquardo Apr 09 '15 at 23:36