Search code examples
javaeclipseimageslick2d

How can I attach images to 2 variables in a card game?


I wrongly worded my last question so I'm going to retry this. I am making a card game in java, and currently I have the code to print 4 random cards in an arraylist for x number of players. I am using slick 2d and currently have a menu and a play state. I currently have 2 variables connected to each card, a ranging from 0-3, and b ranging from 0-12.

How can I display an image in the play state that will relate back properly to these 2 variables? (ex. instead of displaying ace of spades, it displays a picture of the ace of spades)

Code:

Deck ()
{
    cards = new ArrayList < > ();
    for (int a = 0 ; a <= 3 ; a++)
    {
        for (int b = 0 ; b <= 12 ; b++)
        {
            cards.add (new Card (a, b));
        }
    }
}

This is the function to draw a card from a deck

public Card PlayerCardDraw ()
{
    Random generator = new Random ();
    int index = generator.nextInt (cards.size ());
    return cards.remove (index);
}

and this is the function that uses the above function to add a card to player 1's hand. I made this to separate each player's hand of cards.

public ArrayList <Card> p1Hand;
public ArrayList <Card> P1CardDraw ()
{

    p1Hand = new ArrayList < > ();
    p1Hand.add (PlayerCardDraw ());
    return p1Hand;
}

I mentioned I'm using slick 2d, so I assume I won't have to post any of that code.


Solution

  • If I were you, I would rewrite/append the Card class to attach an image on initialisation. Create an image in a common directory, and encode it a-bb.* for an example, and create a public image constant for that particular card. then it would be as simple as calling a simple getter to display the image.