版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、武 汉 轻 工 大 学数学与计算机学院 Java 程序设计 课程设计报告专 业: 信息与计算科学 班 级: 信计1402 学 号: 1412010039 姓 名: 徐雄飞 指导教师: 贾瑜 1 课程设计目的与要求 课程设计是在学习Java程序设计之后的实践教学环节。该实践教学是软件设计的综合训练,包括问题分析、总体结构设计、用户界面设计、程序设计基本技能和技巧。要求学生在设计中逐步提高程序设计能力,培养科学的软件工作方法。学生通过课程设计在下述各方面得到锻炼:1、能根据实际问题的具体情况,结合面向对象的基本理论和基本技巧,正确分析问题,并能设计出解决问题的有效算法与程序。2、提高程序设计和调试
2、能力。学生通过上机实习,验证自己设计的算法和程序的正确性。学会有效利用基本调试方法,迅速找出程序代码中的错误并且修改,进一步提高程序设计水平。2 设计内容2.1 蜘蛛纸牌 2.1.1 程序基本功能 蜘蛛纸牌游戏通过鼠标操作,将电脑多次分发的纸牌,按照相同的花色由大到小排 列起来,直到桌面上的纸牌全都消失,即为胜利。包括如下功能: (a)难度选择:包括简单(单色)、中级(双色)、高级(四色)。 (b)开局:任何时候可以重新开始一盘新的游戏。 (c)发牌。 (d)显示可行操作:提示当前可行操作。 (e)简单的帮助文档:对软件的简单介绍和编制说明。2.
3、1.2 程序设计方案和原理蜘蛛纸牌游戏共由4个部分组成,分别是:Spider.java,SpiderMenuBar.java,PKCard.java,AboutDialog.java,SpiderMenuBar.java 包含名为SpiderMenuBar的public类,其主要功能为生成蜘蛛纸牌游戏的菜单栏,实现菜单栏中各个组件的事件侦听。主要包括3个模块:图形用户界面的构建;组件监听接口的实现:显示可执行操作的线程。PKCard.java 。包含名为PKCard的public类,其主要功能为:定义纸牌的属性,包括名称,位置等相关信息。并通过相关方法实现纸牌的
4、移动等。 AboutDialog.java 。包含名为AboutDialog的public类,其主要功能为生成蜘蛛纸牌游戏的帮助栏。 Spider.java 。包含名为Spider的public类,其主要功能为生成蜘蛛纸牌游戏的框架,实现游戏中的方法,包括:纸牌的随机生成,位置的摆放等。程序流程图2.1.3 程序的代码实现 SpiderMenuBar.javaSpiderMenuBar.java的作用是生成蜘蛛游戏的菜单,实现菜单栏中各个组件的事件倾听。其代码如下:import javax.swing.JMenuBar;import javax.swin
5、g.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 jItem
6、About = new JMenuItem("关于"); JMenuItem jItemOpen = new JMenuItem("开局"); JMenuItem jItemPlayAgain = new JMenuItem("重新发牌"); /生成单选框 JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem("简单:单一花色"); JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuIt
7、em("中级:双花色"); JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem("高级:四花色"); JMenuItem jItemExit = new JMenuItem("退出"); JMenuItem jItemValid = new JMenuItem("显示可行操作"); /* *构造函数,生成JMenuBar的图形界面 */ public SpiderMenuBar(Spider spider) this.main = spider;
8、/* *初始化“游戏”菜单栏 */ 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(); grou
9、p.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(); ); /“重新发牌”jIte
10、mPlayAgain.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.ActionEven
11、t 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.ActionListene
12、r() 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.initCa
13、rds(); 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 menuSel
14、ected(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.eve
15、nt.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) new AboutDialog(); ); /* *构造线程:显示可以执行的操作 */ class Show extends Thread public void run() main.showEnableOperator(); PKCard.javaPKCard,java的作用是定义纸牌的属性,包括名称,位置等相关信息。并通过相关方法实现了纸牌的移动等。其代码如下:import java.awt.*;import java.awt.eve
16、nt.*;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; P
17、KCard 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 Threadprivate PKCard card = null;public Flash(PKCard ca
18、rd)this.card = card;/* *线程的run()方法 *为纸牌的正面设置白色图片 */public void run()boolean is = false;ImageIcon icon = new ImageIcon("images/white.gif");for (int i = 0; i < 4; i+)tryThread.sleep(200);catch (InterruptedException e)e.printStackTrace();if (is)this.card.turnFront();is = !is;elsethis.card.
19、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();/
20、判断可行列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
21、 = false;PKCard card = null;if (point = null)point = main.getGroundLabelLocation(n);isEmpty = true;elsecard = (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(
22、);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);elsethis.setNextCardLocation(null);main.table.remove(this.getLocation();this.setLocation(thi
23、s.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);/* *方法:放置纸牌 */publ
24、ic 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);elsepoint = new Point(point);point.y += 20;card.set
25、NextCardLocation(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;
26、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 mouseE
27、ntered(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) PKC
28、ard 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 arg
29、0) /* *构造函数 */ public PKCard(String name, Spider spider) super(); this.type = new Integer(name.substring(0, 1).intValue(); this.value = new Integer(name.substring(2).intValue(); = name; this.main = spider; this.pane = this.main.getContentPane(); this.addMouseListener(this); this.addMouseMo
30、tionListener(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 *方法:令纸牌显示背面 */publ
31、ic 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) thi
32、s.canMove = can; PKCard card = main.getPreviousCard(this); if (card != null && card.isCardFront() if (!can) if (!card.isCardCanMove() return; elsecard.setCanMove(can); else if (this.value + 1 = card.getCardValue() && this.type = card.getCardType()card.setCanMove(can);elsecard.setCanM
33、ove(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()
34、 return type; AboutDialog.javaAboutDialog.java的主要功能是生成蜘蛛纸牌游戏的帮助栏。其代码如下:import javax.swing.*;import java.awt.*;/* *“关于”窗口 */public class AboutDialog extends JDialogJPanel jMainPane = new JPanel();JTabbedPane jTabbedPane = new JTabbedPane();private JPanel jPanel1 = new JPanel();private JPanel jPanel2
35、= new JPanel();private JTextArea jt1 = new JTextArea("将电脑多次分发给你的牌按照相同的花色由大至小排列起来。直到桌面上的牌全都消失。"); private JTextArea jt2 = new JTextArea("该游戏中,纸牌的图片来自于Windows XP的纸牌游戏,图片权属于原作者所有!"); /* *构造函数 */public AboutDialog()setTitle("蜘蛛牌");setSize(300,200);setResizable(false);setDef
36、aultCloseOperation (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.set
37、Foreground(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);j
38、MainPane.add(jTabbedPane);c.add(jMainPane);pack();this.setVisible(true); Spider.javaSpider.java文件是蜘蛛纸牌游戏的主类文件,其主要功能是生成蜘蛛纸牌游戏的框架,实现游戏中的方法,包括纸牌的随机生成,位置的摆放等。其代码如下:mport java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;public class Spider extends JFrame/整型变量,表示难度等级为:简单public st
39、atic 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 PKCard104; private JLabel clickLabel = null; private int c = 0;
40、 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()setTitle("蜘蛛牌"); setDefaultCloseOperation(javax.sw
41、ing.JFrame.EXIT_ON_CLOSE); /设置框架的大小setSize(1024, 742); /生成SpiderMenuBar对象,并放置在框架之上setJMenuBar(new SpiderMenuBar(this); pane = this.getContentPane(); /设置背景颜色pane.setBackground(new Color(0, 112, 26); /将布局管理器设置成为nullpane.setLayout(null); clickLabel = new JLabel(); clickLabel.setBounds(883, 606, 121, 96
42、); 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 JLabel10; int x = 20; for (int i = 0; i < 10; i+) groundLabeli = n
43、ew JLabel(); groundLabeli .setBorder(javax.swing.BorderFactory .createEtchedBorder(javax.swing.border.EtchedBorder.RAISED); groundLabeli.setBounds(x, 25, 71, 96); x += 101; this.pane.add(groundLabeli); 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().st
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理课件演讲的演讲仪态训练
- 零售连锁超市店长岗位胜任能力评估
- 基于欧标EN1886的船舶机舱环境控制研究
- 影视化妆职业规划
- 吸痰护理中的多学科协作模式
- 宝鸡安全管理认证培训
- 高一历史学案(中外历史纲要上)第17课 挽救民族危亡的斗争
- 2025年氢能发动机故障预警系统开发案例
- 基于循环经济的废物再处理企业发展策略研究
- 快消品行业成本预算经理的职责与面试要点
- 《分子筛结构与应用》课件
- 毕业设计(论文)-桶装纯净水自动灌装机的设计
- 城市轨道交通行车组织50课件
- 2025年江苏护理职业学院高职单招语文2019-2024历年真题考点试卷含答案解析
- 办公室用电安全分享
- 2025年度高速公路智能化监控系统建设合同3篇
- 化工泵技术要求
- 船舶内部审核-审核要素
- 2024年常州信息职业技术学院单招职业适应性测试题库及答案一套
- 贵州源鑫矿业有限公司煤矸石洗选综合利用项目环评报告
- 1993年物理高考试卷与答案
评论
0/150
提交评论