0

So I have my BufferedImages:

private BufferedImage sdk1, sdk2, sdk3, sdk4, sdk5, sdk6, sdk7, sdk8, sdk9, sdk10, bc;

And my Array values set:

ArrayList<Card> deck1 = new ArrayList(50);
deck1.add(CardList.Spade);

I was wondering, can I assign an image to "Spade" in my Array list? So when it's used on input of the Scanner, it will display an image in a JFrame.

Scanner monSummon = new Scanner(System.in);
int whichMonsterPOS1 = monSummon.nextInt();

Sorry if I can't explain it anymore, and thanks in advance.

4
  • Why not just provide a Image property to your Card? Commented May 31, 2014 at 22:58
  • "Why not just provide a Image property to your Card?" To my Card Class? Commented May 31, 2014 at 23:00
  • 1
    Yes, so the image is directly related/contained/managed by the Card class directly Commented May 31, 2014 at 23:06
  • I recommend ^this^ approach; in fact I used it to create a card game platform. EDIT: see answer, below. Commented Jun 1, 2014 at 21:50

2 Answers 2

1

You Could

Use some kind of Map, keyed to an instance of Card...

private Map cardImages;
//...
cardImages = new HashMap<Card, BufferedImage>(25);
//...    
Card card = ...;
sdk1 = ...;
cardImages.put(card, sdk1);

You Could

Provide a Image property for your Card, this would allow you to create a specific implementation of the required card, supplying the image property directly, for example...

public class AceOfSpadesCard extends Card {
    private BufferedImage img;
    public AceOfSpadesCard() {
        img = ...;
    }

    public BufferedImage getImage() {
        return img;
    }
}

In this case, I would make Card abstract and make the getImage method abstract

Or, if that's to much, simply supply a parameter via the Cards constructor.

Sign up to request clarification or add additional context in comments.

9 Comments

Hm, would I have to add this property for every single card, separating them from their CardList, and put every single one in their own class?
Yes, but as I said, you could create concrete implementation of each card type. You could then create a Deck class which would produce all the Card implementations. Given that a card has two basic properties, suit and number, it wouldn't be difficult to produce a compound loop to generate them
It's a kind of card game I'm working on, but not like Poker or anything like that, it's a YuGiOh based game, and I have SpellCard and MonsterCards (soon to be Trap Cards as well) extending a Card Class; and they're all in separate card lists of 50 (you could say decks), so I don't know how I would just assign an image to their declaration in the CardList Class. Feels a bit odd putting 50+ cards in their own classes, is there another way to do this? Or?
To me, a Card is container or "super" class to the actually "playable" Card. It describes all the common attributes that all "playable" cards should have. Now if you wanted to, you could use an external configuration file to store the details of each card, making it easy to update/add/remove properties and cards. You could devise a factory style set up that could read these properties and generate the "Deck". At some point, you're going to need to decide which approach provides you with the most flexibility for the work involved in setting it up, there's no simple way to achieve this
I understand it won't be easy, if you're implying that, but I have a Card class, with a name and description string, which my MonsterCard class extends. Now all Cards are sorted into decklists (I used Spade as an example of card). CardListKaiba contains all of Kaiba's deck formatted like this: MonsterCard BlueEyesWhiteDragon = new MonsterCard("desc", "name", "attribute", "type", "Summon Type", "Effect", "level number", "ATK points", "DEF points");
|
0

An example Card class instance that also contains references for the Image to be drawn. Then it's just a matter of putting a set of Cards in an ArrayList (or build a Deck model you can then manipulate) and calling paint on the Card's bufferedImage.

package model.playing;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import model.AbstractCard;

public class PlayingCard extends AbstractCard {

    private String[] values = {"ace", "two", "three", "four", "five", "six", "seven",
            "eight", "nine", "ten", "jack", "queen", "king"};

    private BufferedImage drawableTarget;
    private BufferedImage drawableBackTarget;

    public PlayingCard(String suit, int value, String desc) throws Exception {
        setFlipped(false);
        setSuit(suit);

        if(null == getSuit()) {
            throw new Exception("Suit not found!");
        } else if(getSuit().equals("joker")) {
            throw new Exception("That's not how you create a joker!");          
        }

        if(value < 14) {
            setValue(value);
        } else {
            throw new Exception("Invalid value, expecting 13 or less!");
        }

        setDescription(desc);
    }

    // removed Joker instantiation to keep short

    public BufferedImage returnDrawable() {
        URL url = null;
        BufferedImage img = null;

        if(isFlipped()) {
            if(null != getDrawableTarget()) {
                img = getDrawableTarget();
            } else {
                url = getClass().getResource("/suits/" + getSuit()
                        + "/" + getValues()[getValue() - 1] + ".png");
                try {
                    img = ImageIO.read(url);
                    setDrawableTarget(img);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            if(null != getDrawableBackTarget()) {
                img = getDrawableBackTarget();
            } else {
                url = getClass().getResource("/suits/back.png");
                try {
                    img = ImageIO.read(url);
                    setDrawableBackTarget(img);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return img;
    }

    // removed all getters and setters to save on space; they're not important

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.