/*
 * CardApplet.java
 *
 * Created on 2006年3月13日, 下午10:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */package playcards;
import java.applet.*;
import java.awt.*;
//import java.awt.Button;
import java.awt.event.*;
/**
 *
 * @author david chen
 */
public class CardApplet extends Applet implements ActionListener {
    private Button sortButton;
    private Button shuffleButton;
    private Button showButton;
    private Deck deck;
    private boolean faceUp;
    /** Creates a new instance of CardApplet */
    public void init() {
        sortButton = new Button("Sort");
        shuffleButton = new Button("Shuffle");
        showButton = new Button("Show");
        deck = new Deck(this);
        faceUp =true;
        sortButton.addActionListener(this);
        shuffleButton.addActionListener(this);
        showButton.addActionListener(this);
        add(sortButton);
        add(shuffleButton);
        add(showButton);
    }
   public void  paint(Graphics g){
       for(int s=0;s<1;s++){
           Card tmpCard = deck.dealOneCard(faceUp);
           g.drawImage(tmpCard.toImage(),10+s,10,this);
       }
       repaint();
   }
    
    public Image cardImage(String s){
        String l = s+".gif";
         return getImage(getCodeBase(),"l");
    }
        public void actionPerformed(ActionEvent e) {
        if(e.getSource()=="Show"){
            faceUp=false;
            showButton.setLabel("Hide Cards");
    }
        else{
            faceUp=true;
            showButton.setLabel("Show");
        }
}
    public void main(String arg[]){
        //return true;
    }
}/*
 *  File: Card.java
 *  Author: Java, Java, Java
 *  Description: This class implements a representation of a
 *   playing card of the sort that would be used in Blackjack,
 *   or Solitaire. A card is represented by an integer that
 *   gives its rank, from 0 to 51. This enables cards easily 
 *   to be sorted. Simple arithmetic can be used to derive a
 *   card's suit (clubs, diamonds, etc.) and face value (ace,
 *   king, etc.) from its rank. Thus rank, suit, and value are
 *   defined as instance variables.
 *
 *  A card is also represented by a String, with CDHS standing
 *   for suit names and 2..A standing for face value. Finally
 *   a card is also represented by two images, one for its face
 *   value, and one for its back. 
 */
package playcards;
import java.awt.*;public class Card implements Comparable {
    public static final int LOW_RANK = 0,   // Ranks of 52 cards
                           HIGH_RANK = 51;
                           
    public int rank;  // 0..51
    public int value;  // face value 2,3,,,10,11(jack),14(ace)
    public int suit;  // 0=club,1=diamond,2=heart,3=spade

    private Image faceImg;       // Face-up image
    private boolean faceUp;      // True when face-up

    /**
     *  The Card() constructor assigns the card a face value and suit,
     *   and records its rank. Initially the card is face up.
     *  @param rank -- an integer between 0 and 51
     */
    public Card (int rank) {
        this.rank = rank;               
        suit = rank / 13;          // Gives a number between 0..3
        value = 2 + rank % 13;     // Gives a number between 2..14 
        faceUp = true;
    } // Card()

    /**
     *  turnUp() changes the card's state by settin the faceUp variable to true
     */
    public void turnUp() {
        faceUp = true;
    }
    /**
     *  turnDown() changes the card's state by settin the faceUp variable to false
     */
    public void turnDown() {
        faceUp = false;
    }    /**
     *  setImage() assigns its parameter as the card's face up image
     *  @param img -- a reference to an Image
     */
    public void setImage (Image img) {
        faceImg = img;
    } // setImage    /**
     *  toImage() returns an Image representing the card's state,
     *   either face up or face down.
     *  @return -- an Image giving the card's state
     */
    public Image toImage() {
        if (faceUp) 
            return faceImg;
        else 
            return null;
    }

    /** 
     * compareTo() -- from the Comparable interface. This method's
     *  implementation is left as an lab exercise.
     */
    public int compareTo(Object o) {
        int oRank = ((Card) o).rank;  // Cast o into a Card reference
        return 0;
    }   /**
    *  toString()  returns a 2 character representation of the card. For
    *    example, "2C" means 2 of clubs, "JD" is jack of diamonds.
    * Algorithm: the instance variables, suit, and value,
    *  are used as indexes into the literal strings that store the correct letters
    */
    public String toString() {
        return "" +  "??23456789tjqka".charAt(value) + "cdhs".charAt(suit);
    } // toString} // Card
/*
 * Deck.java
 *
 * Created on 2006年3月12日, 下午10:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */package playcards;
import java.awt.Image;/**
 *
 * @author david chen
 */
public class Deck {
    public static final int NCARDS = 52;
    private Card deck[];
    private int top=0;
    private Image cardBack;
    
    /** Creates a new instance of Deck */
    public Deck(CardApplet a) {
        cardBack = a.cardImage("b");
        a.showStatus("please wait while card images are loading..");
        deck = new Card[52];
        for( int k=0;k<52;k++){
            deck[k] = new Card(Card.LOW_RANK + k);
            
            Image img = a.cardImage(deck[k].toString());
            deck[k].setImage(img);
        }
    } //Deck()
    
    public Card dealOneCard(boolean faceup){
        Card topCard = deck[top];
        if(faceup)
            topCard.turnUp();
        else
            topCard.turnDown();
        top = (top+1) % NCARDS;
        return topCard;
    }
    
    public Image getImage(int s){
       Image img = deck[s].toImage();
       return img;
    }    
}在Cardapplet中要加载入扑克牌图像,现在就想加入一张。但始终无法实现调了2天也没出来。。刚学java。请高手看一下。。