?? pkcard.java
字號(hào):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PKCard extends JLabel implements MouseListener,
MouseMotionListener{
//紙牌的位置
Point point = null;
Point initPoint = null;
int value = 0;
int type = 0;
String name = null;
Container pane = null;
Spider main = null;
boolean canMove = false;
boolean isFront = false;
PKCard previousCard = null;
public void mouseClicked(MouseEvent arg0){
}
public void flashCard(PKCard card){
//啟動(dòng)Flash線程
new Flash(card).start();
//不停的獲得下一張牌,直到完成
if(main.getNextCard(card) != null){
card.flashCard(main.getNextCard(card));
}
}
class Flash extends Thread{
private PKCard card = null;
public Flash(PKCard card){
this.card = card;
}
/*
**線程的run()方法
**為紙牌的正面設(shè)置白色圖片
*/
public void run(){
boolean is = false;
ImageIcon icon = new ImageIcon("images/white.gif");
for (int i = 0; i < 4; i++){
try{
Thread.sleep(200);
}
catch (InterruptedException e){
e.printStackTrace();
}
if (is){
this.card.turnFront();
is = !is;
}
else{
this.card.setIcon(icon);
is = !is;
}
// 根據(jù)當(dāng)前外觀將card的UI屬性重置
card.updateUI();
}
}
}
/**
**點(diǎn)擊鼠標(biāo)
*/
public void mousePressed(MouseEvent mp){
point = mp.getPoint();
main.setNA();
this.previousCard = main.getPreviousCard(this);
}
/**
**釋放鼠標(biāo)
*/
public void mouseReleased(MouseEvent mr){
Point point = ((JLabel) mr.getSource()).getLocation();
//判斷可行列
int n = this.whichColumnAvailable(point);
if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){
this.setNextCardLocation(null);
main.table.remove(this.getLocation());
this.setLocation(this.initPoint);
main.table.put(this.initPoint, this);
return;
}
point = main.getLastCardLocation(n);
boolean isEmpty = false;
PKCard card = null;
if (point == null){
point = main.getGroundLabelLocation(n);
isEmpty = true;
}
else{
card = (PKCard) main.table.get(point);
}
if (isEmpty || (this.value + 1 == card.getCardValue())){
point.y += 40;
if (isEmpty) point.y -= 20;
this.setNextCardLocation(point);
main.table.remove(this.getLocation());
point.y -= 20;
this.setLocation(point);
main.table.put(point, this);
this.initPoint = point;
if (this.previousCard != null){
this.previousCard.turnFront();
this.previousCard.setCanMove(true);
}
this.setCanMove(true);
}
else{
this.setNextCardLocation(null);
main.table.remove(this.getLocation());
this.setLocation(this.initPoint);
main.table.put(this.initPoint, this);
return;
}
point = main.getLastCardLocation(n);
card = (PKCard) main.table.get(point);
if (card.getCardValue() == 1){
point.y -= 240;
card = (PKCard) main.table.get(point);
if (card != null && card.isCardCanMove()){
main.haveFinish(n);
}
}
}
/*
**方法:放置紙牌
*/
public void setNextCardLocation(Point point){
PKCard card = main.getNextCard(this);
if (card != null){
if (point == null){
card.setNextCardLocation(null);
main.table.remove(card.getLocation());
card.setLocation(card.initPoint);
main.table.put(card.initPoint, card);
}
else{
point = new Point(point);
point.y += 20;
card.setNextCardLocation(point);
point.y -= 20;
main.table.remove(card.getLocation());
card.setLocation(point);
main.table.put(card.getLocation(), card);
card.initPoint = card.getLocation();
}
}
}
/**
**返回值:int
**方法:判斷可用列
*/
public int whichColumnAvailable(Point point){
int x = point.x;
int y = point.y;
int a = (x - 20) / 101;
int b = (x - 20) % 101;
if (a != 9){
if (b > 30 && b <= 71){
a = -1;
}
else if (b > 71){
a++;
}
}
else if (b > 71){
a = -1;
}
if (a != -1){
Point p = main.getLastCardLocation(a);
if (p == null) p = main.getGroundLabelLocation(a);
b = y - p.y;
if (b <= -96 || b >= 96){
a = -1;
}
}
return a;
}
public void mouseEntered(MouseEvent arg0){
}
public void mouseExited(MouseEvent arg0){
}
/**
**用鼠標(biāo)拖動(dòng)紙牌
*/
public void mouseDragged(MouseEvent arg0){
if (canMove){
int x = 0;
int y = 0;
Point p = arg0.getPoint();
x = p.x - point.x;
y = p.y - point.y;
this.moving(x, y);
}
}
/**
**返回值:void
**方法:移動(dòng)(x,y)個(gè)位置
*/
public void moving(int x, int y){
PKCard card = main.getNextCard(this);
Point p = this.getLocation();
//將組件移動(dòng)到容器中指定的順序索引。
pane.setComponentZOrder(this, 1);
//在Hashtable中保存新的節(jié)點(diǎn)信息
main.table.remove(p);
p.x += x;
p.y += y;
this.setLocation(p);
main.table.put(p, this);
if (card != null) card.moving(x, y);
}
public void mouseMoved(MouseEvent arg0){
}
/**
**構(gòu)造函數(shù)
*/
public PKCard(String name, Spider spider){
super();
this.type = new Integer(name.substring(0, 1)).intValue();
this.value = new Integer(name.substring(2)).intValue();
this.name = name;
this.main = spider;
this.pane = this.main.getContentPane();
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setIcon(new ImageIcon("images/rear.gif"));
this.setSize(71, 96);
this.setVisible(true);
}
/**
**返回值:void
**方法:令紙牌顯示正面
*/
public void turnFront(){
this.setIcon(new ImageIcon("images/" + name + ".gif"));
this.isFront = true;
}
/**
**返回值:void
**方法:令紙牌顯示背面
*/
public void turnRear(){
this.setIcon(new ImageIcon("images/rear.gif"));
this.isFront = false;
this.canMove = false;
}
/**
**返回值:void
**方法:將紙牌移動(dòng)到點(diǎn)point
*/
public void moveto(Point point){
this.setLocation(point);
this.initPoint = point;
}
/**
**返回值:void
**方法:判斷牌是否能移動(dòng)
*/
public void setCanMove(boolean can){
this.canMove = can;
PKCard card = main.getPreviousCard(this);
if (card != null && card.isCardFront()){
if (!can){
if (!card.isCardCanMove()){
return;
}
else{
card.setCanMove(can);
}
}
else{
if (this.value + 1 == card.getCardValue()
&& this.type == card.getCardType()){
card.setCanMove(can);
}
else{
card.setCanMove(false);
}
}
}
}
/**
**返回值:boolean
**方法:判斷card是否是正面
*/
public boolean isCardFront(){
return this.isFront;
}
/*
**返回值:boolean
**方法:返回是否能夠移動(dòng)
*/
public boolean isCardCanMove(){
return this.canMove;
}
/**
**返回值:int
**方法:獲得card的內(nèi)容值
*/
public int getCardValue(){
return value;
}
/**
**返回值:int
**方法:獲得card的類型
*/
public int getCardType(){
return type;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -