推荐魔板游戏Java课程设计报告可编辑.docx
《推荐魔板游戏Java课程设计报告可编辑.docx》由会员分享,可在线阅读,更多相关《推荐魔板游戏Java课程设计报告可编辑.docx(19页珍藏版)》请在冰豆网上搜索。
推荐魔板游戏Java课程设计报告可编辑
GUI应用开发(JAVA)期末报告
学号:
06
姓名:
马伟
班级:
Y122092班
老师:
谢从华
模板游戏的设计
1概述
1.1课程设计目的
利用所学高级语言的语法特性,结合数据结构算法、网络知识、文件处理和数据库等知识完成综合题目的设计和代码实现,并培养锻炼分析程序、撰写报告等能力。
1.2课程设计内容
(1)设计一款经典的智力游戏,魔板游戏。
魔板由3*3或4*4个格子组成。
对于3*3魔板,在前8个格子里随即放置8个编号1-8的方块,最后一个格子是未放置方块的空格子;对于4*4的魔板,在前15格子里随即放置15编号1-15的方块,最后一个格子是未放置方块的空格子。
(2)用鼠标单击任何与空格子水平或垂直相邻的方块可以把该方块移入空格子,而当前方块移动之前所在的格子成为空格子。
通过不断地移动方块可以将方块一行一行地按数字顺序排好,例如,对于3*3格子组成的魔板,要求方块最后排列的顺序如图6.1所示。
(3)魔板游戏也可以使用图像来代替数字。
例如,对于4*4的魔板,将一幅图像分成4*4幅小图像,除去最后一幅小图像(图像的右下角),将其余各幅小图像打乱顺序后放在魔板方块上,最终目标是通过移动方块恢复原始图像(不包括图像的右下角)。
2系统需求分析
2.1系统目标
设计GUI界面的魔板游戏程序。
2.2主体功能
这个程序定义了一些组件,工具栏按钮,文本区和菜单。
魔板游戏程序设计通过PuzzleFrame.java、PuzzlePad.java、SquarePoint.java3个类来实现魔板游戏的整体功能。
2.3开发环境
操作系统:
MicrosoftWindows7
开发工具:
jdk1.6.0_22、MyEclipse8.5
3系统概要设计
3.1系统的功能模块划分
在设计魔板游戏时,需要编写3个java源文件:
PuzzleFrame.java、PuzzlePad.java、SquarePoint.java。
魔板游戏除了需要编写的上述3个Java源文件所给出的类外,还需要Java系统提供的一些重要的类,如JMenuItem、JButton和JRadioButton等类。
3.1.2PuzzleFrame.java类模块
PuzzleFrame类负责创建魔板游戏的主窗口,该类含有main方法,程序从该类开始。
PuzzleFrame类的成员变量中有4种重要类型的对象:
PuzzlePad、JmenuItem、JRadioButton和JButton。
PuzzleFrame类的主要成员的作用将在后面的详细设计中阐述,PuzzleFrame类主要成员变量(属性)和主要方法如表3-1、3-2所示。
成员变量描述
变量类型
名称
魔板
开始游戏的按钮
选择玩法
PuzzlePad
Jbutton
JradioButton
PuzzlePad
开始
数字玩法,图像玩法
表3-1PuzzleFrame.java主要成员变量(属性)
名称
功能
备注
PuzzleFrame
actionPerformed
main
创建程序主窗口
处理ActionEvent事件
程序开始运行的方法
构造方法
接口方法
表3-2PuzzleFrame.java主要方法
3.1.3PuzzlePad.java类模块
PuzzlePad类创建的对象是PuzzleFrame类最重要的成员之一,代表“魔板”。
该类的成员变量中有5种重要类型的对象:
Point、Block和Image。
PuzzlePad类类主要成员变量(属性)和主要方法如表3-3、3-4所示。
描述
变量类型
名称
魔板中的行数和列数
魔板中方块的宽和高
魔板中二维点数组
魔板中而为方块数组
模板可能用到的图像
int
int
SquarePoint[][]
JButton
Image
rows,colums
width,height
point
block
image
表3-3PuzzlePad.java主要成员变量(属性)
名称
功能
备注
PuzzlePad
随机排列数字
随机排列图像
paintComponent
actionPerformed
创建魔板对象
随机排列魔板中方块上的数字
随机排列魔板中方块上的图像
绘制图像
处理ActionEvent事件
构造方法
接口方法
表3-4PuzzlePad.java主要方法
3.1.4SquarePoint.java类模块
SquarePoint类负责创建确定位置的对象,使用Point对象可以确定Block对象在PuzzlePad对象中的位置,即确定“方块”在“魔板”中的位置。
SquarePoint.java类主要成员变量(属性)和主要方法如表3-5、3-6所示。
描述
变量类型
名称
判断点上是否有方块
点上的方块对象
boolean
JButton
有方块
block
表3-5SquarePoint.java主要成员变量(属性)
名称
功能
备注
SquarePoint
get有方块
set有方块
setBlock
getBlock
创建“点”对象
判断点上是否有方块
设置点上是否有方块
在点上放置方块
获取点上的方块
构造方法
表3-6SquarePoint.java主要方法
3.2系统流程图
根据对模板游戏的功能分析,设计得到魔板游戏总流程,如图2-2所示:
图2-2系统流程图
4系统详细设计
4.1运行效果
1、将三个java文件和图像文件保存到一个文件夹中,例如C:
\moban。
将三个java文件分别编译生成相应的字节码文件(class)。
然后,用java解释器运行主类:
E:
\Eclipse(实验)\javaPuzzleFrame效果如图4-1所示。
图4-1数字玩法主界面
本程序实现的游戏分为初级和高级模式,可以在单机选择级别之后进行选择级别模式。
初级的话共分为9格魔板,一个为空,8个魔板上下左右自由移动,直至排列完成;高级的话分为16格魔板,一个为空,15个魔板上下左右自由移动,直至排列完成。
该游戏可分为数字玩法和图像玩法,在单击图像玩法或者数字玩法之后可以进行相应的玩法选择,选择之后单击开始。
如果玩家想重新开始,只需要再次单击开始按钮即可。
5测试
5.1测试方案
游戏难度级别设置为数字玩法,进行测试。
用java解释器运行主类:
E:
\Eclipse(实验)\javaPuzzleFrame
5.2测试结果
图5-1测试结果
6小结
通过这次实验,使我在软件开发方面也累积了不少经验,对java的知识重要性的认识更深了。
通过设计过程的锻炼,自己分析问题和解决问题的能力都得到了锻炼和提高,完善了自己的知识结构,加深了对所学知识的理解。
通过课程设计,我学习到不少的JAVA编程语言的知识,但由于自己的理论知识水平有限,实践知识和设计经验不足,在设计过程中难免存在一些问题,甚至错误。
这些我会在以后的工作学习和实践中加以改进和提高。
附录
附录1源程序清单
附录11.1PuzzleFrame模块源程序
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassPuzzleFrameextendsJFrameimplementsActionListener
{
PuzzlePadpuzzlePad;
JButton开始;
JMenuBarbar;
JMenufileMenu;
JMenuItem初级,高级;
JRadioButton数字玩法,图像玩法;
ButtonGroupgroup=null;
Containercon=null;
publicPuzzleFrame()
{
bar=newJMenuBar();
fileMenu=newJMenu("魔板游戏");
初级=newJMenuItem("初级");
高级=newJMenuItem("高级");
fileMenu.add(初级);
fileMenu.add(高级);
bar.add(fileMenu);
setJMenuBar(bar);
初级.addActionListener(this);
高级.addActionListener(this);
开始=newJButton("开始玩");
开始.addActionListener(this);
group=newButtonGroup();
数字玩法=newJRadioButton("数字玩法",true);
图像玩法=newJRadioButton("图像玩法",false);
group.add(数字玩法);
group.add(图像玩法);
puzzlePad=newPuzzlePad(3,3,50,50);
con=getContentPane();
con.add(puzzlePad,BorderLayout.CENTER);
JPanelpNorth=newJPanel();
pNorth.add(数字玩法);
pNorth.add(图像玩法);
pNorth.add(开始);
con.add(pNorth,BorderLayout.NORTH);
con.validate();
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,440,360);
validate();
}
publicvoidactionPerformed(ActionEvente)
{if(e.getSource()==开始)
{
if(数字玩法.isSelected())
{
puzzlePad.随机排列数字();
}
elseif(图像玩法.isSelected())
{
puzzlePad.随机排列图像();
}
}
elseif(e.getSource()==初级)
{
con.remove(puzzlePad);
puzzlePad=newPuzzlePad(3,3,50,50);
con.add(puzzlePad,BorderLayout.CENTER);
con.validate();
this.validate();
}
elseif(e.getSource()==高级)
{
con.remove(puzzlePad);
puzzlePad=newPuzzlePad(4,4,50,50);
con.add(puzzlePad,BorderLayout.CENTER);
con.validate();
this.validate();
}
}
publicstaticvoidmain(Stringargs[])
{
newPuzzleFrame();
}
}
附录1.2PuzzlePad模块源程序
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.*;
importjava.util.Vector;
importjava.awt.image.*;
publicclassPuzzlePadextendsJPanelimplementsActionListener
{
introws,colums;
intwidth,height;
SquarePointpoint[][];
JButtonblock[][];
Vectorvector;
intleftX=50,leftY=50;
Imageimage=null;
Toolkittool=null;
MediaTrackermt;
int图像的宽,图像的高;
ImageblockImage[];
boolean画图像=true;
publicPuzzlePad(intr,intc,intw,inth)
{
setLayout(null);
vector=newVector();
rows=r;
colums=c;
width=w;
height=h;
tool=getToolkit();
try
{
image=tool.createImage("气球.jpg");
mt.addImage(image,1);
mt.waitForAll();
}
catch(Exceptione)
{
}
block=newJButton[rows][colums];
point=newSquarePoint[rows][colums];
for(inti=0;i{
for(intj=0;j{
if((i==rows-1)&&(j==colums-1))
{
}
else
{
block[i][j]=newJButton();
block[i][j].setSize(width,height);
block[i][j].addActionListener(this);
vector.add(block[i][j]);
}
}
}
for(inti=0;i{
for(intj=0;j{
point[i][j]=newSquarePoint(50+j*width,50+i*height);
if((i==rows-1)&&(j==colums-1))
{
}
else
{
point[i][j].setBlock(block[i][j],this);
}
}
}
}
publicvoidactionPerformed(ActionEvente)
{
JButtonbutton=(JButton)e.getSource();
intx=button.getBounds().x;
inty=button.getBounds().y;
intm=0,n=0;
for(inti=0;i{
for(intj=0;j{
if(point[i][j].getX()==x&&point[i][j].getY()==y)
{
m=i;
n=j;
}
}
}
int上=Math.max(0,m-1);
int下=Math.min(rows-1,m+1);
int左=Math.max(0,n-1);
int右=Math.min(colums-1,n+1);
if(point[上][n].get有方块()==false)
{
point[上][n].setBlock(button,this);
point[m][n].set有方块(false);
}
elseif(point[下][n].get有方块()==false)
{
point[下][n].setBlock(button,this);
point[m][n].set有方块(false);
}
elseif(point[m][左].get有方块()==false)
{
point[m][左].setBlock(button,this);
point[m][n].set有方块(false);
}
elseif(point[m][右].get有方块()==false)
{
point[m][右].setBlock(button,this);
point[m][n].set有方块(false);
}
}
publicvoid随机排列数字()
{
vector.removeAllElements();
for(inti=0;i{
for(intj=0;j{
if((i==rows-1)&&(j==colums-1))
{
}
else
{
vector.add(block[i][j]);
}
}
}
inti=1;
while(vector.size()>0)
{
intn=(int)(Math.random()*vector.size());
JButtonb=(JButton)vector.elementAt(n);
b.setText(""+i);
b.setIcon(null);
b.setBorder(BorderFactory.createLineBorder(Color.blue,1));
vector.remove(n);
i++;
}
画图像=false;
repaint();
}
publicvoid随机排列图像()
{
try
{
图像的宽=image.getWidth(this);
图像的高=image.getHeight(this);
intw=图像的宽/colums;
inth=图像的高/rows;
blockImage=newImage[rows*colums];
intk=0;
PixelGrabberpg=null;
ImageProducerip=null;
for(inti=0;i{
for(intj=0;j{
intpixels[]=newint[w*h];
pg=newPixelGrabber(image,j*w,i*h,w,h,pixels,0,w);
pg.grabPixels();
ip=newMemoryImageSource(w,h,pixels,0,w);
blockImage[k]=tool.createImage(ip);
k++;
}
}
vector.removeAllElements();
for(inti=0;i{
for(intj=0;j{
if((i==rows-1)&&(j==colums-1))
{
}
else
{
vector.add(block[i][j]);
}
}
}
intnum=0;
while(vector.size()>0)
{
intn=(int)(Math.random()*vector.size());
JButtonb=(JButton)vector.elementAt(n);
b.setIcon(newImageIcon(blockImage[num]));
b.setBorder(null);
b.setText("");
vector.remove(n);
num++;
}
画图像=true;
repaint();
}
catch(Exceptionee)
{
}
}
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(leftX,leftY,colums*width,rows*height);
if(画图像==true)
{
g.drawImage(image,colums*width+80,height,colums*width,rows*height,this);
for(inti=0;i{
g.setColor(Color.white);
g.drawLine(colums*width+80,height+i*height,
colums*width+80+colums*width,height+i*height);
}
for(intj=0;j{
g.drawLine(colums*width+80+j*width,height,
colums*width+80+j*width,height+rows*height);
}
g.setColor(Color.black);
g.fillRect(colums*width+80+(colums-1)*width,
height+(rows-1)*height,width,height);
}
}
}
附录1.3SquarePoint类模块源程序
importjava.awt.*;
importjavax.swing.JButton;
publicclassSquarePoint
{
intx,y;
boolean有方块;
JButtonblock=null;
Containercon=null;
publicSquarePoint(intx,inty)
{
this.x=x;
this.y=y;
}
publicbooleanget有方块()
{
return有方块;
}
publicvoidset有方块(booleanboo)
{
有方块=boo;
}
publicintgetX()
{
returnx;
}
publicintgetY()
{
returny;
}
publicvoidsetBlock(JButtonblock,Containercon)
{
this.con=con;
this.block=block;
con.add(block);
block.setLocation(x,y);
有方块=true;
con.validate();
}
publicJBut