}
//函数略
}
●扩散功能
voidcheckMine(introw,intcol){
inti,j;
i=row<0?
0:
row;
i=i>9?
9:
i;
j=col<0?
0:
col;
j=j>9?
9:
j;
if(mine.mine[i][j]==9){
bomb(i,j);
}elseif(mine.mine[i][j]==0
&&mineButton[i][j].getClickFlag()==false){
mineButton[i][j].setClickFlag(true);
showLabel(i,j);
for(intii=i-1;ii<=i+1;ii++)
for(intjj=j-1;jj<=j+1;jj++)
checkMine(ii,jj);
}else{
showLabel(i,j);
mineButton[i][j].setClickFlag(true);
}
if(isWin()){
win();
}
}
●计时器
privateclassTimeThreadextendsThread{
publicTimeThread(){}
//对Run方法进行重构
publicvoidrun(){
finalThreadcurrentThread=Thread.currentThread();
while(timepassed<1000&¤tThread==timeThread){
//changetimeLabel
SwingUtilities.invokeLater(
//innerclassRunnable
newRunnable(){
publicvoidrun(){
timeLabel.setIcon(newImageIcon(led.getLedImage(timepassed,3)));
}
}
);
try{
Thread.sleep(999);
}catch(InterruptedExceptioni){
System.err.println("sleepinterrupted");
}
timepassed++;
}
}//endofmethodrun
}
●序列化存储的实现
//实现Serializable接口
classLevelLogimplementsSerializable{
//变量略
……
publicvoidsetRecord(intr){
record=r;
}
publicvoidsetUserName(Stringname){
user=name;
}
publicintgetRecord(){
returnrecord;
}
publicStringgetUserName(){
returnuser;
}
publicStringtoString(){
returnrecord+"\t"+user+"\n";
}
}
2.实现结果
●可视化效果
[用户自定义游戏][扫雷扩散][扫雷失败]
●序列化存储
[对象序列化后的字节码]
六、结果分析
本课程设计完成了扫雷游戏的基本工作。
并且设计了菜单,对话框等可视化图形界面,使其更具有实用性,方便人们操作,同时通过序列化技术完成了对游戏数据的存储和读入。
七、结论
本次课程设计完成了扫雷游戏的基本过程,并给出了相应的图形化界面模拟演示。
通过本次课程设计,训练了对Java中JLabel等基本组件的应用和掌握。
并对面向对象的程序设计技术(Java)和多线程技术有了一定的锻炼。
对集成开发环境NetBeansIDE有了一定程度的熟练。
八、附录(主要功能函数代码)
MineFrame:
packagesaolei.game.mine;
/**
*
*authorkit
*/
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.swing.*;
importsaolei.awt.LedNumber;
importsaolei.swing.AboutDialog;
publicclassMainFrameextendsJFrameimplementsActionListener{
//UIcomponents
privateJLabelmineLabel;
privateJLabeltimeLabel;
privateJLabelstatusButton;
privateJPanelgridPanel;
privateJPanelstatusPanel;
privateDimensiongpd;
privateDimensionspd;
privateMineGridgrid[][];
privatebooleanmode[][];
privatefinalintmargin=7;
privatefinalinttitleh=52;
privateintxBound;
privateintyBound;
privateintmineCount;
privateintshowCount;//theamountofgridsopened
privateintleftCount;//theamountofminesnotlabeled
privateinttimepassed;
privatebooleanfirstClick;
privatebooleanmarkCheck;
//MenuComponents
privateJMenuItemstartItem;
privateJMenuItemexitItem;
privateJMenuItemlogItem;
privateJMenuItemaboutItem;
privateJRadioButtonMenuItemlevelItem[];
privateJCheckBoxMenuItemmarkCheckItem;
//Gameinformations
privateLevelInfolevelInfo;
privateintcurrentLevel;
privateLevelLoglog[];
privateLedNumberled;
privateGridMouseAdaptergma;
privateStatusMouseAdaptersma;
privateTimeThreadtimeThread;
publicMainFrame(){
super("扫雷");
//default
currentLevel=0;
levelInfo=LevelInfo.DEFAULT_LEVEL[currentLevel];
log=newLevelLog[3];
for(inti=0;i<3;i++)
log[i]=newLevelLog();
//readfromfile
readLog();
led=newLedNumber();
gma=newGridMouseAdapter();
sma=newStatusMouseAdapter();
//setupmenus
setMenuBar();
setStatusPanel();
resetPane();
addWindowListener(
newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
writeLog();
System.exit(0);
}
}
);
setIconImage(ImageFactory.getInstance().getImageicon(16).getImage());
setResizable(false);
}
//executionapplication
publicstaticvoidmain(Stringargs[]){
MainFrameapplication=newMainFrame();
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exceptione){}
}
//fileoperation
privatevoidreadLog(){
try{
FilelogFile=newFile("mine.log");
if(!
logFile.exists())return;
ObjectInputStreamois=newObjectInputStream(newFileInputStream(logFile));
for(inti=0;i<3;i++){
log[i]=(LevelLog)ois.readObject();
}
markCheck=ois.readBoolean();
currentLevel=ois.readInt();
if(currentLevel==3){
levelInfo=(LevelInfo)ois.readObject();
}else{
levelInfo=LevelInfo.DEFAULT_LEVEL[currentLevel];
}
ois.close();
}catch(Exceptione){System.out.println("readfail");}
}
privatevoidwriteLog(){
try{
FilelogFile=newFile("mine.log");
ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream(logFile));
for(inti=0;i<3;i++){
oos.writeObject(log[i]);
}
oos.writeBoolean(markCheck);
oos.writeInt(currentLevel);
if(currentLevel==3){
oos.writeObject(levelInfo);
}
oos.close();
}catch(Exceptione){System.out.println("writefail");}
}
/*
*addstatuslabeltostatuspanel
*/
privatevoidsetStatusPanel(){
JPaneltemp;
statusPanel=newJPanel(newBorderLayout());
mineLabel=newJLabel();
mineLabel.setBorder(BorderFactory.createLoweredBevelBorder());
temp=newJPanel(newFlowLayout(1,4,4));
temp.add(mineLabel);
temp.setBackground(Color.LIGHT_GRAY);
statusPanel.add(temp,BorderLayout.WEST);
timeLabel=newJLabel();
timeLabel.setBorder(BorderFactory.createLoweredBevelBorder());
temp=newJPanel(newFlowLayout(1,4,4));
temp.add(timeLabel);
temp.setBackground(Color.LIGHT_GRAY);
statusPanel.add(temp,BorderLayout.EAST);
statusButton=newJLabel();
statusButton.setBorder(BorderFactory.createRaisedBevelBorder());
statusButton.addMouseListener(sma);
temp=newJPanel(newFlowLayout(1,0,4));
temp.setBackground(Color.LIGHT_GRAY);
temp.add(statusButton);
statusPanel.add(temp,BorderLayout.CENTER);
statusPanel.setSize(10,37);
status