效果展示
前面的导入过程这里就不多说了,不会的可以自己去问度娘。导入后,选择spider.java
类直接运行就可以了,下面是游戏运行的截图:
游戏结构
核心代码
代码显示:代码注释很清楚 ,大家可以自行参考。
aboutdialog.java类
import javax.swing.*; import java.awt.*; /* **“关于”窗口 */ public class aboutdialog extends jdialog { jpanel jmainpane = new jpanel(); jtabbedpane jtabbedpane = new jtabbedpane(); private jpanel jpanel1 = new jpanel(); private jpanel jpanel2 = new jpanel(); private jtextarea jt1 = new jtextarea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。"); private jtextarea jt2 = new jtextarea("该游戏中,纸牌的图片来自于windows xp的纸牌游戏,图片权属于原作者所有!"); /* **构造函数 */ public aboutdialog() { settitle("蜘蛛牌"); setsize(300,200); setresizable(false); setdefaultcloseoperation (windowconstants.dispose_on_close); container c = this.getcontentpane(); jt1.setsize(260,200); jt2.setsize(260,200); jt1.seteditable(false); jt2.seteditable(false); jt1.setlinewrap(true); jt2.setlinewrap(true); jt1.setfont(new font("楷体_gb2312", java.awt.font.bold, 13)); jt1.setforeground(color.blue); jt2.setfont(new font("楷体_gb2312", java.awt.font.bold, 13)); jt2.setforeground(color.black); jpanel1.add(jt1); jpanel2.add(jt2); jtabbedpane.setsize(300,200); jtabbedpane.addtab("游戏规则", null, jpanel1, null); jtabbedpane.addtab("声明", null, jpanel2, null); jmainpane.add(jtabbedpane); c.add(jmainpane); pack(); this.setvisible(true); } }
pkcard.java类
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){ //启动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("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(); } } } /** **点击鼠标 */ 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.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){ } /** **用鼠标拖动纸牌 */ 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 **方法:移动(x,y)个位置 */ public void moving(int x, int y){ pkcard card = main.getnextcard(this); point p = this.getlocation(); //将组件移动到容器中指定的顺序索引。 pane.setcomponentzorder(this, 1); //在hashtable中保存新的节点信息 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){ } /** **构造函数 */ 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 **方法:将纸牌移动到点point */ public void moveto(point point){ this.setlocation(point); this.initpoint = point; } /** **返回值:void **方法:判断牌是否能移动 */ 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 **方法:返回是否能够移动 */ public boolean iscardcanmove(){ return this.canmove; } /** **返回值:int **方法:获得card的内容值 */ public int getcardvalue(){ return value; } /** **返回值:int **方法:获得card的类型 */ public int getcardtype(){ return type; } }
spidermenubar.java类
import javax.swing.jmenubar; import javax.swing.jmenu; import javax.swing.jmenuitem; import javax.swing.jradiobuttonmenuitem; import javax.swing.buttongroup; public class spidermenubar extends jmenubar{ //生成spider框架对象 spider main = null; //生成菜单组 jmenu jnewgame = new jmenu("游戏"); jmenu jhelp = new jmenu("帮助"); //生成菜单项 jmenuitem jitemabout = new jmenuitem("关于"); jmenuitem jitemopen = new jmenuitem("开局"); jmenuitem jitemplayagain = new jmenuitem("重新发牌"); //生成单选框 jradiobuttonmenuitem jrmitemeasy = new jradiobuttonmenuitem("简单:单一花色"); jradiobuttonmenuitem jrmitemnormal = new jradiobuttonmenuitem("中级:双花色"); jradiobuttonmenuitem jrmitemhard = new jradiobuttonmenuitem("高级:四花色");; jmenuitem jitemexit = new jmenuitem("退出"); jmenuitem jitemvalid = new jmenuitem("显示可行操作"); /** **构造函数,生成jmenubar的图形界面 */ public spidermenubar(spider spider){ this.main = spider; /** **初始化“游戏”菜单栏 */ jnewgame.add(jitemopen); jnewgame.add(jitemplayagain); jnewgame.add(jitemvalid); jnewgame.addseparator(); jnewgame.add(jrmitemeasy); jnewgame.add(jrmitemnormal); jnewgame.add(jrmitemhard); jnewgame.addseparator(); jnewgame.add(jitemexit); buttongroup group = new buttongroup(); group.add(jrmitemeasy); group.add(jrmitemnormal); group.add(jrmitemhard); jhelp.add(jitemabout); this.add(jnewgame); this.add(jhelp); //为组件添加事件监听并实现 //“开局” jitemopen.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { main.newgame(); } }); //“重新发牌” jitemplayagain.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { if(main.getc() < 60){ main.deal(); } } }); //"显示可行操作" jitemvalid.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { new show().start(); } }); //“退出” jitemexit.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { main.dispose(); system.exit(0); } }); //“简单级别”默认已选 jrmitemeasy.setselected(true); //“简单级别” jrmitemeasy.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { main.setgrade(spider.easy); main.initcards(); main.newgame(); } }); //“中级” jrmitemnormal.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { main.setgrade(spider.natural); main.initcards(); main.newgame(); } }); //“高级” jrmitemhard.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { main.setgrade(spider.hard); main.initcards(); main.newgame(); } }); jnewgame.addmenulistener(new javax.swing.event.menulistener() { public void menuselected(javax.swing.event.menuevent e) { if(main.getc() < 60){ jitemplayagain.setenabled(true); } else{ jitemplayagain.setenabled(false); } } public void menudeselected(javax.swing.event.menuevent e) {} public void menucanceled(javax.swing.event.menuevent e) {} }); //“关于” jitemabout.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent e) { new aboutdialog(); } }); } /** **构造线程:显示可以执行的操作 */ class show extends thread{ public void run(){ main.showenableoperator(); } } }
spider.java 类
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class spider extends jframe{ //整型变量,表示难度等级为:简单 public static final int easy = 1; //整型变量,表示难度等级为:普通 public static final int natural = 2; //整型变量,表示难度等级为:难 public static final int hard = 3; //设定初始难度等级为简单 private int grade = spider.easy; private container pane = null; //生成纸牌数组 private pkcard cards[] = new pkcard[104]; private jlabel clicklabel = null; private int c = 0; private int n = 0; private int a = 0; private int finish = 0; hashtable table = null; private jlabel groundlabel[] = null; public static void main(string[] args){ spider spider = new spider(); spider.setvisible(true); } /** **构造函数 */ public spider(){ //改变系统默认字体 font font = new font("dialog", font.plain, 12); java.util.enumeration keys = uimanager.getdefaults().keys(); while (keys.hasmoreelements()) { object key = keys.nextelement(); object value = uimanager.get(key); if (value instanceof javax.swing.plaf.fontuiresource) { uimanager.put(key, font); } } settitle("蜘蛛牌"); setdefaultcloseoperation(javax.swing.jframe.exit_on_close); //设置框架的大小 setsize(1024, 742); //生成spidermenubar对象,并放置在框架之上 setjmenubar(new spidermenubar(this)); pane = this.getcontentpane(); //设置背景颜色 pane.setbackground(new color(0, 112, 26)); //将布局管理器设置成为null pane.setlayout(null); clicklabel = new jlabel(); clicklabel.setbounds(883, 606, 121, 96); pane.add(clicklabel); clicklabel.addmouselistener(new mouseadapter(){ public void mousereleased(mouseevent me){ if (c < 60){ spider.this.deal(); } } }); this.initcards(); this.randomcards(); this.setcardslocation(); groundlabel = new jlabel[10]; int x = 20; for (int i = 0; i < 10; i++) { groundlabel[i] = new jlabel(); groundlabel[i] .setborder(javax.swing.borderfactory .createetchedborder(javax.swing.border.etchedborder.raised)); groundlabel[i].setbounds(x, 25, 71, 96); x += 101; this.pane.add(groundlabel[i]); } this.setvisible(true); this.deal(); this.addkeylistener(new keyadapter(){ class show extends thread{ public void run(){ spider.this.showenableoperator(); } } public void keypressed(keyevent e){ if (finish != 8) if (e.getkeycode() == keyevent.vk_d && c < 60){ spider.this.deal(); } else if (e.getkeycode() == keyevent.vk_m){ new show().start(); } } }); } /** **开始新游戏 */ public void newgame(){ this.randomcards(); this.setcardslocation(); this.setgroundlabelzorder(); this.deal(); } /** **返回值:int **返回牌的数量 */ public int getc(){ return c; } /** **设置等级 */ public void setgrade(int grade){ this.grade = grade; } /** **纸牌初始化 */ public void initcards(){ //如果纸牌已被赋值,即将其从框架的面板中移去 if (cards[0] != null){ for (int i = 0; i < 104; i++){ pane.remove(cards[i]); } } int n = 0; //通过难度等级,为n赋值 if (this.grade == spider.easy){ n = 1; } else if (this.grade == spider.natural){ n = 2; } else{ n = 4; } //为card赋值 for (int i = 1; i <= 8; i++){ for (int j = 1; j <= 13; j++){ cards[(i - 1) * 13 + j - 1] = new pkcard((i % n + 1) + "-" + j, this); } } //随机纸牌初始化 this.randomcards(); } /** **纸牌随机分配 */ public void randomcards(){ pkcard temp = null; //随机生成牌号 for (int i = 0; i < 52; i++){ int a = (int) (math.random() * 104); int b = (int) (math.random() * 104); temp = cards[a]; cards[a] = cards[b]; cards[b] = temp; } } /** **设置还原 */ public void setna(){ a = 0; n = 0; } /** **设置纸牌的位置 */ public void setcardslocation(){ table = new hashtable(); c = 0; finish = 0; n = 0; a = 0; int x = 883; int y = 580; //初始化待展开的纸牌 for (int i = 0; i < 6; i++){ for (int j = 0; j < 10; j++){ int n = i * 10 + j; pane.add(cards[n]); //将card转向背面 cards[n].turnrear(); //将card放在固定的位置上 cards[n].moveto(new point(x, y)); //将card的位置及相关信息存入 table.put(new point(x, y), cards[n]); } x += 10; } x = 20; y = 45; //初始化表面显示的纸牌 for (int i = 10; i > 5; i--){ for (int j = 0; j < 10; j++){ int n = i * 10 + j; if (n >= 104) continue; pane.add(cards[n]); cards[n].turnrear(); cards[n].moveto(new point(x, y)); table.put(new point(x, y), cards[n]); x += 101; } x = 20; y -= 5; } } /** **返回值:void **方法:显示可移动的操作 */ public void showenableoperator(){ int x = 0; out: while (true){ point point = null; pkcard card = null; do{ if (point != null){ n++; } point = this.getlastcardlocation(n); while (point == null){ point = this.getlastcardlocation(++n); if (n == 10) n = 0; x++; if (x == 10) break out; } card = (pkcard) this.table.get(point); } while (!card.iscardcanmove()); while (this.getpreviouscard(card) != null && this.getpreviouscard(card).iscardcanmove()){ card = this.getpreviouscard(card); } if (a == 10){ a = 0; } for (; a < 10; a++){ if (a != n){ point p = null; pkcard c = null; do{ if (p != null){ a++; } p = this.getlastcardlocation(a); int z = 0; while (p == null){ p = this.getlastcardlocation(++a); if (a == 10) a = 0; if (a == n) a++; z++; if (z == 10) break out; } c = (pkcard) this.table.get(p); } while (!c.iscardcanmove()); if (c.getcardvalue() == card.getcardvalue() + 1){ card.flashcard(card); try{ thread.sleep(800); } catch (interruptedexception e){ e.printstacktrace(); } c.flashcard(c); a++; if (a == 10){ n++; } break out; } } } n++; if (n == 10){ n = 0; } x++; if (x == 10){ break out; } } } /* **返回值:void **方法:游戏运行 */ public void deal() { this.setna(); //判断10列中是否空列 for (int i = 0; i < 10; i++){ if (this.getlastcardlocation(i) == null){ joptionpane.showmessagedialog(this, "有空位不能发牌!", "提示", joptionpane.warning_message); return; } } int x = 20; for (int i = 0; i < 10; i++){ point lastpoint = this.getlastcardlocation(i); //这张牌应“背面向上” if (c == 0){ lastpoint.y += 5; } //这张牌应“正面向上” else{ lastpoint.y += 20; } table.remove(cards[c + i].getlocation()); cards[c + i].moveto(lastpoint); table.put(new point(lastpoint), cards[c + i]); cards[c + i].turnfront(); cards[c + i].setcanmove(true); //将组件card移动到容器中指定的顺序索引。 this.pane.setcomponentzorder(cards[c + i], 1); point point = new point(lastpoint); if (cards[c + i].getcardvalue() == 1){ int n = cards[c + i].whichcolumnavailable(point); point.y -= 240; pkcard card = (pkcard) this.table.get(point); if (card != null && card.iscardcanmove()){ this.havefinish(n); } } x += 101; } c += 10; } /* **返回值:pkcard对象 **方法:获得card上面的那张牌 */ public pkcard getpreviouscard(pkcard card){ point point = new point(card.getlocation()); point.y -= 5; card = (pkcard) table.get(point); if (card != null){ return card; } point.y -= 15; card = (pkcard) table.get(point); return card; } /** **返回值:pkcard对象 **方法:取得card下面的一张牌 */ public pkcard getnextcard(pkcard card){ point point = new point(card.getlocation()); point.y += 5; card = (pkcard) table.get(point); if (card != null) return card; point.y += 15; card = (pkcard) table.get(point); return card; } /** **返回值:point对象 **方法:取得第column列最后一张牌的位置 */ public point getlastcardlocation(int column){ point point = new point(20 + column * 101, 25); pkcard card = (pkcard) this.table.get(point); if (card == null) return null; while (card != null){ point = card.getlocation(); card = this.getnextcard(card); } return point; } public point getgroundlabellocation(int column){ return new point(groundlabel[column].getlocation()); } /* **返回值:void **方法:放置groundlable组件 */ public void setgroundlabelzorder(){ for (int i = 0; i < 10; i++){ //将组件groundlable移动到容器中指定的顺序索引。顺序(105+i)确定了绘制组件的顺序;具有最高顺序的组件将第一个绘制,而具有最低顺序的组件将最后一个绘制。在组件重叠的地方,具有较低顺序的组件将覆盖具有较高顺序的组件。 pane.setcomponentzorder(groundlabel[i], 105 + i); } } /* **返回值:void **方法:判断纸牌的摆放是否完成 */ public void havefinish(int column){ point point = this.getlastcardlocation(column); pkcard card = (pkcard) this.table.get(point); do{ this.table.remove(point); card.moveto(new point(20 + finish * 10, 580)); //将组件移动到容器中指定的顺序索引。 pane.setcomponentzorder(card, 1); //将纸牌新的相关信息存入hashtable this.table.put(card.getlocation(), card); card.setcanmove(false); point = this.getlastcardlocation(column); if (point == null) card = null; else card = (pkcard) this.table.get(point); } while (card != null && card.iscardcanmove()); finish++; //如果8付牌全部组合成功,则显示成功的对话框 if (finish == 8){ joptionpane.showmessagedialog(this, "恭喜你,顺利通过!", "成功", joptionpane.plain_message); } if (card != null){ card.turnfront(); card.setcanmove(true); } } }
图片资源这里就不方便贴出来了,想要源码的直接点击的链接地址
到此这篇关于基于java实现经典蜘蛛纸牌游戏的文章就介绍到这了,更多相关java蜘蛛纸牌内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!