Hey guys and guyettes, I'm trying to make a game, when you click on a card, a certain set of things happen to that particular card. As the set of actions that occur are the same for each and every card, putting everything in the MouseClicked event for each and every card would result in a lot of duplicate code with only the parameter names changed, so I am trying to simplify this down. The code for when a card is clicked is such:

private void CardOneMouseClicked(java.awt.event.MouseEvent evt) {                                     
        int cardClicked = 0;
        cardAction(cardClicked);
    }

cardAction then uses a switch on cardClicked to determine which card called the method and change the parameters accordingly.

private void cardAction(int cardClicked) {
        String cardNum = null;
        String cardNumImage = null;
        String cardNumName = null;
        switch(cardClicked) {
            case 0: cardNum = "CardOne";
                    cardNumImage = "cardOneImage";
                    cardNumName = "cardOneName";
            case 1: cardNum = "CardTwo";
                    cardNumImage = "cardTwoImage";
                    cardNumName = "cardTwoName";
            case 2: cardNum = "CardThree";
                    cardNumImage = "cardThreeImage";
                    cardNumName = "cardThreeName";
            case 3: cardNum = "CardFour";
                    cardNumImage = "cardFourImage";
                    cardNumName = "cardFourName";
            case 4: cardNum = "CardFive";
                    cardNumImage = "cardFiveImage";
                    cardNumName = "cardFiveName";
            case 5: cardNum = "CardSix";
                    cardNumImage = "cardSixImage";
                    cardNumName = "cardSixName";
        }

I then use the same code for each card, but tailored via the switch to each specific card:

cardNum.setIcon(new ImageIcon(cardNumImage));
        if(currentCardName == null) {
            currentCardName = cardNumName;
        } else {
            newCardName = cardNumName;
        }

and some other code which isn't really important right now, although in the long term it is as I don't want to have to duplicate it over and over in each MouseClicked event with only changed names.

My issue is that, instead of replacing cardNum with, for example, the object CardOne to use the setIcon method on, it seems to replace it with the String "CardOne", which makes sense I guess. Same with cardNumImage, instead of fetching the BufferedImage cardOneImage, it just sees it as a string. How do I go about making it use what I'm actually trying to point it at instead of just seeing it as a useless string?

Recommended Answers

All 6 Replies

In your event handler you can use evt.getComponent() to get the actual card object that was clicked, which you can then use setIcon etc on.
That way you can also use the same event handler for all the cards.
If cards are some kind of Swing JComponent then you can associate any number of arbitrary values (eg a String, or BufferedImage) with each card by using putClientProperty and getClientProperty

Okay thank you very much, I'm Googling as we speak, could you possibly show how that might be used with some generic code?

What kind of objects are your cards? Are they JLabels or what?

Yes they are indeed JLabels. I have managed to do it as such:

private void cardAction(int cardClicked) {
        JLabel cardNum = null;
        BufferedImage cardNumImage = null;
        String cardNumName = null;
        switch(cardClicked) {
            case 0: cardNum = CardOne;
                    cardNumImage = cardOneImage;
                    cardNumName = cardOneName;
                    break;
            case 1: cardNum = CardTwo;
                    cardNumImage = cardTwoImage;
                    cardNumName = cardTwoName;
                    break;
            case 2: cardNum = CardThree;
                    cardNumImage = cardThreeImage;
                    cardNumName = cardThreeName;
                    break;
            case 3: cardNum = CardFour;
                    cardNumImage = cardFourImage;
                    cardNumName = cardFourName;
                    break;
            case 4: cardNum = CardFive;
                    cardNumImage = cardFiveImage;
                    cardNumName = cardFiveName;
                    break;
            case 5: cardNum = CardSix;
                    cardNumImage = cardSixImage;
                    cardNumName = cardSixName;
                    break;
        }
        cardNum.setIcon(new ImageIcon(cardNumImage));
        if(currentCardName == null) {
            currentCardName = cardNumName;
        } else {
            newCardName = cardNumName;
        }

etc etc which works just as I expect it to, so theoretically I have what I need, but feel free to suggest a better way if you wish, thanks for your replies and help regardless.

Quickly typed in, so no guarantees, but you'll get the idea

// setup the cards at initialisation time...
JLabel card1 = new JLabel();
card1.putClientProperty("Image", new ImageIcon(cardOneImage));
card1.putClientProperty("Name", "cardOneName");
card1.addMouseListener(this);
// repeat this setup for each card

// single listener for all cards
public void MouseClicked(java.awt.event.MouseEvent evt) {
   JLabel card = (JLabel) evt.getComponent();
   card.setImage((ImageIcon) card.getClientProperty("Image"));
   String cardName = ((String) card.getClientProperty("Name));
   // etc
}

Thanks a lot for your help, I'll see if I can apply it like that, cheers again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.