Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Game: display four cards) Use the same cards from Exercise 12.9 to display a fr

ID: 3550984 • Letter: #

Question

(Game: display four cards) Use the same cards from Exercise 12.9 to display a

frame that contains four buttons. All buttons have the same icon from

backCard.png, as shown in Figure 12.29a. The pressed icons are four cards randomly

selected from the 54 cards in a deck, as shown in Figure 12.29b.


(a) (b) (c)

FIGURE 12.28 (a) Six labels are placed in the frame. (b) Three cards are randomly selected.

(c) A checkerboard is displayed using buttons.

(a) (b) (c)

FIGURE 12.29 (a) The four buttons have the same icon. (b) Each button

Explanation / Answer

/** * Paints the cards stacked top-down in addition to the rest of the * components. The cards are arranged so the user can still see all of * the cards' values. */ public void paintComponent(Graphics g) { super.paintComponent(g); if (hand == null) return; for (int i = 0; i < hand.length(); i++) { drawCard(g, hand.get(i), 10, 76 + 33*i); } }
/** * Paints a card image onto (x,y) of the container. A face down card will * be drawn accordingly. * @param g the graphics context * @param card the card to be printed * @param x the x-position of the printed card in this container * @param y the y-position of the printed card in this container */ private void drawCard(Graphics g, Card card, int x, int y){ String face = ""; String suit = ""; String filepath = "";
if (!card.isFaceUp()) { suit = "b1"; face = "fv"; } else { face = String.valueOf(card.getFace()); if (face == "11"){ face = "j"; } if (face == "12"){ face = "q"; } if (face == "13"){ face = "k"; } switch (card.getSuit()) { case Card.DIAMONDS: suit = "d"; break; case Card.CLUBS: suit = "c"; break; case Card.HEARTS: suit = "h"; break; default: suit = "s"; break; //Spades } BufferedImage cardImg = null; filepath = "C:/Users/Student/workspace/Blackjack GUI/src/images/" + suit + face + ".png"; try { cardImg = ImageIO.read(new File(filepath)); } catch (IOException e) { } g.drawImage(cardImg,x,y,71,96,this);
}