动物换位游戏java课设教学教材Word文档下载推荐.docx
《动物换位游戏java课设教学教材Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《动物换位游戏java课设教学教材Word文档下载推荐.docx(18页珍藏版)》请在冰豆网上搜索。
布置课程设计任务,查阅资料,分组设计,程序调试。
第19周:
程序调试,编写课程设计报告,验收,答辩。
指导教师:
2013年6月20日
专业负责人:
学院教学副院长:
1.设计目的..................................................4
2.设计内容..................................................4
3.课程软件主要功能........................................4
4.类总体设计................................................4
4.1类组合关系图...........................................5
4.2主类.....................................................5
4.3其他类...................................................5
5.类详细设计(画UML图)....................................6
6.关键类成员函数流程图及代码.........................7
6.1总体流程图.............................................7
6.2程序代码...............................................8
7.软件运行结果.............................................14
8.结论.......................................................16
参考文献.......................................................16
1.设计目的
1.通过实验,使学生进一步加深面向对象的编程设计思想;
2.训练掌握、加深理解并巩固《Java程序设计》课程中所学到的基本概念、编程思路;
3.综合运用Java程序设计的编程方法,能够利用这些程序设计方法来解决工程和科研中常见的实际问题;
4.培养分析解决问题的实际动手能力,进一步提高进行大型程序设计的能力。
2.设计要求
设计GUI界面的动物换位游戏,游戏的结果是让左右两组动物交换位置。
具体内容如下:
程序分为4个级别,用户可以通过界面上提供的菜单选择“初级”、“中级”、“高级”、“自定义”这4个级别。
对于“高级”级别,在水平排列的11块石头上的左右各有5个类型相同的动物,中间的石头上没有动物;
对于“自定义”级别,用户可以输入自己想设置的一个动物数量(输入的数必须是偶数),在水平排列的N块石头上的左右各有(N-1)/2个类型相同的动物,中间的石头上没有动物。
3.课程软件主要功能
程序分为不同的级别,左右各有动物,中间石头没有动物,将左右动物交换,不可后退。
用鼠标单击一个动物,如果该动物前方的石头上没有动物,该动物就跳跃到该石头上,如果该动物前方的石头上有动物,但隔位上的石头没有动物,该动物就越过自己前面的动物跃到石头上,其他情形时,该动物不能跳跃。
如果对游戏有疑问可以点击帮助,查看游戏规则。
不能持续玩的可以点自动演示,将自动完成游戏,可以帮助游戏者更好的了解系统的主体功能
4.类总体设计
设计动物换位时需要编写10个Java源文件:
ChangeAnimalWindow.java、Point.java、Animal.java、Stone.java、HandleMouse.java、AutoMoveAnimal.java、People.java、Record.java、ShowRecordDialog.java和Customdlg.java。
动物换位除了需要编写上述10个Java源文件所给出的类外,还需要Java系统提供的一些重要的类,如:
JmenuItem、File和Jbutton类等。
动物换位所用到的一些重要的类以及之间组合关系如图4-1所示。
4.1类组合关系图
以下是10个Java源文件的总体设计:
图4-1类之间的组合关系
4.2主类
ChangeAnimalWindow.java
ChangeAnimalWindow类负责创建动物换位的主窗口,该类含有main方法,程序从该类开始执行。
4.3其他类
Animal.java
Animal类是JPanel的一个子类,创建的对象是ChangeAnimalWindow类的重要成员之一,用来表示“动物”。
Point.java
Point类负责创建确定位置的对象,使用Point对象,可以确定Animal以及Stone对象在窗体中的位置。
Stone.java
Stone类是JButton的一个子类,创建的对象表示“石头”。
HandleMouse.java
HandleMouse类所创建的对象负责处理鼠标事件。
AutoMoveAnimal.java
AutoMoveAnimal类所创建的对象使用算法完成动物的自动换位。
Customdlg.java
Customdlg类所创建的对象用来设置“自定义”级别的动物数量。
People.java
People对象封装用户的名字和成绩,以便ShowRecordDialog对象可以排序。
Record.java
Record类是javax.swing包中JDialog对话框的子类,当用户成功完成相应级别的游戏时,Record创建的对象负责保存用户的名字和成绩到相应的级别文件中。
ShowRecordDialog.java
ShowRecordDialog类是javax.swing包中JDialog对话框的子类,当用户单击查看相应的级别排行榜时,ShowRecordDialog创建的对象负责显示用户的成绩。
5.类详细设计(画UML图)
ChangeAnimalWindow类是javax.swing包中的JFrame的一个子类,并实现了ActionListener接口,标明该类的主要成员变量和方法的UML图如图5-1所示。
以下是UML图中有关数据和方法的详细说明:
图5-1ChangeAnimalWindow类的UML图
6.关键类成员函数流程图及代码
6.1总体流程图
图6-1总体流程图
6.2程序代码
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.ArrayList;
importjava.io.*;
importjavax.swing.filechooser.*;
publicclassChangeAnimalWindowextendsJFrameimplementsActionListener{
intamountOfAnimal=6;
intdistance=80;
Animal[]animal;
Point[]point;
Stone[]stone;
HandleMousehandleMouse;
AutoMoveAnimalautoMoveAnimal;
FileleftImageFile,rightImageFile;
JButtonrenew,quit,autoButton=null;
JMenuBarbar;
JMenumenuGrade,menuImage;
JMenuItemoneGradeItem,twoGradeItem,threeGradeItem;
JMenuItemleftIamge,rightIamge,defaultImage;
JPanelpCenter;
ChangeAnimalWindow(){
pCenter=newJPanel();
pCenter.setBackground(Color.lightGray);
pCenter.setLayout(null);
handleMouse=newHandleMouse();
autoMoveAnimal=newAutoMoveAnimal();
leftImageFile=newFile("
dog.jpg"
);
rightImageFile=newFile("
cat.jpg"
init();
bar=newJMenuBar();
menuGrade=newJMenu("
选择级别"
menuImage=newJMenu("
选择动物图像(JPG,GIF)"
oneGradeItem=newJMenuItem("
初级"
twoGradeItem=newJMenuItem("
中级"
threeGradeItem=newJMenuItem("
高级"
leftIamge=newJMenuItem("
左面动物的图像"
rightIamge=newJMenuItem("
右面动物的图像"
defaultImage=newJMenuItem("
左右面动物的默认图像"
menuGrade.add(oneGradeItem);
menuGrade.add(twoGradeItem);
menuGrade.add(threeGradeItem);
menuImage.add(leftIamge);
menuImage.add(rightIamge);
menuImage.add(defaultImage);
bar.add(menuGrade);
bar.add(menuImage);
setJMenuBar(bar);
oneGradeItem.addActionListener(this);
twoGradeItem.addActionListener(this);
threeGradeItem.addActionListener(this);
leftIamge.addActionListener(this);
rightIamge.addActionListener(this);
defaultImage.addActionListener(this);
renew=newJButton("
重新开始"
renew.addActionListener(this);
autoButton=newJButton("
自动演示"
autoButton.addActionListener(this);
quit=newJButton("
撤消"
quit.addActionListener(this);
JPanelnorth=newJPanel();
north.add(renew);
north.add(quit);
north.add(autoButton);
Stringmess="
互换左右动物位置"
;
JLabelhintMess=newJLabel(mess,JLabel.CENTER);
north.add(hintMess);
add(north,BorderLayout.NORTH);
add(pCenter,BorderLayout.CENTER);
JPanelsouth=newJPanel();
south.add(handleMouse);
add(south,BorderLayout.SOUTH);
setVisible(true);
setBounds(60,60,710,300);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidinit(){
animal=newAnimal[amountOfAnimal];
point=newPoint[amountOfAnimal+1];
stone=newStone[amountOfAnimal+1];
intspace=distance;
for(inti=0;
i<
point.length;
i++){
point[i]=newPoint(space,100);
space=space+distance;
}
animal.length;
animal[i]=newAnimal();
animal[i].addMouseListener(handleMouse);
if(i<
animal.length/2){
animal[i].setIsLeft(true);
else{
animal[i].setIsLeft(false);
stone.length;
stone[i]=newStone();
pCenter.add(stone[i]);
stone[i].setSize(distance*8/9,18);
intw=stone[i].getBounds().width;
inth=stone[i].getBounds().height;
stone[i].setLocation(point[i].getX()-w/2,point[i].getY());
point[i].setThisStone(stone[i]);
animal[i].setSize(distance*6/7,distance*3/4);
intw=animal[i].getBounds().width;
inth=animal[i].getBounds().height;
pCenter.add(animal[i]);
animal.length/2){
animal[i].setLeftImage(leftImageFile);
animal[i].repaint();
animal[i].setLocation(point[i].getX()-w/2,point[i].getY()-h);
animal[i].setAtPoint(point[i]);
point[i].setThisAnimal(animal[i]);
point[i].setIsHaveAnimal(true);
animal[i].setRightImage(rightImageFile);
animal[i].setLocation(point[i+1].getX()-w/2,point[i+1].getY()-h);
animal[i].setAtPoint(point[i+1]);
point[i+1].setThisAnimal(animal[i]);
point[i+1].setIsHaveAnimal(true);
handleMouse.setPoint(point);
handleMouse.setCountTime(true);
autoMoveAnimal.setPoint(point);
autoMoveAnimal.setStep();
publicvoidsetAmountOfAnimal(intm){
if(m>
=2&
&
m%2==0)
amountOfAnimal=m;
publicvoidremoveAnimalAndStone(){
if(point[i].getThisAnimal()!
=null)
pCenter.remove(point[i].getThisAnimal());
if(point[i].getThisStone()!
pCenter.remove(point[i].getThisStone());
pCenter.validate();
pCenter.repaint();
publicvoidneedDoing(){
handleMouse.initStep();
handleMouse.initSpendTime();
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==oneGradeItem){
distance=80;
removeAnimalAndStone();
setAmountOfAnimal(6);
needDoing();
elseif(e.getSource()==twoGradeItem){
distance=70;
setAmountOfAnimal(8);
elseif(e.getSource()==threeGradeItem){
distance=60;
setAmountOfAnimal(10);
elseif(e.getSource()==renew){
elseif(e.getSource()==autoButton){
handleMouse.setCountTime(false);
intx=this.getBounds().x+this.getBounds().width/2;
inty=this.getBounds().y+this.getBounds().height;
autoMoveAnimal.setLocation(x,y);
autoMoveAnimal.setSize(this.getBounds().width/2,120);
autoMoveAnimal.setVisible(true);
elseif(e.getSource()==quit){
ArrayList<
Integer>
step=handleMouse.getStep();
intlength=step.size();
intstart=-1,end=-1;
if(length>
=2){
end=step.get(length-1);
start=step.get(length-2);
step.remove(length-1);
step.remove(length-2);
Animalani=point[end].getThisAnimal();
intw=ani.getBounds().width;
inth=ani.getBounds().height;
ani.setLocation(point[start].getX()-w/2,point[start].getY()-h);
ani.setAtPoint(point[start]);
point[start].setThisAnimal(ani);
point[start].setIsHaveAnimal(true);
point[end].setIsHaveAnimal(false);
elseif(e.getSource()==leftIamge){
JFileChooserchooser=newJFileChooser();
FileNameExtensionFilterfilter=newFileNameExtensionFilter(
"
JPG&
GIFImages"
"
jpg"
gif"
chooser.setFileFilter(filter);
intstate=chooser.showOpenDialog(null);
F