// Programmers: Michele Weigle and Peter VanLund // Assignment: Program 5 import java.awt.*; import java.awt.event.*; import javax.swing.*; /******************************************************************** * BlackjackUI * * Member Variables: * ImageIcon[] playerCard - images of player's hand * JLabel[] playerLabel - playerLabel[0] contains the player's score * JButton cmdDeal - Deal button * JButton cmdStand - Stand button * JButton cmdQuit - Quit button * JLabel instructLabel - label that contains the instructions * 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() - draw all blank cards * void setCard(int position, Card c) - draw the image of the card * * 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 visual constants private final static String TITLE = "Blackjack"; private final static int HEIGHT = 400; // Button state constants private final static boolean IS_DEAL = true; private final static boolean IS_HIT = false; // MEMBER VARIABLES // Card images private ImageIcon[] playerCard; private JLabel[] playerLabel; // Buttons private JButton cmdDeal; // Deal button private JButton cmdStand; // Stand button private JButton cmdQuit; // Quit button // Others private JLabel instructLabel; // Instruction 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; i<=Deck.DECK_SIZE+1; i++) { imgDeck[i] = new ImageIcon("img/"+i+".gif"); } // Set window properties setTitle(TITLE); setSize(imgDeck[0].getIconWidth()*BlackjackGame.MAX_CARDS_IN_HAND+50, HEIGHT); setBackground(Color.green.darker().darker()); // Get content and set layout of window Container content = getContentPane(); content.setLayout(new BorderLayout()); // Declare and instantiate different panels JPanel playerCardPanel = new JPanel(new GridLayout (1, BlackjackGame.MAX_CARDS_IN_HAND)); JPanel middlePanel = new JPanel(new GridLayout(3,1)); JPanel bottomPanel = new JPanel (new BorderLayout()); JPanel instructPanel = new JPanel(new FlowLayout()); JPanel tallyPanel = new JPanel(new FlowLayout()); JPanel buttonPanel = new JPanel(new GridLayout(1,3)); // Setup player's hand playerCard = new ImageIcon[BlackjackGame.MAX_CARDS_IN_HAND]; 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; } 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