jeudi 9 avril 2015

How to add an image to an object in Java?

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: http://ift.tt/1yh8OWK


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.


Aucun commentaire:

Enregistrer un commentaire