// BlackjackHandDemo.java - John K. Estell - 16 February 2004 // Last modified: 17 Febraury 2004 // Demonstration program for the basic extension of the Hand class //package cardgame; import java.awt.*; import javax.swing.*; import java.util.*; /** * A basic extension of the javax.swing.JApplet class */ public class BlackjackHandDemo extends JApplet { private Deck cardDeck; private BlackjackHand myHand; private final int SIZE_OF_HAND = 2; private final String directory = "cards/"; private JLabel[] handLbl = new JLabel[ SIZE_OF_HAND ]; public void init() { // This line prevents the "Swing: checked access to system event queue" message seen in some browsers. getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); // This code is automatically generated by Visual Cafe when you add // components to the visual environment. It instantiates and initializes // the components. To modify the code, only use code syntax that matches // what Visual Cafe can generate, or Visual Cafe may be unable to back // parse your Java file into its visual environment. //{{INIT_CONTROLS getContentPane().setLayout(null); getContentPane().setBackground(java.awt.Color.cyan); setSize(383,203); JLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel1.setText("Card"); JLabel1.setOpaque(true); JLabel1.setToolTipText("This is a card."); getContentPane().add(JLabel1); JLabel1.setForeground(java.awt.Color.black); JLabel1.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel1.setBounds(12,12,101,125); JLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); JLabel2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); JLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); JLabel2.setText("Card"); JLabel2.setOpaque(true); JLabel2.setToolTipText("This is a card."); getContentPane().add(JLabel2); JLabel2.setForeground(java.awt.Color.black); JLabel2.setFont(new Font("Dialog", Font.BOLD, 10)); JLabel2.setBounds(120,12,101,125); JButton1.setText("Draw a Hand"); JButton1.setActionCommand("Draw a Hand"); getContentPane().add(JButton1); JButton1.setBounds(12,156,212,32); scoreLbl.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); scoreLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); getContentPane().add(scoreLbl); scoreLbl.setForeground(java.awt.Color.black); scoreLbl.setFont(new Font("Dialog", Font.BOLD, 64)); scoreLbl.setBounds(252,48,103,88); //}} // add the JLabel array mapping here handLbl[0] = JLabel1; handLbl[1] = JLabel2; // add the Card instantiations here cardDeck = new Deck(); Iterator suitIterator = Suit.VALUES.iterator(); while ( suitIterator.hasNext() ) { Suit suit = (Suit) suitIterator.next(); Iterator rankIterator = Rank.VALUES.iterator(); while ( rankIterator.hasNext() ) { Rank rank = (Rank) rankIterator.next(); String imageFile = directory + Card.getFilename( suit, rank ); ImageIcon cardImage = new ImageIcon( getImage( getCodeBase(), imageFile ) ); Card card = new Card( suit, rank, cardImage ); cardDeck.addCard( card ); } } // set up the initial hand myHand = new BlackjackHand(); // add the displaying of cards here for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card drawnCard = cardDeck.dealCard(); myHand.addCard( drawnCard ); } for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card c = myHand.getCard( i ); handLbl[i].setIcon( c.getCardImage() ); handLbl[i].setText( c.toString() ); } showBlackjackScore(); //{{REGISTER_LISTENERS SymAction lSymAction = new SymAction(); JButton1.addActionListener(lSymAction); //}} } //{{DECLARE_CONTROLS javax.swing.JLabel JLabel1 = new javax.swing.JLabel(); javax.swing.JLabel JLabel2 = new javax.swing.JLabel(); javax.swing.JButton JButton1 = new javax.swing.JButton(); javax.swing.JLabel scoreLbl = new javax.swing.JLabel(); //}} private void showBlackjackScore() { int score = myHand.evaluateHand(); if ( score == 21 ) { scoreLbl.setText( score + "!" ); scoreLbl.setForeground( Color.red ); } else { scoreLbl.setText( "" + score ); scoreLbl.setForeground( Color.black ); } } class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == JButton1) JButton1_actionPerformed(event); } } void JButton1_actionPerformed(java.awt.event.ActionEvent event) { // to do: code goes here. cardDeck.restoreDeck(); cardDeck.shuffle(); myHand.discardHand(); for ( int i = 0; i < SIZE_OF_HAND; i++ ) { Card c = cardDeck.dealCard(); myHand.addCard( c ); handLbl[i].setIcon( c.getCardImage() ); handLbl[i].setText( c.toString() ); } showBlackjackScore(); } }