admin 管理员组

文章数量: 1086019


2024年2月28日发(作者:spidergram什么意思)

目 录

1. 选题目的与系统概述„„„„„„„„„„„„„„„„2

2. 系统可行性分析与相关技术背景分析„„„„„„„„„3

2.1系统可行性分析„„„„„„„„„„„„„„„„„„„3

2.2相关技术背景分析„„„„„„„„„„„„„„„„„„3

3. 系统设计与关键技术„„„„„„„„„„„„„„„„4

3.1系统设计„„„„„„„„„„„„„„„„„„„„„„4

3.2关键技术„„„„„„„„„„„„„„„„„„„„„„5

4.系统测试与操作实施方法„„„„„„„„„„„„„„9

4.1进入游戏运行界面„„„„„„„„„„„„„„„„„„9

4.2 游戏的具体的操作过程„„„„„„„„„„„„„„„„9

4.3进入“帮助”菜单„„„„„„„„„„„„„„„„„„10

4.4游戏中的异常处理情况„„„„„„„„„„„„„„„„11

5.系统设计总结与回顾„„„„„„„„„„„„„„„12

6.附录„„„„„„„„„„„„„„„„„„„„„„„13

6.1 „„„„„„„„„„„„„„„„„13

6.2 „„„„„„„„„„„„„„„„„„„„14

6.3 „„„„„„„„„„„„„„„„„„„„„20

6.4 „„„„„„„„„„„„„„„„„29

1

1.选题目的与系统概述

随着经济全球一体化的逐步加剧,人们的生活节奏越来越快。蜘蛛纸牌游戏的一种,旨在让工作学习之后疲惫的玩家有一个轻愉快的放松方式。与普通的纸牌相比,降低了一点难度。游戏简单级别由8组A-K的清一色黑桃牌面组成,取消了红黑相间的游戏规则,更突然出强化了玩家在安排事物方面的能力。

蜘蛛纸牌游戏实现了与Windows系列操作系统中自带的蜘蛛纸牌游戏类似的功能。该游戏程序中采用Windows系列中游戏的图片,运用Java语言设计了线程、鼠标事件、纸牌的装载、图形界面的构造、组件监听接口的实现、生成蜘蛛纸牌游戏的框架、实现游戏中的方法,是一种休闲益智游戏。

2

2.系统可行性分析与相关技术背景分析

2.1系统可行性分析

蜘蛛纸牌游戏规则是:

(1) 将电脑分发给你的牌按照相同的花色又打至小排列起来,直到桌面上的牌全都消失。

(2) 可执行的牌必须为明牌

(3) 单牌移动规则

(4) 多拍移动规则

游戏分三个等级:(1)简答:单一花色;(2)中级:双花色;(3)四花色。

2.2相关技术背景分析

2.2.1蜘蛛纸牌游戏是基于Eclipse平台的Java程序。Eclipse的背景如下:

Eclipse最初是IBM公司的一个软件产品。2001年11月7日发布Eclipse 1.0版本,以后几乎每年都更新一次版本,到2011年6月22日,发布最新版本Eclipse 3.7(INDIGO)。尽管Eclipse是使用Java语言开发的,但它的用途并不仅限于Java语言。例如,支持诸如C/C++、COBOL和Eiffel等编程语言的插件已经可用。Eclipse平台的主要任务是为开发者提供能够使用各种工具无缝集成的机制和需要遵守的规则。Eclipse的特点是:(1)基于Java、开发源码的可扩展平台;(2)一个免费得Java IDE(Java 集成开发环境)。

2.2.2本课程设计是在最新版本Eclipse 3.7(INDIGO)下运行,进入界面如下:

3

3.系统设计与关键技术

3.1系统设计

3.1.1蜘蛛游戏可以分为四个模块,它们之间的关系如下:

(1)游戏开始的主函数,调用中及实现游戏中的方法,即调用

(2)SpiderMenuBar调用游戏框架和游戏菜单,即调用和

(3) 纸牌的调用和移动位置及纸牌属性,即调用

3.1.2蜘蛛游戏的流程图如图3-1所示。

纸牌的调用和移动位置及纸牌属性游戏菜单中子菜单栏蜘蛛纸牌游戏的“帮助”

调用游戏框架和游戏菜单

游戏开始主函数调用及实现游戏中的方法

图3-1

3.1.3蜘蛛游戏可以分为四个模块各项功能如下:

(1) 其作用是生成游戏的“帮助”下拉菜单

(2) 其作用是定义纸牌属性,包括名称、位置等相关信息。并管理所有可执行操作,如启动Flash线程、点击鼠标、释放鼠标、放置纸牌、判断可用列、用鼠标拖动纸牌、移动(x,y)个位置、令纸牌显示正面、令纸牌显示背面、将纸牌移动到点point、判断纸牌是否移动、判断card是否是正面等 。

(3)其作用是设置游戏等级、框架属性、纸牌初始化、异常及成功处理等

4

(4) 其作用是添加“游戏”下拉列表所有菜单选项,实现各组件的事件侦听 。主要包括三个模块:图形用户界面的构建、组件监听接口的实现、显示可执行操作的线程。

3.2关键技术

3.2.1 中的“关于”窗口

private JTextArea jt1 = new JTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。"); //用来显示“游戏规则”对应的文本框

private JTextArea jt2 = new JTextArea("该游戏中,纸牌的图片来自于Windows XP的纸牌游戏,图片权属于原作者所有!"); //用来显示“声明”对应的文本框

public void flashCard(PKCard card){

new Flash(card).start(); //不停的获得下一张牌,直到完成

if(tCard(card) != null){

ard(tCard(card)); }}

class Flash extends Thread{

3.2.2 当有空位的时候,启动Flash线程,继续进行发牌。

private PKCard card = null;

}

public Flash(PKCard card){

= card;

3.2.3 中点击“游戏”->“显示可行操作”后,可以连续的纸牌会闪烁一下,即一张是图片本身一张是白色图片。这里有线程的run()方法来实现。

public void run(){

boolean is = false;

ImageIcon icon = new ImageIcon("images/");

for (int i = 0; i < 4; i++){

try{

(200);

}

5

catch (InterruptedException e){

}

tackTrace();

if (is){ ont();

is = !is; }

else{ n(icon);

is = !is;

}// 根据当前外观将card的UI属性重置

UI();}}}

3.2.4 放置纸牌

public void setNextCardLocation(Point point){

PKCard card = tCard(this);

if (card != null){

if (point == null){

}

else{

point = new Point(point);

point.y += 20;

tCardLocation(point);

point.y -= 20;

(ation());

ation(point);

(ation(), card);

int = ation();}}}

tCardLocation(null);

(ation());

ation(int);

(int, card);

3.2.5 判断可用列

6

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 = tCardLocation(a);

if (p == null) p = undLabelLocation(a);

b = y - p.y;

if (b <= -96 || b >= 96){

a = -1; }}

return a; }

3.2.6 中对鼠标事件监听程序进行初始布局

seListener(new MouseAdapter(){

public void mouseReleased(MouseEvent me){

if (c < 60){

();

}} });

3.2.7 “游戏”下拉菜单中游戏级别设置

ionListener(new Listener() {

public void actionPerformed(Event e) {

de();

rds();

e();

}}); //“简单级别”默认设置

ionListener(new Listener() {

public void actionPerformed(Event e) {

de(L);

rds();

e();

7

} });//“中级”

ionListener(new Listener() {

public void actionPerformed(Event e) {

de();

rds();

e();

}

});//“高级”

uListener(new stener() {

public void menuSelected(ent e) {

if(() < 60){

bled(true);

}

else{

bled(false); } }

public void menuDeselected(ent e) {}

public void menuCanceled(ent e) {} });

3.2.8 "中显示可行操作"功能控制

ionListener(new Listener() {

public void actionPerformed(Event e) {

new Show().start();

}});

3.2.9 为组件添加事件监听addActionListener并实现

ionListener(new Listener() {

public void actionPerformed(Event e) {

e();

}

});//“开局”

3.2.10“重新发牌”功能控制

ionListener(new Listener() {

public void actionPerformed(Event e) {

if(() < 60){

();

}} });

8

4.系统测试与操作实施方法

4.1进入游戏运行界面。如图4-1所示。

图4-1“游戏”下拉菜单列表

4.2 游戏的具体的操作过程

(1)开始时,为“简单”级别,花色为黑桃,如图4-2所示。

(2)将纸牌从大到小将纸牌排列,当从K到A排好序后,游戏将所有牌归并到界面的左下方,如图4-2到图4-5所示。

(3)当第八次归并完毕后,游戏自动弹出“成功”对话框,如图4-6所示。

(4)设置为“中级”的开局后的界面,如图4-7 所示。

(5)设置为“高级”的开局后的界面,如图4-8所示。

图4-2进入“开局”初始化界面 图4-3 将纸牌从大到小排好后,归并到

左下角

9

图4-4 第二次归并后 图4-5第三次归并后

图4-6 第八次归并完毕后,游戏自动弹出“成功”对话框

图4-7 设置为“中级”的开局后界面 图4-8设置为“高级”的开局后界面

4.3进入“帮助”菜单

点击“帮助”下拉菜单的“关于”会弹出“蜘蛛牌”对话框,如图4-9所示。单击“游戏规则”如图4-10所示。单击“声明”图4-11所示。

10

图4-9“帮助”下拉菜单“关于” 图4-10“游戏规则”下的编辑框

图4-11“声明”下的编辑框

4.4游戏中的异常处理情况

游戏过程中,如果移动纸牌时,中间有空位时,单击发牌,会弹出“提示对话框”。如图4-12所示。

图4-12纸牌的异常处理

11

5.系统设计总结与回顾

蜘蛛纸牌游戏实现了与Windows系列操作系统中自带的蜘蛛纸牌游戏类似的功能。该游戏程序中采用Windows系列中游戏的图片,运用Java语言设计了线程、鼠标事件、纸牌的装载、图形界面的构造、组件监听接口的实现、生成蜘蛛纸牌游戏的框架、实现游戏中的方法,是一种休闲益智游戏。

通过本次课程设计对蜘蛛纸牌游戏的深入研究和分析,对于Eclipse 3.7有了深入的学习和了解,并对其应用有了一定的掌握。对Java语言在实际中的应用有了切身的体会,并将所学的知识在实践中得到应用,同时也巩固了Java中所学的理论知识,对于不了解的知识,又有了新的学习。同学们对于不懂的内容同学查阅相关资料以及老师们的帮助,还有同学间的相互讨论,增加了团队合作的能力。在分析源码的过程中,有些不熟悉的代码,参考了相关资料,以及老师和同学们的帮助,使得问题得以解决,在此向向帮助过我们的老师和同学表示感谢!

12

6.附 录

源代码:

6.1

import .*;

import .*;

/*“关于”窗口*/

public class AboutDialog extends JDialog

{

private static final long serialVersionUID = 1L;//Add default serial version

ID

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);//设置是否可调整大小,初始值设为false

setDefaultCloseOperation (E_ON_CLOSE);

//setDefaultCloseOperation()是用来设定窗口被关闭时候(比如点击了右上角的"x")的行为的。

Container c = tentPane();

e(300,300);//设置“游戏规则”下文本框大小

e(300,300);//设置“声明”下文本框的大小

table(false);//设置文本框是否可以编辑,这里设置为false

table(false);

eWrap(true);

eWrap(true);

t(new Font("楷体_GB2312", , 20));//中文字体:楷体_GB2312,字形:黑体,字号:13

eground();//设置“游戏规则”下文本的字体颜色,可以设置为black(黑)、blue(蓝)、 cyan(蓝绿)、darkGray(深灰)、gray(灰)、green(绿)、lightGray(浅灰)、orange(橘黄)、pink(粉红)、red(红)、white(白)、yellow(黄)等13

中13中标准颜色之一

t(new Font("楷体_GB2312", , 20));

eground();

(jt1);

(jt2);

e(300,200);

("游戏规则", null, jPanel1, null);

("声明", null, jPanel2, null);

(jTabbedPane);

(jMainPane);

pack();

ible(true);

}}

6.2

import .*;

import .*;

import .*;

public class PKCard extends JLabel implements MouseListener,

MouseMotionListener{

private static final long serialVersionUID = 1L;//Add default serial version

ID

//纸牌的位置

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(tCard(card) != null){

ard(tCard(card));

}

}

class Flash extends Thread{

private PKCard card = null;

public Flash(PKCard card){

14

= card;

}

/*线程的run()方法--为纸牌的正面设置白色图片*/

public void run(){

boolean is = false;

ImageIcon icon = new ImageIcon("images/");

for (int i = 0; i < 4; i++){

try{

(200);

}

catch (InterruptedException e){

tackTrace();

}

if (is){

ont();

is = !is;

}

else{

n(icon);

is = !is;

}// 根据当前外观将card的UI属性重置

UI();

}}}

/*点击鼠标*/

public void mousePressed(MouseEvent mp){

point = nt();

();

usCard = viousCard(this);

}

/*释放鼠标*/

public void mouseReleased(MouseEvent mr){

Point point = ((JLabel) rce()).getLocation();

//判断可行列

int n = olumnAvailable(point);

if (n == -1 || n == olumnAvailable(int)){

tCardLocation(null);

(ation());

ation(int);

(int, this);

return;

}

point = tCardLocation(n);

boolean isEmpty = false;

PKCard card = null;

if (point == null){

point = undLabelLocation(n);

15

isEmpty = true;

}

else{

card = (PKCard) (point);

}

if (isEmpty || ( + 1 == dValue())){

point.y += 40;

if (isEmpty) point.y -= 20;

tCardLocation(point);

(ation());

point.y -= 20;

ation(point);

(point, this);

int = point;

if (usCard != null){

ont();

Move(true);

}

Move(true);

}

else{

tCardLocation(null);

(ation());

ation(int);

(int, this);

return;

}

point = tCardLocation(n);

card = (PKCard) (point);

if (dValue() == 1){

point.y -= 240;

card = (PKCard) (point);

if (card != null && CanMove()){

nish(n);

}

}

}

/*

**方法:放置纸牌

*/

public void setNextCardLocation(Point point){

PKCard card = tCard(this);

if (card != null){

if (point == null){

tCardLocation(null);

(ation());

16

ation(int);

(int, card);

}

else{

point = new Point(point);

point.y += 20;

tCardLocation(point);

point.y -= 20;

(ation());

ation(point);

(ation(), card);

int = ation();

}

}

}

/**

**返回值: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 = tCardLocation(a);

if (p == null) p = undLabelLocation(a);

b = y - p.y;

if (b <= -96 || b >= 96){

a = -1;

}

}

return a;

}

17

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 = nt();

x = p.x - point.x;

y = p.y - point.y;

(x, y);

}

}

/**

**返回值:void

**方法:移动(x,y)个位置

*/

public void moving(int x, int y){

PKCard card = tCard(this);

Point p = ation();

//将组件移动到容器中指定的顺序索引。

ponentZOrder(this, 1);

//在Hashtable中保存新的节点信息

(p);

p.x += x;

p.y += y;

ation(p);

(p, this);

if (card != null) (x, y);

}

public void mouseMoved(MouseEvent arg0){

}

/**

**构造方法

*/

public PKCard(String name, Spider spider){

super();

= new Integer(ing(0, 1)).intValue();

= new Integer(ing(2)).intValue();

= name;

= spider;

18

= tentPane();

seListener(this);

seMotionListener(this);

n(new ImageIcon("images/"));//背面牌

e(71, 96);//纸牌的大小

ible(true);

}

/**

**返回值:void

**方法:令纸牌显示正面(纸牌将全部归位到游戏界面的左下角)

*/

public void turnFront(){

n(new ImageIcon("images/" + name + ".gif"));

t = true;

}

/**

**返回值:void

**方法:令纸牌显示背面

*/

public void turnRear(){

n(new ImageIcon("images/"));

t = false;

e = false;

}

/**

**返回值:void

**方法:将纸牌移动到点point

*/

public void moveto(Point point){

ation(point);

int = point;

}

/**

**返回值:void

**方法:判断牌是否能移动

*/

public void setCanMove(boolean can){

e = can;

PKCard card = viousCard(this);

if (card != null && Front()){

if (!can){

if (!CanMove()){

return;

}

else{

Move(can);

19

}

}

else{

if ( + 1 == dValue()

&& == dType()){

Move(can);

}

else{

Move(false);

}}}}

/*返回值:boolean--方法:判断card是否是正面*/

public boolean isCardFront(){

return t;

}

/*返回值:boolean--方法:返回是否能够移动*/

public boolean isCardCanMove(){

return e;

}

/*返回值:int--方法:获得card的内容值*/

public int getCardValue(){

return value;

}

/*返回值:int--方法:获得card的类型*/

public int getCardType(){

return type;

}}

6.3

import .*;

import .*;

import .*;

import .*;

public class Spider extends JFrame{

private static final long serialVersionUID = 1L;

//整型变量,表示难度等级为:简单

public static final int EASY = 1;

//整型变量,表示难度等级为:中级

public static final int NATURAL = 2;

//整型变量,表示难度等级为:高级

public static final int HARD = 3;

//设定初始难度等级为简单

private int grade = ;

private Container pane = null;

//生成纸牌数组

private PKCard cards[] = new PKCard[104];

private JLabel clickLabel = null;

20

private int c = 0;

private int n = 0;

private int a = 0;

private int finish = 0;

Hashtable table = null;//Hashtable

private JLabel groundLabel[] = null;

public static void main(String[] args){

Spider spider = new Spider();

ible(true);

}

/**

**构造方法

*/

public Spider(){

setTitle("蜘蛛牌");

setDefaultCloseOperation(_ON_CLOSE);

//设置框架的大小

setSize(1024, 742);

//生成SpiderMenuBar对象,并放置在框架之上

setJMenuBar(new SpiderMenuBar(this));

pane = tentPane();

//设置背景颜色

kground(new Color(0, 112, 26));//0~255之间

//将布局管理器设置成为null

out(null);

clickLabel = new JLabel();

nds(883, 606, 121, 96);

(clickLabel);

seListener(new MouseAdapter(){

public void mouseReleased(MouseEvent me){

if (c < 60){

();

}

}

});//对鼠标事件监听程序进行初始布局

rds();

Cards();

dsLocation();

groundLabel = new JLabel[10];

int x = 20;//因为纸牌按顺序进行摆放,而每一次放的位置都需要进行累加所以在不同位置的纸牌摆放可以引入int x=20;增加一个累加的位置数值变量

for (int i = 0; i < 10; i++)

{

groundLabel[i] = new JLabel();

groundLabel[i] .setBorder(Factory

21

.createEtchedBorder());

groundLabel[i].setBounds(x, 25, 71, 96);

x += 101;

(groundLabel[i]);

}

ible(true);

();

Listener(new KeyAdapter(){

class Show extends Thread{

public void run(){

ableOperator();

}

}

public void keyPressed(KeyEvent e){

if (finish != 8) if (Code() == _D && c < 60){

();

}

else if (Code() == _M){

new Show().start();

}

}

});

}

/**

**开始新游戏

*/

public void newGame(){

Cards();

dsLocation();

undLabelZOrder();

();

}

/**

**返回值:int

**返回牌的数量

*/

public int getC(){

return c;

}

/**

**设置等级

*/

public void setGrade(int grade){

= grade;

}

/**

22

**纸牌初始化

*/

public void initCards(){

//如果纸牌已被赋值,即将其从框架的面板中移去

if (cards[0] != null){

for (int i = 0; i < 104; i++){

(cards[i]);

}

}

int n = 0;

//通过难度等级,为n赋值

if ( == ){

n = 1;

}

else if ( == L){

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);

}

}

//随机纸牌初始化

Cards();

}

/**

**纸牌随机分配

*/

public void randomCards(){

PKCard temp = null;

//随机生成牌号

for (int i = 0; i < 52; i++){

int a = (int) (() * 104);

int b = (int) (() * 104);

temp = cards[a];

cards[a] = cards[b];

cards[b] = temp;

}

}

/**

23

**设置还原

*/

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;

(cards[n]);

//将card转向背面,因为纸牌未点击的时候,纸牌是背面显示,要对其进行初始化

cards[n].turnRear();

//将card放在固定的位置上

cards[n].moveto(new Point(x, y));

//将card的位置及相关信息存入

(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;

(cards[n]);

cards[n].turnRear();

cards[n].moveto(new Point(x, y));

(new Point(x, y), cards[n]);

x += 101;

}

24

x = 20;

y -= 5;

}

}

/**

**返回值:void

**方法:显示可移动的操作(点击可行性操作后,可以连续的纸牌会闪一下,即一下是图片本身,一张是一张白色图片。确定有可行性操作之后SpiderMenuBar对象会调用PKCard对象中的run()来实现提示)

*/

public void showEnableOperator(){

int x = 0;

out: while (true){

Point point = null;

PKCard card = null;

do{

if (point != null){

n++;

}

point = tCardLocation(n);

while (point == null){

point = tCardLocation(++n);

if (n == 10) n = 0;

x++;

if (x == 10) break out;

}

card = (PKCard) (point);

}

while (!CanMove());

while (viousCard(card) != null

&& viousCard(card).isCardCanMove()){

card = viousCard(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 = tCardLocation(a);

int z = 0;

25

while (p == null){

p = tCardLocation(++a);

if (a == 10) a = 0;

if (a == n) a++;

z++;

if (z == 10) break out; }

c = (PKCard) (p);

}

while (!CanMove());

if (dValue() == dValue() + 1){

ard(card);

try{ (800); }

catch (InterruptedException e){ tackTrace();}

ard(c);

a++;

if (a == 10){

n++;

}

break out;

}

}

}

n++;

if (n == 10){

n = 0;

}

x++;

if (x == 10){

break out;

}

}

}

/*

**返回值:void

**方法:游戏运行

*/

public void deal()

{

();

//判断10列中是否空列

for (int i = 0; i < 10; i++){

if (tCardLocation(i) == null){

ssageDialog(this, "有空位不能发牌!", "提示",

G_MESSAGE);

26

return;

}

}

for (int i = 0; i < 10; i++){

Point lastPoint = tCardLocation(i);

//这张牌应“背面向上”

if (c == 0){

lastPoint.y += 5;

}

//这张牌应“正面向上”

else{

lastPoint.y += 20;

}

(cards[c + i].getLocation());

cards[c + i].moveto(lastPoint);

(new Point(lastPoint), cards[c + i]);

cards[c + i].turnFront();

cards[c + i].setCanMove(true);

//将组件card移动到容器中指定的顺序索引。

ponentZOrder(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) (point);

if (card != null && CanMove()){

nish(n);

}

}

}

c += 10;

}

/*

**返回值:PKCard对象

**方法:获得card上面的那张牌

*/

public PKCard getPreviousCard(PKCard card){

Point point = new Point(ation());

point.y -= 5;

card = (PKCard) (point);

if (card != null){

return card;

}

27

point.y -= 15;

card = (PKCard) (point);

return card;

}

/**

**返回值:PKCard对象

**方法:取得card下面的一张牌

*/

public PKCard getNextCard(PKCard card){

Point point = new Point(ation());

point.y += 5;

card = (PKCard) (point);

if (card != null)

return card;

point.y += 15;

card = (PKCard) (point);

return card;

}

/**

**返回值:Point对象

**方法:取得第column列最后一张牌的位置(如果最后一张牌的位置是在该列摆放纸牌的最后一个位置则可以继续发牌)

*/

public Point getLastCardLocation(int column){

Point point = new Point(20 + column * 101, 25);

PKCard card = (PKCard) (point);

if (card == null) return null;

while (card != null){//如果最后一张牌的位置在发牌的列的位置上则可以发牌,即card!=null

point = ation();

card = tCard(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)确定了绘制组件的顺序;具有最高顺序的组件将第一个绘制,而具有最低顺序的组件将最后一个绘制。在组件重叠的地方,具有较低顺序的组件将覆盖具有较高顺序的组件。

28

ponentZOrder(groundLabel[i], 105 + i);

}

}

/*

**返回值:void

**方法:判断纸牌的摆放是否完成

*/

public void haveFinish(int column){

Point point = tCardLocation(column);

PKCard card = (PKCard) (point);

do{

(point);

(new Point(20 + finish * 10, 580));

//将组件移动到容器中指定的顺序索引。

ponentZOrder(card, 1);

//将纸牌新的相关信息存入Hashtable

(ation(), card);

Move(false);

point = tCardLocation(column);

if (point == null)

card = null;

else

card = (PKCard) (point);

}

while (card != null && CanMove());

finish++;

//如果8付牌全部组合成功,则显示成功的对话框

if (finish == 8){

ssageDialog(this, "恭喜你,顺利通过!",

",

_MESSAGE);

}

if (card != null){

ont();

Move(true);

}

}}

6.4

import ar;

import ;

import tem;

import ButtonMenuItem;

import Group;

public class SpiderMenuBar extends JMenuBar{

private static final long serialVersionUID = 1L

29

"成功

Spider main = null; //生成spider框架对象

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){

= spider;

/**

**初始化“游戏”菜单栏

*/

(jItemOpen);

(jItemPlayAgain);

(jItemValid);

arator();

(jRMItemEasy);

(jRMItemNormal);

(jRMItemHard);

arator();

(jItemExit);

ButtonGroup group = new ButtonGroup();

(jRMItemEasy);

(jRMItemNormal);

(jRMItemHard);

(jItemAbout);

(jNewGame);

(jHelp);

30

//为组件添加事件监听addActionListener并实现

//“开局”

ionListener(new Listener() {

public void actionPerformed(Event e) {

e();

}

});

//“重新发牌”

ionListener(new Listener()

{

public void actionPerformed(Event e) {

if(() < 60){

();

}

}

});

//"显示可行操作"

ionListener(new Listener() {

public void actionPerformed(Event e) {

new Show().start();

}

});

//“退出”

ionListener(new Listener() {

public void actionPerformed(Event e) {

e();

(0);

}

});

//“简单级别”默认已选

ected(true);

//“简单级别”

ionListener(new Listener() {

public void actionPerformed(Event e) {

de();

rds();

e();

}

});

//“中级”

ionListener(new Listener() {

31

public void actionPerformed(Event e) {

de(L);

rds();

e();

}

});

//“高级”

ionListener(new Listener() {

public void actionPerformed(Event e) {

de();

rds();

e();

}

});

uListener(new stener() {

public void menuSelected(ent e) {

if(() < 60){

bled(true);

}

else{

bled(false);

}

}

public void menuDeselected(ent e) { }

public void menuCanceled(ent e) {}

});

//“关于”下拉列表

ionListener(new Listener() {

public void actionPerformed(Event e) {

new AboutDialog();

}

});

}

/**

**构造线程:显示可以执行的操作

*/

class Show extends Thread{

public void run(){

ableOperator();

}

}

}

32


本文标签: 纸牌 游戏 蜘蛛 方法 组件