import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
//纸牌操作类,包括各种操作方法
public class Card extends JLabel implements MouseListener, MouseMotionListener {
public Point point, oldPoint;// point是初始鼠标位置
CardMain m = null; //对CarMain引用
public String name;// 卡片名称 1-1
public String up = "rear";// 默认反面
public int currentCol = 0;
public boolean canmove = false;// 判断当前卡片是否可以拖拽
public Card(CardMain m) {
this.m = m;
this.setSize(71, 96);
this.setVisible(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public Card(CardMain m, String name, String flag) {
this.m = m;
this.name = name;
//正反面显示
if (flag == "front")
this.turnFront();
else {
this.turnRear();
}
this.setSize(71, 96);
this.setVisible(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
// 判断输赢
public boolean win() {
// 当牌堆的牌全部去掉的时候,说明已经胜利了
for (int i = 0; i < 7; i++) {
if(!CardMain.tablelist[i].isEmpty())
return false;
}
return true;
}
// 移动操作方法
public void moveto(Point to) {
Point to2 = new Point(to);
//将drag list的脱离加入新的table list
if((to.x>=50)&&(to.x<=650)&&(to.y>=180))
{
for(Card c:CardMain.dragList)
{
if((currentCol>=1)&&(CardMain.tablelist[currentCol-1].indexOf(c)>-1))
CardMain.tablelist[currentCol-1].remove(c);
if((currentCol<=4)&&(currentCol>=1))//来自A区
CardMain.AList[currentCol-1].remove(c);
CardMain.tablelist[this.getCol()-1].add(0,c);//向头部加入,就是最面上那张牌后面
Point point=new Point(to2);
c.setLocation(point);
to2.y+=m.max;
}
}
//移动到A区
if((to.x>=50)&&(to.x<=350)&&(to.y<=150))
{
if((currentCol<=4)&&(!CardMain.AList[currentCol-1].isEmpty()))
CardMain.AList[currentCol-1].remove(this);
CardMain.tablelist[currentCol-1].remove(this);
this.setLocation(new Point(50 + 100 * (this.getCol() - 1), 30));
CardMain.AList[this.getCol()-1].add(0,this);
}
}
// 实现拖动过程
public void moving(int x, int y) {
for (Card card : CardMain.dragList) {
Point p = card.getLocation();
p.x += x;
p.y += y;
card.setLocation(p);
m.container.setComponentZOrder(card, 0);
}
}
public void turnFront() {
this.setIcon(new ImageIcon("images/" + name + ".gif"));
this.up = "front";
}
public void turnRear() {
this.setIcon(new ImageIcon("images/rear.gif"));
this.up = "rear";
}
// 错误地方回滚
public void rollback() {
Point to2=new Point(oldPoint);
for(Card card:CardMain.dragList)
{
Point point=new Point(to2);
card.setLocation(point);
to2.y+=m.max;
}
}
// 获取当前卡片是第几列(1-7)
public int getCol() {
Point nowPoint = this.getLocation();
int xx = nowPoint.x, col = 0;
for (int i = 1; i <= 7; i++) {
if ((Math.abs(xx - 50 - 100 * (i - 1)) < 80)) {
col = i;
break;
}
}
return col;
}
// 得到当前列表的最后一张牌
public Card getLastCard(int col) {
Card card = null;
if (col > 0 && (!CardMain.tablelist[col-1].isEmpty()))
card = CardMain.tablelist[col - 1].get(0);
return card;
}
public boolean Check() {
// 检查范围
Card lastCard = this.getLastCard(this.getCol());
int x = -1, y = -1;
if (lastCard != null) {
x = Math.abs(this.getLocation().x - lastCard.getLocation().x);
y = Math.abs(this.getLocation().y - lastCard.getLocation().y);
}
if (!((x >= 0) && (x < 40) && (y >= 0) && (y < 40))) {
System.out.println("范围出错");
return false;
}
// 检查花色
// 黑1 红2 梅3 方4
String cur = this.name, last = lastCard.name;
int color1 = Integer.valueOf(cur.substring(0, 1));
int color2 = Integer.valueOf(last.substring(0, 1));
if (Math.abs(color1 - color2) % 2 == 0) {
System.out.println(cur+","+last+"color wrong:"+color1+","+color2);
return false;
}
// 检查大小
int num1 = Integer.valueOf(cur.substring(2, cur.length()));
int num2 = Integer.valueOf(last.substring(2, last.length()));
if (num2 - num1 != 1) {
System.out.println("num1-num2!=1");
return false;
}
// 檢查牌是否翻開
if (lastCard.up == "rear") {
System.out.println("lastcard up==rear");
return false;
}
return true;
}
// 判断当前是不是待发牌堆
public boolean isCards() {
if (this.getLocation().x == 600 && this.getLocation().y == 30) {
return true;
} else
return false;
}
// 判断当前是不是在4个框框内
public boolean isFour() {
Point point = this.getLocation();
if (this.getNextCard(this) != null)
return false;
int x = -1, y = -1;
x = Math.abs(point.x - (50 + 100 * (this.getCol() - 1)));
y = Math.abs(point.y - 30);
if ((x >= 0 && x <= 40) && (y >= 0 && y <= 40)) {
System.out.println(x + "," + y);
return true;
} else {
return false;
}
}
// 放入框框
public boolean locateFour() {
// 框框是空的时候只能放A
Card card = null;
Point point = new Point(50 + 100 * (this.getCol() - 1), 30);
if(!CardMain.AList[this.getCol()-1].isEmpty())
card=CardMain.AList[this.getCol()-1].get(0);
if (card == null) {
String s = this.name.substring(2, this.name.length());
if (!s.equals("1")) {
System.out.println(s + "不是A");
return false;// 不是A
}
this.moveto(point);
return true;
} else {
// 说明//框框不是空的时候由小到大,相同花色
String s1 = this.name.substring(0, 1);
String s2 = card.name.substring(0, 1);
int num1 = Integer.valueOf(this.name.substring(2,
this.name.length()));
int num2 = Integer.valueOf(card.name.substring(2,
card.name.length()));
if (!((s1.equals(s2)) && (num1 - num2 == 1))) {
System.out.println("花色不同");
return false;
} else {
this.moveto(point);
return true;
}
}
}
public boolean isK() {
int x = -1, y = -1;
Point point = this.getLocation();
x = Math.abs(point.x - (50 + 100 * (this.getCol() - 1)));
y = Math.abs(point.y - 180);
if (this.getLastCard(this.getCol()) != null) {
System.out.println("当前K下面不为空");
return false;
}
if (!((x >= 0 && x <= 40) && (y >= 0 && y <= 40))) {
System.out.println("区域错误");
return false;
}
String string = this.name.substring(2, this.name.length());
if (!string.equals("13")) {
System.out.println("不是K");
return false;
}
Point point2 = new Point((50 + 100 * (this.getCol() - 1)), 180);
this.moveto(point2);
return true;
}
// 得到当前列表的下一张牌
public Card getNextCard(Card c) {
Card card = null;
int cur = this.getCol() - 1;// 0-6
int flag = -1;
if((cur>=0)&&(cur<7))
flag=CardMain.tablelist[cur].indexOf(c);
if (flag - 1 >= 0) {
card = CardMain.tablelist[cur].get(flag - 1);
}
return card;
}
// 从wait list牌堆里面移除
public void leaveWait(){
if (oldPoint.x == 700 && oldPoint.y == 30) {
CardMain.waitlist.remove(this);// 从牌堆抽出
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if (!CardMain.dragList.isEmpty()) {
Point now = e.getPoint();
int x, y;
x = now.x - point.x;
y = now.y - point.y;
moving(x, y);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
point = e.getPoint();
oldPoint = this.getLocation();
currentCol = this.getCol();
// 将可移动纸牌加入drag list
CardMain.dragList.clear();
if (this.canmove) {
CardMain.dragList.add(this);
Card card = this.getNextCard(this);
while (card != null) {
CardMain.dragL
- 1
- 2
- 3
- 4
- 5
- 6
前往页