代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
蜘蛛纸牌
作者:
火龙战士
/ 发布于
2013/2/17
/
831
package dashan; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PKCard extends JLabel implements MouseListener, MouseMotionListener { // 纸牌的位置 private Point point = null; private Point initPoint = null; private int value = 0; private int type = 0; private String name = null; private Container pane = null; private Spider main = null; private boolean canMove = false; private boolean isFront = false; private PKCard previousCard = null; /** **返回值:boolean 方法:判断card是否是正面 */ public boolean isFront() { return isFront; } public void setFront(boolean isFront) { this.isFront = isFront; } /** * 返回值:boolean方法:返回是否能够移动 */ public boolean isCanMove() { return canMove; } /** **返回值:void 方法:判断牌是否能移动 */ public void setCanMove(boolean canMove) { this.canMove = canMove; PKCard card = main.getPreviousCard(this); if (card != null && card.isFront()) { if (!canMove) { if (!card.isCanMove()) { return; } else { card.setCanMove(canMove); } } else { if (this.value + 1 == card.getValue() && this.type == card.getType()) { card.setCanMove(canMove); } else { card.setCanMove(false); } } } } /** **返回值:int 方法:获得card的内容值 */ public int getValue() { return value; } public void setValue(int value) { this.value = value; } /** **返回值:int 方法:获得card的类型 */ public int getType() { return type; } public void setType(int type) { this.type = type; } /** **构造函数 */ 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("src/images/rear.gif")); this.setSize(71, 96); this.setVisible(true); } public void mouseClicked(MouseEvent arg0) { } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } /** **点击鼠标 */ public void mousePressed(MouseEvent mp) { point = mp.getPoint(); main.setNA(); this.previousCard = main.getPreviousCard(this); } /** **释放鼠标 */ 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.map.remove(this.getLocation()); this.setLocation(this.initPoint); main.map.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 = main.map.get(point); } if (isEmpty || (this.value + 1 == card.getValue())) { point.y += 40; if (isEmpty) { point.y -= 20; } this.setNextCardLocation(point); main.map.remove(this.getLocation()); point.y -= 20; this.setLocation(point); main.map.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.map.remove(this.getLocation()); this.setLocation(this.initPoint); main.map.put(this.initPoint, this); return; } point = main.getLastCardLocation(n); card = main.map.get(point); if (card.getValue() == 1) { point.y -= 240; card = main.map.get(point); if (card != null && card.isCanMove()) { main.haveFinish(n); } } } public void mouseMoved(MouseEvent arg0) { } /** **用鼠标拖动纸牌 */ 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); } } /* * 方法:放置纸牌 */ public void setNextCardLocation(Point point) { PKCard card = main.getNextCard(this); if (card != null) { if (point == null) { card.setNextCardLocation(null); main.map.remove(card.getLocation());// 先从HashMap中删除card card.setLocation(card.initPoint); // 为card设置新的坐标 main.map.put(card.initPoint, card); // 再将card添加到HashMap中 } else { point = new Point(point); point.y += 20; card.setNextCardLocation(point); point.y -= 20; main.map.remove(card.getLocation()); card.setLocation(point); main.map.put(card.getLocation(), card); card.initPoint = card.getLocation(); } } } /** **返回值:int 方法:判断可用列 */ public int whichColumnAvailable(Point point) {// point是鼠标释放时,纸牌所在的坐标 int x = point.x; // 获取坐标的x坐标 int y = point.y; // 获取坐标的y坐标 int a = (x - 20) / 101; // 20是第一列距离窗口左边的距离,每两列之间的间距是30,101则是30 + // 71,其中71是每一列的宽度 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; } /** **返回值:void 方法:移动(x,y)个位置 */ public void moving(int x, int y) { PKCard card = main.getNextCard(this); Point p = this.getLocation(); // 将组件移动到容器中指定的顺序索引。 pane.setComponentZOrder(this, 1); // 在HashMap中保存新的节点信息 main.map.remove(p); p.x += x; p.y += y; this.setLocation(p); main.map.put(p, this); if (card != null) card.moving(x, y); } /** **返回值:void 方法:令纸牌显示正面 */ public void turnFront() { this.setIcon(new ImageIcon("src/images/" + name + ".gif")); this.isFront = true; } /** **返回值:void 方法:令纸牌显示背面 */ public void turnRear() { this.setIcon(new ImageIcon("src/images/rear.gif")); this.isFront = false; this.canMove = false; } /** **返回值:void 方法:将纸牌移动到点point */ public void moveto(Point point) { this.setLocation(point); this.initPoint = point; } public void flashCard(PKCard card) { // 启动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()方法为纸牌的正面设置白色图片 */ public void run() { boolean is = false; ImageIcon icon = new ImageIcon("src/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; } // 根据当前外观将card的UI属性重置 card.updateUI(); } } } }
试试其它关键字
蜘蛛纸牌
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
火龙战士
贡献的其它代码
(
2
)
.
查找文件
.
蜘蛛纸牌
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3