// Programmers: Michele Weigle and Peter VanLund // Assignment: Program 6 import java.awt.*; import java.awt.event.*; import javax.swing.*; /******************************************************************** * BlackjackUI * * Member Variables: * ImageIcon[] playerCard - images of player's hand * ImageIcon[] dealerCard - images of dealer's hand * JLabel[] playerLabel - playerLabel[0] contains the player's score * JLabel[] dealerLabel - dealerLabel[0] contains the dealer's score * JButton cmdDeal - Deal button * JButton cmdStand - Stand button * JButton cmdQuit - Quit button * JLabel instructLabel - label that contains the instructions * JLabel tallyLabel - label that contains the wins/losses/ties * boolean buttonState - state of the Deal/Hit button * ImageIcon[] imgDeck - images for the entire deck of cards * * Private Methods: * void dealButtonPressed() - called when the Deal button is pressed * void hitButtonPressed() - called when the Hit button is pressed * void standButtonPressed() - called when the Stand button is pressed * void playerDone() - finish player's turn * void drawBlankCards(boolean dealer) - draw all blank cards for * dealer/player * void setCard(int position, Card c, boolean dealer) - draw the image * of the card in the appropriate hand * * Public Methods: * BlackjackUI() - constructor that sets up the user interface * void actionPerformed(ActionEvent e) - handles button press * * Protected Methods: * void processWindowEvent(WindowEvent e) - handles when app closed *******************************************************************/ public class BlackjackUI extends JFrame implements ActionListener { // Window visuals constants private final static String TITLE = "Blackjack"; private final static int HEIGHT = 600; // Button state constants private final static boolean IS_DEAL = true; private final static boolean IS_HIT = false; private final static boolean PLAYER = false; private final static boolean DEALER = true; // MEMBER VARIABLES // Card images private ImageIcon[] playerCard; private ImageIcon[] dealerCard; private JLabel[] playerLabel; private JLabel[] dealerLabel; // Buttons private JButton cmdDeal; // Deal button private JButton cmdStand; // Stand button private JButton cmdQuit; // Quit button // Others private JLabel instructLabel; // Instruction label private JLabel tallyLabel; // Tally label private boolean buttonState; // State of Deal/Hit button private ImageIcon[] imgDeck; // Images of deck of cards /********************************************* * constructor * * Sets up the user interface -- draws buttons * cards, labels. *********************************************/ public BlackjackUI() { // Instantiate deck array and hand imgDeck = new ImageIcon[Deck.DECK_SIZE+2]; // Instantiate each card in deck image in img folder for(int i=0; ic.getSuit() || c.getSuit()>Card.SPADES) && (c.getSuit() != -1)) { System.out.println("setCard error: suit is out of range"); return; } else if ((2>c.getFace() || c.getFace()>Card.ACE) && (c.getFace() != -1)) { System.out.println("setCard error: face is out of range"); return; } else if(0>position || position>BlackjackGame.MAX_CARDS_IN_HAND) { System.out.println("setCard error: position is out of range"); return; } // Calculate index if (c.getSuit() == -1 && c.getFace() == -1) { // face down index = 0; } else if (c.getSuit() == 0 && c.getFace() == -1) { // blank index = Deck.DECK_SIZE+1; } else { index = Deck.CARDS_IN_SUIT*c.getSuit()+c.getFace()-1; } // Update images if (dealer) { dealerLabel[position].setIcon(imgDeck[index]); } else { playerLabel[position].setIcon(imgDeck[index]); } } // end of setCard method /********************************************* * actionPerformed * * Takes an event as a parameter and returns nothing * Handle button press *********************************************/ public void actionPerformed(ActionEvent e) { if (e.getSource() == cmdDeal) { if (buttonState == IS_DEAL) { // Deal dealButtonPressed(); } else if (buttonState == IS_HIT) { // Hit hitButtonPressed(); } } else if (e.getSource() == cmdStand) { // Stand standButtonPressed(); } else if (e.getSource() == cmdQuit) { // Quit System.exit(0); } } // end of actionPerformed method /********************************************* * processWindowEvent * * Takes an event as a parameter and returns nothing * Handle when the application close button * is pressed *********************************************/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); } } // end of BlackjackUI class