// CardDeckDemo.java - John K. Estell - 8 May 2003 // last modified 17 February 2004 // demonstration of the Card and Deck classes. import java.awt.*; import javax.swing.*; import java.util.*; /** * A basic extension of the javax.swing.JApplet class */ public class CardDeckDemo extends JApplet { private Deck cardDeck; private final String directory = "cards/"; public void init() { // Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller //symantec.itools.lang.Context.setApplet(this); // 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(236,333); cardDisplayLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); cardDisplayLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); cardDisplayLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); cardDisplayLabel.setText("Draw a card..."); cardDisplayLabel.setOpaque(true); getContentPane().add(cardDisplayLabel); cardDisplayLabel.setBackground(java.awt.Color.lightGray); cardDisplayLabel.setForeground(java.awt.Color.black); cardDisplayLabel.setBounds(48,24,132,168); drawCardBtn.setText("Draw a card..."); getContentPane().add(drawCardBtn); drawCardBtn.setBounds(24,204,192,24); newDeckBtn.setText("New & shuffled deck..."); getContentPane().add(newDeckBtn); newDeckBtn.setBounds(24,240,192,24); cardsLeftLbl.setText("Cards Left:"); getContentPane().add(cardsLeftLbl); cardsLeftLbl.setForeground(java.awt.Color.black); cardsLeftLbl.setBounds(24,276,193,24); //}} 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 ); } } cardsLeftLbl.setText( "Cards Left: " + cardDeck.getNumberOfCardsRemaining() ); //{{REGISTER_LISTENERS SymAction lSymAction = new SymAction(); drawCardBtn.addActionListener(lSymAction); newDeckBtn.addActionListener(lSymAction); //}} } //{{DECLARE_CONTROLS javax.swing.JLabel cardDisplayLabel = new javax.swing.JLabel(); javax.swing.JButton drawCardBtn = new javax.swing.JButton(); javax.swing.JButton newDeckBtn = new javax.swing.JButton(); javax.swing.JLabel cardsLeftLbl = new javax.swing.JLabel(); //}} class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == drawCardBtn) drawCardBtn_actionPerformed(event); else if (object == newDeckBtn) newDeckBtn_actionPerformed(event); } } void drawCardBtn_actionPerformed(java.awt.event.ActionEvent event) { Card card = cardDeck.dealCard(); cardDisplayLabel.setText( card.toString() ); cardDisplayLabel.setIcon( card.getCardImage() ); cardsLeftLbl.setText( "Cards Left: " + cardDeck.getNumberOfCardsRemaining() ); if ( cardDeck.getNumberOfCardsRemaining() == 0 ) drawCardBtn.setEnabled( false ); } void newDeckBtn_actionPerformed(java.awt.event.ActionEvent event) { cardDeck.restoreDeck(); cardDeck.shuffle(); drawCardBtn.setEnabled( true ); cardDisplayLabel.setIcon( null ); cardDisplayLabel.setText( "Draw a card..." ); cardsLeftLbl.setText( "Cards Left: " + cardDeck.getNumberOfCardsRemaining() ); } }