PKCard.java
Upload User: h_z_ygh
Upload Date: 2020-12-29
Package Size: 134k
Code Size: 7k
Category:

Games

Development Platform:

Java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class PKCard extends JLabel implements MouseListener,
  5.         MouseMotionListener{
  6.     //纸牌的位置
  7. Point point = null;
  8.     Point initPoint = null;
  9.     
  10. int value = 0;
  11.     int type = 0;
  12.     
  13. String name = null;
  14.     Container pane = null;
  15.     
  16. Spider main = null;
  17.     
  18. boolean canMove = false;
  19.     boolean isFront = false;
  20.     PKCard previousCard = null;
  21.     public void mouseClicked(MouseEvent arg0){
  22. }
  23.     
  24.     public void flashCard(PKCard card){
  25. //启动Flash线程
  26. new Flash(card).start();
  27. //不停的获得下一张牌,直到完成
  28. if(main.getNextCard(card) != null){
  29. card.flashCard(main.getNextCard(card));
  30. }
  31. }
  32.     class Flash extends Thread{
  33. private PKCard card = null;
  34. public Flash(PKCard card){
  35. this.card = card;
  36. }
  37. /*
  38.  **线程的run()方法
  39.  **为纸牌的正面设置白色图片
  40.  */
  41. public void run(){
  42. boolean is = false;
  43. ImageIcon icon = new ImageIcon("images/white.gif");
  44. for (int i = 0; i < 4; i++){
  45. try{
  46. Thread.sleep(200);
  47. }
  48. catch (InterruptedException e){
  49. e.printStackTrace();
  50. }
  51. if (is){
  52. this.card.turnFront();
  53. is = !is;
  54. }
  55. else{
  56. this.card.setIcon(icon);
  57. is = !is;
  58. }
  59. // 根据当前外观将card的UI属性重置
  60. card.updateUI();
  61. }
  62. }
  63. }
  64.     /**
  65.  **点击鼠标
  66.  */
  67. public void mousePressed(MouseEvent mp){
  68.         point = mp.getPoint();
  69.         main.setNA();
  70.         this.previousCard = main.getPreviousCard(this);
  71.     }
  72.     /**
  73.  **释放鼠标
  74.  */
  75. public void mouseReleased(MouseEvent mr){
  76. Point point = ((JLabel) mr.getSource()).getLocation();
  77. //判断可行列
  78. int n = this.whichColumnAvailable(point);
  79. if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){
  80. this.setNextCardLocation(null);
  81. main.table.remove(this.getLocation());
  82. this.setLocation(this.initPoint);
  83. main.table.put(this.initPoint, this);
  84. return;
  85. }
  86. point = main.getLastCardLocation(n);
  87. boolean isEmpty = false;
  88. PKCard card = null;
  89. if (point == null){
  90. point = main.getGroundLabelLocation(n);
  91. isEmpty = true;
  92. }
  93. else{
  94. card = (PKCard) main.table.get(point);
  95. }
  96. if (isEmpty || (this.value + 1 == card.getCardValue())){
  97. point.y += 40;
  98. if (isEmpty) point.y -= 20;
  99. this.setNextCardLocation(point);
  100. main.table.remove(this.getLocation());
  101. point.y -= 20;
  102. this.setLocation(point);
  103. main.table.put(point, this);
  104. this.initPoint = point;
  105. if (this.previousCard != null){
  106. this.previousCard.turnFront();
  107. this.previousCard.setCanMove(true);
  108. }
  109. this.setCanMove(true);
  110. }
  111. else{
  112. this.setNextCardLocation(null);
  113. main.table.remove(this.getLocation());
  114. this.setLocation(this.initPoint);
  115. main.table.put(this.initPoint, this);
  116. return;
  117. }
  118.         point = main.getLastCardLocation(n);
  119.         card = (PKCard) main.table.get(point);
  120.         if (card.getCardValue() == 1){
  121. point.y -= 240;
  122. card = (PKCard) main.table.get(point);
  123. if (card != null && card.isCardCanMove()){
  124. main.haveFinish(n);
  125. }
  126. }
  127. }
  128. /*
  129.  **方法:放置纸牌
  130.  */
  131. public void setNextCardLocation(Point point){
  132. PKCard card = main.getNextCard(this);
  133. if (card != null){
  134. if (point == null){
  135. card.setNextCardLocation(null);
  136. main.table.remove(card.getLocation());
  137. card.setLocation(card.initPoint);
  138. main.table.put(card.initPoint, card);
  139. }
  140. else{
  141. point = new Point(point);
  142. point.y += 20;
  143. card.setNextCardLocation(point);
  144. point.y -= 20;
  145. main.table.remove(card.getLocation());
  146. card.setLocation(point);
  147. main.table.put(card.getLocation(), card);
  148. card.initPoint = card.getLocation();
  149. }
  150. }
  151. }
  152.     /**
  153.  **返回值:int
  154.  **方法:判断可用列
  155.  */
  156. public int whichColumnAvailable(Point point){
  157. int x = point.x;
  158. int y = point.y;
  159. int a = (x - 20) / 101;
  160. int b = (x - 20) % 101;
  161. if (a != 9){
  162. if (b > 30 && b <= 71){
  163. a = -1;
  164. }
  165. else if (b > 71){
  166. a++;
  167. }
  168. }
  169. else if (b > 71){
  170. a = -1;
  171. }
  172. if (a != -1){
  173. Point p = main.getLastCardLocation(a);
  174. if (p == null) p = main.getGroundLabelLocation(a);
  175. b = y - p.y;
  176. if (b <= -96 || b >= 96){
  177. a = -1;
  178. }
  179. }
  180. return a;
  181. }
  182.     public void mouseEntered(MouseEvent arg0){
  183.     }
  184.     public void mouseExited(MouseEvent arg0){
  185.     }
  186.     /**
  187.  **用鼠标拖动纸牌
  188.  */
  189. public void mouseDragged(MouseEvent arg0){
  190.         if (canMove){
  191. int x = 0;
  192. int y = 0;
  193. Point p = arg0.getPoint();
  194. x = p.x - point.x;
  195. y = p.y - point.y;
  196. this.moving(x, y);
  197. }
  198. }
  199.     /**
  200.  **返回值:void
  201.  **方法:移动(x,y)个位置
  202.  */
  203. public void moving(int x, int y){
  204.         PKCard card = main.getNextCard(this);
  205.         Point p = this.getLocation();
  206.         
  207. //将组件移动到容器中指定的顺序索引。 
  208. pane.setComponentZOrder(this, 1);
  209.         
  210. //在Hashtable中保存新的节点信息
  211. main.table.remove(p);
  212.         p.x += x;
  213.         p.y += y;
  214.         this.setLocation(p);
  215.         main.table.put(p, this);
  216.         if (card != null) card.moving(x, y);
  217.     }
  218.     public void mouseMoved(MouseEvent arg0){
  219.     }
  220.     /**
  221.      **构造函数
  222.      */
  223.     public PKCard(String name, Spider spider){
  224.         super();
  225.         this.type = new Integer(name.substring(0, 1)).intValue();
  226.         this.value = new Integer(name.substring(2)).intValue();
  227.         this.name = name;
  228.         this.main = spider;
  229.         this.pane = this.main.getContentPane();
  230.         this.addMouseListener(this);
  231.         this.addMouseMotionListener(this);
  232.         this.setIcon(new ImageIcon("images/rear.gif"));
  233.         this.setSize(71, 96);
  234.         this.setVisible(true);
  235.     }
  236.     /**
  237.  **返回值:void
  238.  **方法:令纸牌显示正面
  239.  */
  240. public void turnFront(){
  241.         this.setIcon(new ImageIcon("images/" + name + ".gif"));
  242.         this.isFront = true;
  243.     }
  244.     /**
  245.  **返回值:void
  246.  **方法:令纸牌显示背面
  247.  */
  248. public void turnRear(){
  249.         this.setIcon(new ImageIcon("images/rear.gif"));
  250.         this.isFront = false;
  251.         this.canMove = false;
  252.     }
  253.     /**
  254.  **返回值:void
  255.  **方法:将纸牌移动到点point
  256.  */
  257. public void moveto(Point point){
  258.         this.setLocation(point);
  259.         this.initPoint = point;
  260.     }
  261.     /**
  262.  **返回值:void
  263.  **方法:判断牌是否能移动
  264.  */
  265. public void setCanMove(boolean can){
  266.         this.canMove = can;
  267.         PKCard card = main.getPreviousCard(this);
  268.         if (card != null && card.isCardFront()){
  269.             if (!can){
  270.                 if (!card.isCardCanMove()){
  271.                     return;
  272. }
  273.                 else{
  274. card.setCanMove(can);
  275. }
  276. }
  277.             else{
  278.                 if (this.value + 1 == card.getCardValue()
  279.                         && this.type == card.getCardType()){
  280. card.setCanMove(can);
  281. }
  282. else{
  283. card.setCanMove(false);
  284. }
  285.             }
  286.         }
  287.     }
  288.     /**
  289.  **返回值:boolean
  290.  **方法:判断card是否是正面
  291.  */
  292. public boolean isCardFront(){
  293.         return this.isFront;
  294.     }
  295.     /*
  296.  **返回值:boolean
  297.  **方法:返回是否能够移动
  298.  */
  299. public boolean isCardCanMove(){
  300.         return this.canMove;
  301.     }
  302.     /**
  303.  **返回值:int
  304.  **方法:获得card的内容值
  305.  */
  306. public int getCardValue(){
  307.         return value;
  308.     }
  309.     /**
  310.  **返回值:int
  311.  **方法:获得card的类型
  312.  */
  313. public int getCardType(){
  314.         return type;
  315.     }
  316. }