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?