// Programmers: Michele Weigle and Peter VanLund // Assignment: Program 5 import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.net.URL; /******************************************************************** * BlackjackApplet * * 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: * void init() - sets up the user interface * void actionPerformed(ActionEvent e) - handles button press *******************************************************************/ public class BlackjackApplet extends JApplet implements ActionListener { // Button state constants private final static boolean IS_DEAL = true; private final static boolean IS_HIT = false; // constant that allows us to load the applet using a web browser // the images will be loaded from a web site private final static String IMG_URL = "http://www.cs.unc.edu/~mcweigle/courses/comp14-fall03/code/img/cards"; // 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 private JLabel instructLabel; // Instruction label private boolean buttonState; // State of Deal/Hit button private ImageIcon[] imgDeck; // Images of deck of cards /********************************************* * void init() * * Takes no parameters and returns nothing * Sets up the user interface -- draws buttons * cards, labels. *********************************************/ public void init () { BlackjackGame.initGame(); // 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++) { try { imgDeck[i] = new ImageIcon (new URL(IMG_URL + "/" + i + ".gif")); } catch (Exception exception) {} } 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; i<5; i++) { playerCard[i] = new ImageIcon(); } playerLabel = new JLabel[5]; for (int i=0; i<5; i++) { playerLabel[i] = new JLabel (" ", playerCard[i], SwingConstants.LEFT); playerLabel[i].setHorizontalTextPosition(SwingConstants.CENTER); playerLabel[i].setVerticalTextPosition(SwingConstants.TOP); playerLabel[i].setForeground(Color.yellow); } drawBlankCards(); playerLabel[0].setText ("Player:"); for (int i=0; i<5; i++) { playerCardPanel.add(playerLabel[i]); } playerCardPanel.setBackground(Color.green.darker().darker()); content.add(playerCardPanel, BorderLayout.CENTER); // Setup instructions instructLabel = new JLabel("Click Deal to Begin."); instructLabel.setForeground(Color.white); instructPanel.add(instructLabel); instructPanel.setBackground(Color.green.darker().darker()); middlePanel.add(instructPanel); // Setup buttons cmdDeal = new JButton("Deal"); cmdDeal.addActionListener(this); cmdDeal.setBackground(Color.green.darker().darker()); buttonState = IS_DEAL; buttonPanel.add(cmdDeal); cmdStand = new JButton("Stand"); cmdStand.addActionListener(this); cmdStand.setBackground(Color.green.darker().darker()); cmdStand.setEnabled(false); buttonPanel.add(cmdStand); cmdQuit = new JButton("Quit"); cmdQuit.addActionListener(this); cmdQuit.setBackground(Color.green.darker().darker()); buttonPanel.add(cmdQuit); buttonPanel.setBackground(Color.green.darker().darker()); middlePanel.setBackground(Color.green.darker().darker()); middlePanel.add(buttonPanel); // Setup bottom panel bottomPanel.add (middlePanel, BorderLayout.CENTER); content.add(bottomPanel, BorderLayout.SOUTH); setVisible(true); } // end of init method /********************************************* * dealButtonPressed * * Takes no parameters and returns nothing. * Sets up a new hand. Draws the player's hand * and total points in the user interface. * Sets the Deal button to Hit and enables the * Stand button. * Called from: actionPerformed() *********************************************/ private void dealButtonPressed() { BlackjackGame.setupNewHand(); // draw all blank cards drawBlankCards(); // draw player hand in the user interface 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 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 destroy(); setVisible(false); } } } // end of BlackjackApplet class