JAVA课程设计报告.docx
《JAVA课程设计报告.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计报告.docx(21页珍藏版)》请在冰豆网上搜索。
JAVA课程设计报告
JAVA课程设计
题目:
扫雷游戏
姓名:
学号:
101101014
班级:
10级1班
日期:
2013年05月25日
指导老师
唐远翔
分数
目录
一、设计思路1
二、具体实现1
三、运行调试与分析讨论3
四、设计体会与小结7
五、参考文献8
六、附录8
一、设计思路
扫雷游戏分为初级、中级和高级三个级别,单击游戏菜单可以选择“初级”、“中级”和“高级”或“退出”。
选择级别后将出现相应级别的扫雷区域,这是用户使用鼠标左键单击雷区中任何一个方块便启动计时器。
用户要揭开某个方块,可单击它。
若所揭方块下有雷,用户便输了这一局,若所揭方块下五雷,则显示一个数字,该数字代表方块的周围的8个方块中共有多少颗雷。
如果用户认为某个方块下埋着雷,单击右键可以在方块上标识一个用户认为是雷的图标,即给出一个扫雷标记。
用户每标记出一个扫雷标记(无论用户的标记是否正确),程序将显示的剩余雷数减少一个。
当用户想结束此局时可直接点击重新开始,开始下一局。
二、具体实现
系统的整体布局为:
JFrom布局,采用了菜单、按钮、面板…等组件,菜单主要包括开始,选择级别,退出,按钮的功能是重新开始新的游戏。
在设计扫雷游戏时,需要编写7个JAVA源文件:
ControlPanel.java、Difficulty.java、Digit.java、Display.java、Field.java、Minesweeper.java、和MinesweeperPanel.java。
扫雷游戏除需要编写上述7个源文件所给出的类外,还需要JAVA系统提供的一些重要的类,如File、JButton和JLable等类。
图1总体功能
图2程序流程图
三、运行调试与分析讨论
系统的详细设计代码请查阅附带的代码。
以下仅对游戏界面进行截图展示:
图一:
高级
图二:
中级
图三:
初级
运行JAVA程序代码后,如图所示为不同阶段断“初级”、“中级”、“高级”的界面视图;除去界面大小网格数量排列和雷数不同以外其他均相同。
运行程序后点击菜单选择所需阶段进入该阶段的界面视图,左面计数器为显示的该阶段的具体剩下的雷数;右面为时间的显示,显示从开始到现在所花费的具体时间,任意单击一网格开始。
四、设计体会与小结
三周的课程设计,自己通过查找资料、复习课本、编程调试,写实验报告等环节,进一步掌握了以前学到的知识,并且还对JAVA的窗体设计的应用有了更深入的认识与掌握,另外还学到了一些新东西,比如JAVA的SWING、AWT包,以前是没有接触过的,可是通过这次课程设计使得我们对这个包更了解。
通过与数据库的连接掌握了Java与数据库的连接技术。
通过实践的学习,我认到学好计算机要重视实践操作,不仅仅是学习java语言,还是其它的语言,以及其它的计算机方面的知识都要重在实践,所以后在学习过程中,我会更加注视实践操作,使自己便好地学好计算机。
五、参考文献
《软件工程课程设计》,吕云翔、刘浩、王昕鹏周建编著,机械工业出版社
《JAVA课程设计第二版》,耿祥义、张跃平编著,清华大学出版社
《JAVA程序设计与项目实践》,李兆峰、庞永庆、张华杰、许小荣编著、电子工业出版社
六、附录
程序的主要部分代码:
主界面模块主要代码Minesweeper.java
publicvoidactionPerformed(ActionEvente)
{
if(e.getActionCommand().equals("重新开始"))
{
controlPanel.setMinesweeper(difficulty);
minesweeperPanel.setMinesweeper(difficulty);
}
elseif(e.getActionCommand().equals("初级")||e.getActionCommand().equals("中级")||e.getActionCommand().equals("高级"))
{
difficulty=e.getActionCommand().equals("初级")?
Difficulty.BEGINNER:
e.getActionCommand().equals("中级")?
Difficulty.INTERMEDIATE:
Difficulty.EXPERT;
setMainMenuDifficulty();
controlPanel.setMinesweeper(difficulty);
minesweeperPanel.setMinesweeper(difficulty);
pack();
}
else
System.exit(0);
}
}
控制界面模块主要代码ControlPanel.Java
publicvoidsetMinesweeper(Difficultydifficulty)
{
timer.stop();
mineDisplay.setValue(difficulty==Difficulty.BEGINNER?
10:
difficulty==Difficulty.INTERMEDIATE?
40:
99);
timeDisplay.setValue(0);
begin=-1;
repaint();
}
publicvoidstartTime()
{
if(begin!
=-1)
return;
begin=newDate().getTime();
timer.start();
}
publicintstopTime()
{
timer.stop();
intretval=(int)((newDate().getTime()-begin)*1000);
returnretval;
}
@Override
publicvoidactionPerformed(ActionEvente)
{
if(e.getActionCommand()!
=null&&e.getActionCommand().equals("重新开始"))
minesweeper.actionPerformed(e);
else
{
timeDisplay.setValue((int)((newDate().getTime()-begin)/1000));
timeDisplay.repaint();
}
}
界面大小设定模块代码主要代码Digit.java
publicDigit(intdigit,intx,inty)
{
this.digit=digit;
this.x=x;
this.y=y;
width=13;
height=23;
darkRed=newColor(128,0,0);
}
publicvoidpaint(Graphicsg)
{
g.setColor(Color.BLACK);
g.fillRect(x,y,width,height);
switch(digit)
{
case0:
case2:
case3:
case5:
case6:
case7:
case8:
case9:
g.setColor(Color.RED);
g.drawLine(x+2,y+1,x+10,y+1);
g.drawLine(x+3,y+2,x+9,y+2);
g.drawLine(x+4,y+3,x+8,y+3);
break;
case-2:
case-1:
case1:
case4:
g.setColor(darkRed);
g.fillRect(x+3,y+1,1,1);
g.fillRect(x+5,y+1,1,1);
g.fillRect(x+7,y+1,1,1);
g.fillRect(x+9,y+1,1,1);
g.fillRect(x+4,y+2,1,1);
g.fillRect(x+6,y+2,1,1);
g.fillRect(x+8,y+2,1,1);
g.fillRect(x+5,y+3,1,1);
g.fillRect(x+7,y+3,1,1);
break;
}
界面大小显示模块主要代码Display.java
publicDisplay(intvalue)
{
setPreferredSize(newDimension(53,37));
setBackground(Color.LIGHT_GRAY);
this.value=value;
digit0=newDigit(0,7,7);
digit1=newDigit(0,20,7);
digit2=newDigit(0,33,7);
}
publicvoidsetValue(intvalue)
{
this.value=value;
}
publicintgetValue()
{
returnvalue;
}
@Override
publicvoidpaint(Graphicsg)
{
super.paint(g);
digit0.setDigit(0);
digit1.setDigit(0);
digit2.setDigit(0);
inttemp=value;
if(temp>=0)
{
if(temp/100>0)
{
digit0.setDigit(temp/100);
temp%=100;
}
if(temp/10>0)
{
digit1.setDigit(temp/10);
temp%=10;
}
digit2.setDigit(temp);
}
else
{
temp=-temp%100;
if(temp/10>0)
{
digit0.setDigit(-1);
digit1.setDigit(temp/10);
temp%=10;
}
else
{
digit0.setDigit(-2);
digit1.setDigit(-1);
}
digit2.setDigit(temp);
}
g.setColor(Color.WHITE);
g.drawRect(5,30,42,1);
g.drawRect(46,5,1,25);
g.setColor(Color.GRAY);
g.drawRect(5,5,41,1);
g.drawRect(5,5,1,25);
digit0.paint(g);
digit1.paint(g);
digit2.paint(g);
}
雷区网格设置模块代码Field.java
publicField(intx,inty)
{
this.x=x;
this.y=y;
neighbourMines=0;
color1=newColor(0,0,255);
color2=newColor(0,128,0);
color3=newColor(255,0,0);
color4=newColor(0,0,128);
color5=newColor(128,0,0);
color6=newColor(0,128,128);
color7=newColor(0,0,0);
color8=newColor(128,128,128);
flag=mineClicked=false;
}
publicbooleansetClicked()
{
if(!
flag)
{
clicked=true;
mineClicked=mine;
return!
mine;
}
returntrue;
}
publicvoidsetGameOver()
{
if(mine&&!
flag||!
mine&&flag)
clicked=true;
}
publicvoidpaint(Graphicsg)
{
Colorback=mineClicked?
Color.RED:
Color.LIGHT_GRAY;
g.setColor(back);
g.fillRect(x*16,y*16,16,16);
if(clicked)
{
g.setColor(Color.GRAY);
g.fillRect(x*16,y*16,16,1);
g.fillRect(x*16,y*16+1,1,15);
if(mine||!
mine&&flag)
{
g.setColor(Color.BLACK);
g.fillRect(x*16+4,y*16+4,9,9);
g.fillRect(x*16+2,y*16+8,13,1);
g.fillRect(x*16+8,y*16+2,1,13);
g.setColor(back);
g.fillRect(x*16+5,y*16+4,1,1);
g.fillRect(x*16+11,y*16+4,1,1);
g.fillRect(x*16+4,y*16+5,1,1);
g.fillRect(x*16+12,y*16+5,1,1);
g.fillRect(x*16+4,y*16+11,1,1);
g.fillRect(x*16+12,y*16+11,1,1);
g.fillRect(x*16+5,y*16+12,1,1);
g.fillRect(x*16+11,y*16+12,1,1);
g.setColor(Color.WHITE);
g.fillRect(x*16+6,y*16+6,2,2);
if(!
mine&&flag)
{
g.setColor(Color.RED);
g.drawLine(x*16+2,y*16+3,x*16+13,y*16+14);
g.drawLine(x*16+3,y*16+3,x*16+14,y*16+14);
g.drawLine(x*16+2,y*16+14,x*16+13,y*16+3);
g.drawLine(x*16+3,y*16+14,x*16+14,y*16+3);
}
return;
}
switch(neighbourMines)
{
case1:
g.setColor(color1);
g.fillRect(x*16+5,y*16+11,7,2);
g.fillRect(x*16+8,y*16+3,2,8);
g.fillRect(x*16+7,y*16+4,1,7);
g.fillRect(x*16+6,y*16+5,1,2);
g.fillRect(x*16+5,y*16+6,1,1);
break;
case2:
g.setColor(color2);
g.fillRect(x*16+3,y*16+11,10,2);
g.fillRect(x*16+3,y*16+10,4,1);
g.fillRect(x*16+4,y*16+9,5,1);
g.fillRect(x*16+6,y*16+8,5,1);
g.fillRect(x*16+8,y*16+7,4,1);
g.fillRect(x*16+10,y*16+5,3,2);
g.fillRect(x*16+3,y*16+5,3,1);
g.fillRect(x*16+3,y*16+4,10,1);
g.fillRect(x*16+4,y*16+3,8,1);
break;
case3:
g.setColor(color3);
g.fillRect(x*16+3,y*16+3,7,2);
g.fillRect(x*16+6,y*16+7,4,2);
g.fillRect(x*16+3,y*16+11,7,2);
g.fillRect(x*16+10,y*16+3,2,10);
g.fillRect(x*16+12,y*16+4,1,3);
g.fillRect(x*16+12,y*16+9,1,3);
break;
case4:
g.setColor(color4);
g.fillRect(x*16+9,y*16+3,3,10);
g.fillRect(x*16+3,y*16+7,10,2);
g.fillRect(x*16+4,y*16+5,3,2);
g.fillRect(x*16+5,y*16+3,3,2);
break;
case5:
g.setColor(color5);
g.fillRect(x*16+3,y*16+3,10,2);
g.fillRect(x*16+3,y*16+5,3,2);
g.fillRect(x*16+3,y*16+7,9,2);
g.fillRect(x*16+10,y*16+8,3,4);
g.fillRect(x*16+3,y*16+11,9,2);
break;
case6:
g.setColor(color6);
g.fillRect(x*16+4,y*16+3,8,2);
g.fillRect(x*16+6,y*16+7,6,2);
g.fillRect(x*16+4,y*16+11,8,2);
g.fillRect(x*16+3,y*16+4,3,8);
g.fillRect(x*16+10,y*16+8,3,4);
break;
case7:
g.setColor(color7);
g.fillRect(x*16+3,y*16+3,10,2);
g.fillRect(x*16+10,y*16+5,3,2);
g.fillRect(x*16+9,y*16+7,3,2);
g.fillRect(x*16+8,y*16+9,3,2);
g.fillRect(x*16+7,y*16+11,3,2);
break;
case8:
g.setColor(color8);
g.fillRect(x*16+4,y*16+3,8,2);
g.fillRect(x*16+4,y*16+7,8,2);
g.fillRect(x*16+4,y*16+11,8,2);
g.fillRect(x*16+3,y*16+4,3,3);
g.fillRect(x*16+10,y*16+4,3,3);
g.fillRect(x*16+3,y*16+9,3,3);
g.fillRect(x*16+10,y*16+9,3,3);
break;
}
}
else
{
g.setColor(Color.WHITE);
g.fill