基于Java的动物换位游戏软件设计.doc

上传人:仙人指路1688 文档编号:2387393 上传时间:2023-02-17 格式:DOC 页数:16 大小:3.41MB
返回 下载 相关 举报
基于Java的动物换位游戏软件设计.doc_第1页
第1页 / 共16页
基于Java的动物换位游戏软件设计.doc_第2页
第2页 / 共16页
基于Java的动物换位游戏软件设计.doc_第3页
第3页 / 共16页
基于Java的动物换位游戏软件设计.doc_第4页
第4页 / 共16页
基于Java的动物换位游戏软件设计.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《基于Java的动物换位游戏软件设计.doc》由会员分享,可在线阅读,更多相关《基于Java的动物换位游戏软件设计.doc(16页珍藏版)》请在三一办公上搜索。

1、目 录1. 软件主要功能22. 类总体设计22.1 类组合关系图22.2 系统流程图32.3 主类42.4 其他类42.4.1 Animal.java42.4.2 Point.java42.4.3 Stone.java42.4.4 HandleMouse.java42.4.5 AutoMoveAnimal.java42.4.6 Customdlg.java42.4.7 People.java42.4.8 Record.java42.4.9 ShowRecordDialog.java43. 类详细设计43.1 主类43.2 其他类64. 关键成员函数代码75. 软件运行结果15结 论16参考文献

2、161. 软件主要功能采用java技术设计动物换位游戏软件,设计要求如下:(1)程序分为3个级别,用户可以通过界面上提供的菜单来选择“初级”、“中级”和“高级”这三个级别。对于“初级”级别,在水平排列的7块石头上的左右各有3个类型相同的动物,中间的石头上没有动物;对于“中级”级别,在水平排列的9块石头上的左右各有4个类型相同的动物,中间的石头上没有动物;对于“高级”级别,在水平排列的11块石头上的左右各有5个类型相同的动物,中间的石头上没有动物。(2)用鼠标单击一个动物,如果该动物的前方的石头上没有动物,该动物就跳跃到该石头上,如果该动物前方的石头上有其他的动物,但隔位上的石头没有动物,该动物

3、就越过自己前面的动物跳跃到石头上,其他情形时,该动物不跳跃(跳跃时不能超过2块石头)。(3)左面的动物只能向右方跳跃,右面的动物只能向左方跳跃。(4)用户可以通过单击界面上提供的按钮,让程序自动完成动物换位。(5)可以随时单击界面上提供的按钮,重新开始当前的级别。(6)用户通过菜单选项,可以选择左、右动物的图像。2. 类总体设计2.1 类组合关系图在设计动物换位时需要编写10个Java源文件:ChangeAnimalWindow.java、Animal.java、Point.java、Stone.java、HandleMouse.java、AutoMoveAnimal.java、Customd

4、lg.java、People.java、Record.java和ShowRecordDialog.java。动物换位除了需要编写上述10个Java源文件所给出的类外,还需要Java系统提供的一些重要的类,如:JmenuItem、File和Jbutton类等。动物换位所用到的一些重要的类以及之间组合关系如图2.1所示。ChangeAnimalWindowAnimalPointStoneHandleMouseAutoMoveAnimalCustomdlgPeopleRecordJbuttonFileJmenuItemShowRecordDialog 图2.1 类之间的组合关系2.2 系统流程图开始

5、进入游戏界面是否选择级别进入所选级别等级是否选择动物图像进入选择动物图像界面游戏开始是否相邻两个图片相同游戏终止自动演示游戏结果是否要重新开始是否要自动演示是否达到英雄榜记录撤销游戏输入游戏者信息是否游戏成功继续游戏游戏结束是否是否是否否否否是是是是否 图2.2 系统流程图2.3 主类ChangeAnimalWindow类负责创建动物换位的主窗口,该类含有main方法,程序从该类开始执行。2.4 其他类2.4.1 Animal.javaAnimal类是JPanel的一个子类,创建的对象是ChangeAnimalWindow类的重要成员之一,用来表示“动物”。2.4.2 Point.javaPo

6、int类负责创建确定位置的对象,使用Point对象,可以确定Animal以及Stone对象在窗体中的位置。2.4.3 Stone.javaStone类是JButton的一个子类,创建的对象表示“石头”。2.4.4 HandleMouse.javaHandleMouse类所创建的对象负责处理鼠标事件。2.4.5 AutoMoveAnimal.javaAutoMoveAnimal类所创建的对象使用算法完成动物的自动换位。2.4.6 Customdlg.javaCustomdlg类所创建的对象用来设置“自定义”级别的动物数量。2.4.7 People.javaPeople对象封装用户的名字和成绩,以

7、便ShowRecordDialog对象可以排序。2.4.8 Record.javaRecord类是javax.swing包中JDialog对话框的子类,当用户成功完成相应级别的游戏时,Record创建的对象负责保存用户的名字和成绩到相应的级别文件中。2.4.9 ShowRecordDialog.javaShowRecordDialog类是javax.swing包中JDialog对话框的子类,当用户单击查看相应的级别排行榜时,ShowRecordDialog创建的对象负责显示用户的成绩。3. 类详细设计3.1 主类 图3.1 ChangeAnimalWindow类的UML图(1)成员变量l am

8、outOfAnimal是int型数据,默认值是6。amoutOfAnimal的值用来确定ChangeAnimalWindow窗体中“动物”的数目,即Animal对象的数目。l distance是int型数据,默认值是80。distance的值用来确定Point对象之间的距离,进而可以确定Animal之间,Stone之间的距离以及Animal和Stone对象外观的大小。l animal是Animal型数组,其长度为amoutOfAnimal。l point是Point型数组,其长度为amoutOfAnimal+1。l stone是Stone型数组,其长度为amoutOfAnimal+1。l ha

9、ndleMouse是HandleMouse类创建的鼠标事件监视器,用来监视animal数组中Animal对象触发的鼠标事件。l autoMoveAnimal是AutoMoveAnimal类创建的对话框。通过该对话框可以实现程序自动将左右动物交换位置。l bar是JMenuBar创建的菜单条,用来添加菜单。bar被放置在窗口的顶部。l menuGrade是JMenu创建的菜单,其名字为“选择级别”。menuImage也是JMenu创建的菜单,其名字为“选择动物图像(JPG,GIF)”。menuHelp也是JMenu创建的菜单,其名字为“帮助”。menuHero也是JMenu创建的菜单,其名字为“

10、英雄榜”。menuGrade、menuImage、menuHero和menuHelp被添加到菜单条bar中。l oneGradeItem、twoGradeItem、threeGradeItem和custom是JMenuItem创建的菜单项,它们的名字依次为“初级”、“中级”、“高级”和“自定义”,这四个菜单项被添加到菜单项menuGrade中。leftImage、rightImage和defaultImage也是JMenuItem创建的菜单项,其名字依次为“左面动物的图像”、“右面动物的图像”和“左右面动物的默认图像”,这三个菜单项被添加到菜单项menuImage中。oneGradeResul

11、t、twoGradeResult、threeGradeResult、customGradeResult和limitTime也是JMenuItem创建的菜单项,其名字依次为“初级英雄榜”、“中级英雄榜”、“高级英雄榜”、“自定义英雄榜”、“我超时了吗”,这五个菜单项被添加到菜单项menuHero中。helpcontent和aboutUs是JMenuItem创建的菜单项,它们的名字依次为“游戏规则”、和“关于我们”,这两个菜单项被添加到菜单项menuHelp中。oneGradeItem、twoGradeItem、threeGradeItem和custom以及leftImage、rightImage

12、和defaultImage等菜单项都将当前窗口注册为自己的ActionEvent事件监视器。renew、quit和autoButton是JButton创建的按钮对象,名字依次为“重新开始”、“撤销”和“自动演示”。renew、quit和autoButton都将当前窗口注册为自己的 ActionEvent事件监视器。(2)方法l ChangeAnimalWindow()是构造方法,负责完成窗口的初始化。l init()方法负责完成Animal、Stone和Point对象的创建。l setAmoutOfAnimal(int)方法负责设置动物的个数,即amountOfAnimal的值。l remov

13、eAnimalAndStone()方法负责从窗体中移掉动物和石头,即移掉Animal对象和Stone对象。l needDoing()方法负责执行一些经常需要重复执行的代码。l actionPerformed(ActionEvent)方法是ChangeAnimalWindow类实现的ActionListener接口中的方法。ChangeAnimalWindow创建的窗口是oneGradeItem、twoGradeItem、threeGradeItem、custom、leftImage、rightImage、defaultImage等菜单项和renew、quit、autoButton按钮的Acti

14、onEvent事件监视器。当用户选中某个菜单项或单击按钮时,窗口将执行actionPerformed(ActionEvent)方法进行相应的操作。如果用户选中oneGradeItem、twoGradeItem、threeGradeItem和custom四个菜单项的某一项时,actionPerformed(ActionEvent)方法所执行的操作就是改变amountOfAnimal的值,并执行needDoing()方法,完成必要的初始化。如果用户选中leftImage和rightImage菜单项的某一个时,actionPerformed(ActionEvent)方法所执行的操作就是改变leftI

15、mageFile和rightImageFile的引用。当用户单击renew按钮时,actionPerformed(ActionEvent)方法所进行的操作是保持当前的amountOfAnimal的值,并执行needDoing()方法,完成必要的初始化,当用户单击quit按钮时,actionPerformed(ActionEvent)方法所进行的操作是撤销当前“动物”的前一次跳跃。当用户单击autoButton按钮时,actionPerformed(ActionEvent)方法所进行的操作是弹出autoMoveAnimal对话框,用户可以通过该对话框让程序显示动物换位过程。main(String

16、)方法是程序运行的入口方法。3.2 其他类 图3.2 Animal类的UML图 图3.3 Point类的UML图 图3.4 Stone类的UML图 图3.5 HandleMouse类的UML图 图3.6 AutoMoveAnimal类的UML图4. 关键成员函数代码ChangeAnimalWindow.java(主类)import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.ArrayList;import java.io.*;import javax.swing.filechooser.*;

17、public class ChangeAnimalWindow extends JFrame implements ActionListener int amountOfAnimal=6; int distance=80; Animal animal; Point point; Stone stone; HandleMouse handleMouse; AutoMoveAnimal autoMoveAnimal; File leftImageFile,rightImageFile; File fileOneGrade,fileTwoGrade,fileThreeGrade,filecustom

18、Grade; JButton renew,quit,autoButton=null; JMenuBar bar; JMenu menuGrade,menuImage,menuHelp,menuHero; JMenuItem oneGradeResult,twoGradeResult,threeGradeResult,customGradeResult; JMenuItem oneGradeItem,twoGradeItem,threeGradeItem,limitTime; JMenuItem custom; JMenuItem helpcontent,aboutUs; JMenuItem l

19、eftIamge,rightIamge,defaultImage; JPanel pCenter; ShowRecordDialog showDiolag=null; ChangeAnimalWindow() fileOneGrade=new File(初级英雄排行榜.txt); fileTwoGrade=new File(中级英雄排行榜.txt); fileThreeGrade=new File(高级英雄排行榜.txt); filecustomGrade=new File(自定义英雄排行榜.txt); pCenter=new JPanel(); pCenter.setBackground(C

20、olor.pink); pCenter.setLayout(null); handleMouse=new HandleMouse(); autoMoveAnimal=new AutoMoveAnimal(); leftImageFile=new File(dog.jpg); rightImageFile=new File(cat.jpg); init(); bar=new JMenuBar(); menuGrade=new JMenu(选择级别); menuImage=new JMenu(选择动物图像(JPG,GIF); menuHero=new JMenu(英雄榜); menuHelp=ne

21、w JMenu(帮助); oneGradeItem=new JMenuItem(初 级); twoGradeItem=new JMenuItem(中 级); threeGradeItem=new JMenuItem(高 级); custom=new JMenuItem(自定义); leftIamge=new JMenuItem(左面动物的图像); rightIamge=new JMenuItem(右面动物的图像); defaultImage=new JMenuItem(左右面动物的默认图像); oneGradeResult=new JMenuItem(初级英雄榜); twoGradeResul

22、t=new JMenuItem(中级英雄榜); threeGradeResult=new JMenuItem(高级英雄榜); customGradeResult=new JMenuItem(自定义英雄榜); limitTime=new JMenuItem(我超时了吗); helpcontent=new JMenuItem(游戏规则); aboutUs=new JMenuItem(关于我们); menuGrade.add(oneGradeItem); menuGrade.add(twoGradeItem); menuGrade.add(threeGradeItem); menuGrade.add

23、(custom); menuImage.add(leftIamge); menuImage.add(rightIamge); menuImage.add(defaultImage); menuHero.add(oneGradeResult); menuHero.add(twoGradeResult); menuHero.add(threeGradeResult); menuHero.add(customGradeResult); menuHero.add(limitTime); menuHelp.add(helpcontent); menuHelp.add(aboutUs); bar.add(

24、menuGrade); bar.add(menuImage); bar.add(menuHero); bar.add(menuHelp); setJMenuBar(bar); oneGradeItem.addActionListener(this); twoGradeItem.addActionListener(this); threeGradeItem.addActionListener(this); custom.addActionListener(this); leftIamge.addActionListener(this); rightIamge.addActionListener(

25、this); defaultImage.addActionListener(this); oneGradeResult.addActionListener(this); twoGradeResult.addActionListener(this); threeGradeResult.addActionListener(this); customGradeResult.addActionListener(this); limitTime.addActionListener(this); helpcontent.addActionListener(this); aboutUs.addActionL

26、istener(this); renew=new JButton(重新开始); renew.addActionListener(this); autoButton=new JButton(自动演示); autoButton.addActionListener(this); quit=new JButton(撤消); quit.addActionListener(this); JPanel north=new JPanel(); north.add(renew); north.add(quit); north.add(autoButton); String mess=互换左右动物位置; JLab

27、el hintMess=new JLabel(mess,JLabel.CENTER); north.add(hintMess); add(north,BorderLayout.NORTH); add(pCenter,BorderLayout.CENTER); JPanel south=new JPanel(); south.add(handleMouse); add(south,BorderLayout.SOUTH); setVisible(true); setBounds(60,60,710,300); validate(); setDefaultCloseOperation(JFrame.

28、EXIT_ON_CLOSE); if(!fileOneGrade.exists() try fileOneGrade.createNewFile(); catch(IOException exp) if(!fileTwoGrade.exists() try fileTwoGrade.createNewFile(); catch(IOException exp) if(!fileThreeGrade.exists() try fileThreeGrade.createNewFile(); catch(IOException exp) if(!filecustomGrade.exists() tr

29、y filecustomGrade.createNewFile(); catch(IOException exp) handleMouse.gradeFile=fileOneGrade; showDiolag=new ShowRecordDialog(); public void init() animal=new AnimalamountOfAnimal; point=new PointamountOfAnimal+1; stone=new StoneamountOfAnimal+1; int space=distance; for(int i=0;ipoint.length;i+) poi

30、nti=new Point(space,100); space=space+distance; for(int i=0;ianimal.length;i+) animali=new Animal(); animali.addMouseListener(handleMouse); if(ianimal.length/2) animali.setIsLeft(false); /else if(i=animal.length/2+1) / /if(animali.equals(animali) /animali.setIsLeft(true); /else / animali.setIsLeft(f

31、alse); / for(int i=0;istone.length;i+) stonei=new Stone(); pCenter.add(stonei); stonei.setSize(distance*8/9,18); int w=stonei.getBounds().width; int h=stonei.getBounds().height; stonei.setLocation(pointi.getX()-w/2,pointi.getY(); pointi.setThisStone(stonei); for(int i=0;ianimal.length;i+) animali.se

32、tSize(distance*6/7,distance*3/4); int w=animali.getBounds().width; int h=animali.getBounds().height; pCenter.add(animali); if(i=2&m%2=0) amountOfAnimal=m; public void removeAnimalAndStone() for(int i=0;ipoint.length;i+) if(pointi.getThisAnimal()!=null) pCenter.remove(pointi.getThisAnimal(); for(int

33、i=0;istone.length;i+) if(pointi.getThisStone()!=null) pCenter.remove(pointi.getThisStone(); pCenter.validate(); pCenter.repaint(); public void needDoing() init(); handleMouse.initStep(); handleMouse.initSpendTime(); handleMouse.setCountTime(true); autoMoveAnimal.setPoint(point); public void actionPe

34、rformed(ActionEvent e) if(e.getSource()=oneGradeItem) handleMouse.gradeFile=fileOneGrade; /good.getname(初级英雄排行榜.txt); distance=80; removeAnimalAndStone(); setAmountOfAnimal(6); needDoing(); else if(e.getSource()=twoGradeItem) handleMouse.gradeFile=fileTwoGrade; /good.getname(中级英雄排行榜.txt); distance=7

35、0; removeAnimalAndStone(); setAmountOfAnimal(8); needDoing(); else if(e.getSource()=threeGradeItem) handleMouse.gradeFile=fileThreeGrade; /good.getname(高级英雄排行榜.txt); distance=60; removeAnimalAndStone(); setAmountOfAnimal(10); needDoing(); else if(e.getSource()=custom) handleMouse.gradeFile=filecustomGrade; /good.getname(自定义英雄排行榜.txt); Customdlg dialog=new Customdlg(this,自定义,true); dialog.setVisible(true); amountOfAnimal=dialog.getAmountOfAnimal(); distance=560/amountOfAnimal; removeAnimalAndStone(); setAmountOfAnimal(amountOfAnimal); needDoing(); else if(e.getSource()=helpcontent) JOp

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号