Java day 05丶11

Java第十天##


package day0511;

import java.awt.Image;
import java.awt.Toolkit;

/**
 * 一张扑克
 * 
 * @author A
 *
 */
public class Card {
	private Suite suite;
	private int face;
	private Image image;
	
	/**
	 * 构造器
	 * @param suite 花色
	 * @param face 点数
	 */
	public Card(Suite suite, int face) {
		this(suite, face, null);
	}
	
	/**
	 * 构造器
	 * @param suite 花色
	 * @param face 点数
	 * @param filename 对应的图片
	 */
	public Card(Suite suite, int face, String filename) {
		this.suite = suite;
		this.face = face;
		this.image = Toolkit.getDefaultToolkit().getImage(filename);
	}

	/**
	 * 获得花色
	 * @return 花色
	 */
	public Suite getSuite() {
		return suite;
	}

	/**
	 * 获得点数
	 * @return 点数
	 */
	public int getFace() {
		return face;
	}
	
	/**
	 * 获得图片
	 * @return 牌对应的图片
	 */
	public Image getImage() {
		return image;
	}
	
	public String toString() {
		String str = "";
		if(suite != Suite.Joker) {
			switch(suite) {
			case Spade:
				str += "黑桃"; break;
			case Heart:
				str += "红桃"; break;
			case Club:
				str += "草花"; break;
			case Diamond:
				str += "方块"; break;
			default:
			}
			
			switch(face) {
			case 1:
				str += "A"; break;
			case 11:
				str += "J"; break;
			case 12:
				str += "Q"; break;
			case 13:
				str += "K"; break;
			default:
				str += face; break;
			}
		}
		else {
			str = face == 15? "小王" : "大王";
		}
		
		return str;
	}

}

package day0511;

public class Poker {
	private Card[] cards = new Card[54];
	
	/**
	 * 构造器(初始化52张牌)
	 */
	public Poker() {
		Suite[] suites = { Suite.Spade, Suite.Heart, Suite.Club, Suite.Diamond };
		int[] faces = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
	
		for(int i = 0; i < cards.length - 2; i++) {
			cards[i] = new Card(suites[i / 13], faces[i % 13], (i + 1) + ".jpg");
		}
		
		cards[52] = new Card(Suite.Joker, 15, "53.jpg");	// 小王
		cards[53] = new Card(Suite.Joker, 16, "54.jpg");	// 大王
	}
	
	/**
	 * 洗牌(随机乱序)
	 */
	public void shuffle() {
		for(int i = 0; i < 100; i++) {
			int index1 = (int) (Math.random() * cards.length);
			int index2 = (int) (Math.random() * cards.length);
			Card temp = cards[index1];
			cards[index1] = cards[index2];
			cards[index2] = temp;
		}
	}
	
	/**
	 * 发牌
	 * @param index 位置
	 * @return 一张牌
	 */
	public Card deal(int index) {
		return cards[index];
	}
	
}

package day0511;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

import day0511.Card;


@SuppressWarnings("serial")
public class PokerFrame extends JFrame {
	private int index = 0;//索引号从0开始
	private boolean initialized = false;
	private Poker poker = new Poker();//实例化Poker类,将牌放入其中,牌顺序参见买的新扑克牌
	private Card card = null;//表示还没有把牌抽出来
	private JButton shuffleButton, resetButton, nextButton;
	private List<Card> cardList = new ArrayList<Card>();//设定一个放抽出来的牌的容器
	private BufferedImage offImage = new BufferedImage(1000, 800, 2);//内存中设定一张画纸
	
	
	
	public  PokerFrame(){
		this.setTitle("扑克");
		this.setSize(1000,800);
		this.setVisible(false);//不可修改窗口大小
		this.setLocationRelativeTo(null);//null参数表示位置居中
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		shuffleButton = new JButton("洗牌");
		shuffleButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				poker.shuffle();//由于没有执行cardList.clear();所以已经发出来的牌不会消失。
				//同理,假如加上了cardList.clear();但是由于没有repaint()语句,所以窗口上的牌不会消失
				//当你执行发牌的时候,因为发牌里面含有repaint;所以在这里如果加上cardList.clear();你将会看见
				//屏幕上的牌从起点开始发了
				
			}
		});
		resetButton = new JButton("重洗");
		resetButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {				
				cardList.clear();//重洗,先清洗cardList里面储存的牌,以便下一次发牌,窗口上显示的已经发的牌会消失
				index = 0;//将索引号回归默认,此时一直点可以发54张牌,不然重新发牌的话不会发够54张
				poker.shuffle();//洗牌
				repaint();//repaint()方法默认的是调用paint()方法,因此到这会执行一次paint()方法。
				//但是因为我们将cardList清除了,所以paint()绘制出来的cardList没有内容,所以相当于把原来画的东西去掉。
				
			}
		});
		nextButton = new JButton("发牌");
		nextButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (index < 54){
					card = poker.deal(index++);//从0开始发,0发了发1,所以有++
					cardList.add(card);	//将发出来的card装入容器				
					repaint();//刷新图形,重新绘制,调用后面的paint(Graphics g)方法
				}
				
			}
		});
		
		
		this.setLayout(new FlowLayout());//设置流式样式管理器
		this.add(shuffleButton);
		this.add(resetButton);
		this.add(nextButton);
	}
	
	public void paint(Graphics g){
		
		Graphics g3 = offImage.getGraphics();//调用offImage的画笔,此画笔在BufferedImage画布上作画
		super.paint(g3);//调用offImage的画笔,
		if(!initialized) {//在Frame不显示的地方将每个图片绘制出来,加快图片加载速度。
			for(int i = 1; i <= 54; i++) {
				g.drawImage(getToolkit().getImage(i + ".jpg"), -200, -200, null);
			}
			initialized = true; //因为只需要绘制一次,所以设定个true,下次执行时上述语句就不执行了。
		}
		for(int i = 0; i < cardList.size(); i++) {//因为洗牌后cardList
			Card temp = cardList.get(i);//设置一个临时Card类型变量来储存每次发牌得到的牌
			g3.drawImage(temp.getImage(), 20 + 16 * i, 150, null);//将得到的牌在BufferedImage上画出来
		}
		
		g.drawImage(offImage, 0, 0, null);//将用BufferedImage上画的图片画在窗口上,此画笔在窗口上作画
	}
	public static void main(String[] args) {
		new PokerFrame().setVisible(true);//显示以上绘制的窗口可见
	}
}



package day0511;

class PokerTest {

	public static void main(String[] args) {
		Poker p = new Poker();
		System.out.println("洗牌前:");
		for(int i = 0; i < 54; i++) {
			System.out.println(p.deal(i));
		}
		
		p.shuffle();
		System.out.println("\n\n洗牌后:");
		for(int i = 0; i < 54; i++) {
			System.out.println(p.deal(i));
		}
	}
	
}

package day0511;
/**
 * 枚举法
 * @author A
 *
 */
public enum Suite {
	Spade, Heart, Club, Diamond, Joker
}
posted @ 2015-05-11 23:34  悲伤丶才说爱  阅读(106)  评论(0编辑  收藏  举报