面向對象深入
要求:在窗口中顯示一幅撲克牌,在未洗牌前是按照花色排列顯示,點擊重來按鈕後會洗牌,然後會按照打亂的順序排列
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;
/**
* 撲克遊戲窗口類
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class PokerGameFrame extends JFrame {
private boolean initialized = false;
private int index = 0;
private JButton shuffleButton, resetButton, nextButton;
private List<Card> cardList = new ArrayList<Card>();
private Poker poker = new Poker();
private Card card = null;
private BufferedImage offImage = new BufferedImage(1000, 600, 1);
public PokerGameFrame() {
this.setTitle("撲克遊戲");
this.setSize(1000, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
shuffleButton = new JButton("洗牌");
shuffleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
poker.shuffle();
}
});
resetButton = new JButton("重來");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardList.clear();
index = 0;
poker.shuffle();
repaint();
}
});
nextButton = new JButton("下一張");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(index < 54) {
card = poker.deal(index++);
cardList.add(card);
repaint();
}
}
});
this.setLayout(new FlowLayout());
this.add(shuffleButton);
this.add(resetButton);
this.add(nextButton);
}
@Override
public void paint(Graphics g) {
Graphics g3 = offImage.getGraphics();
super.paint(g3);
// 窗口初次加載時在窗口不可見區域將所有圖片繪製一次避免圖片無法顯示的問題
if(!initialized) {
for(int i = 1; i <= 54; i++) {
g.drawImage(getToolkit().getImage(i + ".jpg"), -200, -200, null);
}
initialized = true;
}
// 循環繪製容器中的每張牌
for(int i = 0; i < cardList.size(); i++) {
Card temp = cardList.get(i);
g3.drawImage(temp.getImage(), 20 + 16 * i, 150, null);
}
g.drawImage(offImage, 0, 0, null);
}
public static void main(String[] args) {
new PokerGameFrame().setVisible(true);
}
}
package com.lovoinfo;
import java.awt.Image;
import java.awt.Toolkit;
/**
* 一張撲克
*
* @author
*
*/
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 com.lovoinfo;
public class Poker {
private Card[] cards = new Card[54];
/**
* 構造器(初始化54張牌)
*/
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 com.lovoinfo;
/**
* 枚舉(花色類型)
* @author Administrator
*
*/
public enum Suite {
Spade, Heart, Club, Diamond, Joker
}