
/*
   This applet will allow the user to practice his or her skills at
   "Visual Poker".  The user is dealt five cards.  The user can
   discard cards by clicking on them.  After selecting the cards
   to be discarded, the user clicks on a rectangle labeled "Draw (after
   selecting cards)" and the discarded cards, if any, are replaced
   by new cards.  At this point, the user can check how good a poker hand
   has been achieved.  The label at the bottom of the applet changes
   to read "Deal a new hand".  When the user clicks this, a new hand
   is dealt and the next round of the game begins.
   
   In this applet, each card is 40 pixels wide and 60 pixels high.
   The card images are taken from a file smallcards.gif, and the
   drawCard() routine depends on that particular file.
   
   The programming of this applet assumes that the applet is
   320 pixels wide and 150 pixels high.
   
   This applet depends on the file smallcards.gif and on the
   Hand and Deck classes.
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class CardsDemo extends Applet implements MouseListener {

   Deck deck;    // A deck that is used for dealing the cards.
   Card card1, card2, card3, card4, card5;  // The five cards.
   String message;  // A message that is displayed at the bottom of
                    //   the applet.
   Font bigFont;    // Font that will be used to display the message.
   
   Image cardpics;  // An image that contains the cards.  Each card is 40-by-60
                    //    pixels.  The cards are arranged in 4 rows and 13 columns,
                    //    according to suit and value.  The order of the suits
                    //    is clubs, hearts, spades, diamonds.  The order of the
                    //    values puts ace at the beginning.  This image is
                    //    loaded from the file smallcards.gif.

   

   /*
    *  The init method of an applet is called by the system to
    *  initialize the applet.  In this case, a background color
    *  is set, the applet is set to listen for mouse events on
    *  itself, the card image is loaded, some variables are
    *  initialized, and the deal() method is called to deal the
    *  first hand of cards.
    */
   public void init() {
      setBackground( new Color(150,80,30) );
      addMouseListener(this);
      cardpics = getImage(getCodeBase(), "smallcards.gif");
      bigFont = new Font("Serif", Font.BOLD, 14);
      deck = new Deck();
      deal();
   }
   

   /*
    *  Shuffles the deck of cards and deals the first five
    *  cards from the deck into the variables card1 through card5.
    *  Sets the message appropriately to tell the user to click 
    *  when ready to draw new cards.  Calls repaint() so that the
    *  screen will be updated to show the new cards and message.
    */
   void deal() {
      deck.shuffle();
      card1 = deck.dealCard();
      card2 = deck.dealCard();
      card3 = deck.dealCard();
      card4 = deck.dealCard();
      card5 = deck.dealCard();
      message = "Draw (after selecting cards)";
      repaint();
   }


   /*
    *  This method is called (by the mousePressed() method given below)
    *  when the user clicks on the message area at the bottom of the
    *  applet.
    */
   void clickedMessage() {
      deal();
   }
   

   /*
    *  This method is called (by the mousePressed() method given below)
    *  when the user clicks on one of the cards.  The parameter, cardNum,
    *  tells which card was clicked.
    */
   void clickedCard(int cardNum) {
      switch (cardNum) {
         case 1:
            card1 = null;
            break;
         case 2:
            card2 = null;
            break;
         case 3:
            card3 = null;
            break;
         case 4:
            card4 = null;
            break;
         case 5:
            card5 = null;
            break;
      }
      repaint();
   }
   
   
   //----------------------------------------------------------------------
   //----------- For Lab 9, you don't need to understand or change ---------
   //------------------  anything after this point ------------------------
   

   /*
    *  Because the applet is registered to listen for mouse clicks on
    *  itself, the system will call this method when the user presses
    *  the mouse on the applet.  In this case, the method checks whether
    *  the user clicked on one of the cards or on the message at the
    *  bottom of the screen.  If the user clicked card N, this method
    *  calls clickedCard(N).  If the user clicked on the message, this
    *  method calls clickedMessage().
    */
   public void mousePressed(MouseEvent evt) {
      int x = evt.getX();  // convenient name for x-coord of mouse
      int y = evt.getY();  // convenient name for y-coord of mouse
      if ( y > 20 && y < 80 ) {
         if (x > 20 && x < 60)
            clickedCard(1);
         else if (x > 80 && x < 120)
            clickedCard(2);
         else if (x > 140 && x < 180)
            clickedCard(3);
         else if (x > 200 && x < 240)
            clickedCard(4);
         else if (x > 260 && x < 300)
            clickedCard(5);
      }
      else if ( y > 100 && y < 130 && x > 30 && x < 290 ) {
         clickedMessage();
      }
   }
   
   
   /*
    *  The paint method is called by the system when the applet needs
    *  to be repainted.  In this applet, it draws the five cards
    *  and the message at the bottom of the applet.
    */
   public void paint(Graphics g) {
         // The paint method shows the message at the bottom of the
         // canvas, and it draws all of the dealt cards spread out
         // across the canvas.  If the game is in progress, an
         // extra card is dealt representing the card to be dealt next.
      int width = getSize().width;
      int height = getSize().height;
      g.setColor( Color.red );            // Draw a border around the applet.
      g.drawRect(0,0,width-1,height-1);
      g.drawRect(1,1,width-3,height-3);
      g.drawRect(2,2,width-5,height-5);
      g.setColor(new Color(220,220,255)); // Draw the message on a blue background.
      g.fillRect(30,100,260,30);
      g.setColor( Color.blue );
      g.drawRect(30,100,259,29);
      g.drawRect(31,101,257,27);
      g.setFont( bigFont );
      if (message != null) {  // Only draw the message if message is non-null.
         FontMetrics fm = getFontMetrics(bigFont);  // for centering the string
         int w = fm.stringWidth(message);     // width of message string
         g.drawString( message, 160 - w/2, 120 );
      }
      drawCard( g, card1, 20, 20 );       // Draw the five cards.
      drawCard( g, card2, 80, 20 );
      drawCard( g, card3, 140, 20 );
      drawCard( g, card4, 200, 20 );
      drawCard( g, card5, 260, 20 );
   }
   
  

   /*
    *  Draws a card as a 40 by 60 rectangle with
    *  upper left corner at (x,y).  The card is drawn
    *  in the graphics context g.  If card is null, then
    *  a face-down card is drawn. The cards are taken
    *  from an image file, smallcards.gif.
    */
   void drawCard(Graphics g, Card card, int x, int y) {
       if (card == null) {  
             // Draw a face-down card
         g.setColor(Color.blue);
         g.fillRect(x,y,40,60);
         g.setColor(Color.white);
         g.drawRect(x+3,y+3,33,53);
         g.drawRect(x+4,y+4,31,51);
      }
      else {
         int row = 0;
         switch (card.getSuit()) {
            case Card.CLUBS:    row = 0;  break;
            case Card.HEARTS:   row = 1;  break;
            case Card.SPADES:   row = 2;  break;
            case Card.DIAMONDS: row = 3;  break;
         }
         int sx, sy;  // coords of upper left corner in the source image.
         sx = 40*(card.getValue() - 1);
         sy = 60*row;
         g.drawImage(cardpics, x, y, x+40, y+60, 
                                 sx, sy, sx+40, sy+60, this);
      }
   }
   
   
   /*
    *  Other mouse routines required by the MouseListener interface.
    *  In this applet, these don't do anything.  
    */
   public void mouseClicked(MouseEvent evt) { }
   public void mouseReleased(MouseEvent evt) { }
   public void mouseEntered(MouseEvent evt) { }
   public void mouseExited(MouseEvent evt) { }


} // end class CardsDemo

