librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit d8ccebb49240de9b5867bb36b77881df31a5272f
parent 2ded05b9e8956094544fad6beb0509d1c0969627
Author: Michael Neuper <michael@michaelneuper.com>
Date:   Sun, 30 Apr 2023 22:34:28 +0200

Change icon based on card

Diffstat:
Msrc/controller/Hand.java | 7+++++--
Msrc/view/MainUI.java | 10++++++++++
2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/controller/Hand.java b/src/controller/Hand.java @@ -73,8 +73,11 @@ public class Hand { } public String getCardFilePath(int i) { - System.out.println("/assets/cards/" + HAND.get(i).getFilename()); - return "/assets/cards/" + HAND.get(i).getFilename(); + try { + return "/assets/cards/" + HAND.get(i).getFilename(); + } catch (IndexOutOfBoundsException e) { + return null; // this will set the icon to null + } } /** diff --git a/src/view/MainUI.java b/src/view/MainUI.java @@ -393,6 +393,8 @@ public class MainUI extends javax.swing.JFrame { jLabelBalance.setText(game.getBalance() + ""); // update the balance game.startRound(); // jLabelDealerCard1.setText(game.displayDealerFirstCard()); + jLabelDealerCard1.setIcon(new ImageIcon(getClass().getResource("/assets/cards/back.png"))); + jLabelDealerCard2.setIcon(new ImageIcon(getClass().getResource(game.getDealerCardFilePath(1)))); jLabelDealer.setText("Dealer: " + game.displayDealerFirstCardValue()); printPlayerHand(); }//GEN-LAST:event_jButtonDealActionPerformed @@ -439,11 +441,19 @@ public class MainUI extends javax.swing.JFrame { private void printDealerHand() { // jLabelDealerCard1.setText(game.displayDealerHand()); + jLabelDealerCard1.setIcon(new ImageIcon(getClass().getResource(game.getDealerCardFilePath(0)))); + jLabelDealerCard2.setIcon(new ImageIcon(getClass().getResource(game.getDealerCardFilePath(1)))); + jLabelDealerCard3.setIcon(new ImageIcon(getClass().getResource(game.getDealerCardFilePath(2)))); + jLabelDealerCard4.setIcon(new ImageIcon(getClass().getResource(game.getDealerCardFilePath(3)))); jLabelDealer.setText("Dealer: " + game.displayDealerHandValue()); } private void printPlayerHand() { // jLabelPlayerCard1.setText(game.displayPlayerHand()); + jLabelPlayerCard1.setIcon(new ImageIcon(getClass().getResource(game.getPlayerCardFilePath(0)))); + jLabelPlayerCard2.setIcon(new ImageIcon(getClass().getResource(game.getPlayerCardFilePath(1)))); + jLabelPlayerCard3.setIcon(new ImageIcon(getClass().getResource(game.getPlayerCardFilePath(2)))); + jLabelPlayerCard4.setIcon(new ImageIcon(getClass().getResource(game.getPlayerCardFilePath(3)))); jLabelPlayer.setText("You: " + game.displayPlayerHandValue()); }