Java应用技术实训报告.docx





《Java应用技术实训报告.docx》由会员分享,可在线阅读,更多相关《Java应用技术实训报告.docx(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Java应用技术实训报告-.总体设计本系统共包括3个java源文件和4个图像文件。1 .类之间的关系如图L1所示。图1.1类之的关系2. java源文件及其功能如友1所示。表1源文件及其功能序号文件名主要功能1Main, java生成的类负责创建程序窗口,含有main方法,游戏从该 类开始执行。2MainBomb, java为主类创建整个挖雷游戏的对象,包括窗口,布雷,判断 等等。3Bomb, java负责创建游戏所需要的按钮。3 .图像文件Bomb, gif:地雷方块上的图标。question, gif:疑惑探雷是否正确。 bomb_big.gif:触雷时方块上的图标。 flag, gif:
2、探雷标记。二、详细设计1 .主类Main(1)成员变量(见表2)表2成员变量描述变量类型备注游戏运行MainBomb临时变量(2)成员变量(见表3)表3名称功能备注main游戏开始(3)源代码见文件Main, javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;尸主类*/public class Mainpublic static void main(String args) (new MainBomb( ).show();2 .挖雷游戏MainBomb(1)成员变量(见表4)表4成员变量描述变量类型名称雷数设置
3、JTextFieldtext当前方块数intBlockNum当前雷数intBombNum开始JButtonstart踩雷Iconiconbomb踩雷标记Iconicon_bomb_big雷标记Iconicon_flag疑惑是否有雷Iconicon_question目录面板PanelMenuParoel主面板PanelmainPancl雷BombbombButton该方块是否有雷boolean周围无雷方块周围的雷数int周围的雷数方块上的雷标记int探雷标记显示方块信息JLabelLabel方块在雷区的位置PointP布雷RandomSetset判断标记是否正确DetermineDetermin
4、e是否获得胜利WinnerWin雷标数目intcount周围方块的宙数intcount1(2)方法见(表5)表5方法名功能备注Game创建程序窗口barnewGame新开局actionPerformed处理ActionEvent事件接口方法设置方块是否有雷没置雷的值设置是雷时的图标没置label上的图标设置无雷时的图标设置label上的图标设置有雷时的名字设置label上的文字设置周围的雷数设置周围的雷数获取名字返回方块label上的文字获取周围雷数返回周围的雷数设置雷区位置设置属性P获取雷区位置返回属性P获取探雷标记返回探雷标记mousePressed处理鼠标按下事件接口方法moused i
5、cked处理鼠标双击事件接II方法mouseReleased处理鼠标释放事件接II方法mouscExitcd处理鼠标退出事件接口方法rnouseEntered处理鼠标进入事件接口方法Mi nesSquare创建雷区gameOver游戏失败判断雷标是否正确正确返回true错误返回false布雷随机布雷判断是否成功成功返Pl true失败返1nl false(3)源代码见文件MainBomb. javaimport java awt. *;import java, awt event. *;import javax. swing. *;/*窗口及算法实现类*/class MainBomb exte
6、nds JFrame implements ActionListener, MouseListenerpublic JTextField text;public Label nowBomb, setBomb;public int BlockNum, BombNum;/当前方块数当前雷数public Icon icon_bombnew Imageicon(Bomb, gif);踩雷public Icon iconbomb bignew ImageIcon(bomb_big gif);踩雷标记public Icon icon_flagnew Image Icon (*flag, gif*);雷标记
7、public Iconicon_question = new Imagclcon(*question. gif*);疑惑是否有雷public JButton start = new JButton(* 开始);public Panel McnuPamcl = now Panel();public Panel mainPanel 二 new Panel 0;public Bomb bombButton;/*界面设计*/public MainBomb()super C成型04-2李超JAVA课程设计);BlockNum = 64;BombNum = 10;Container c=gctConten
8、tPano();c. setBackground(Color, gray);c. setLayout(new BordcrLayout ();text=new JTextField(*10 3);nowBomb - new Label (当前雷数+十BouibNum+);setBomb= new Label (设置地雷数);start. addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)BombNum = Integer, parselnt (text. get.Text (). tr
9、imO);if(BombNum =】0 & BombNum 50 )replay。;elseJOptionPanc msg = new JOptionPane();JOptionPane. showMessageDialog(nulL 您设置的地雷数太多了,请重 设!错误12);MenuPamel. add(setBomb);MenuPameL add(text);MenuPameL add(start);MenuPamol. add(nowBomb);c. add(MenuPamol, *North*);mainPanel, setLayout(new GridLayout( (int)Ma
10、th. sqrt(BlockNum) (int)Math. sqrt(BlockNum);bombButton=new Bomb (int)Math, sqrt(BlockNum);for(int i = 0 ; i (int)Math. sqrt(BlockNum) ; i+)bombButton i =new Bomb (int)Math, sqrt(BlockNum);for(int i w O ;for(int j - 0i (int)Math. sqrt(BlockNum) ; i+ )j (int)Math, sqrt(BlockNum) ; j+ )bombButtonj =ne
11、w Bomb(i, j);bombButtonj . setForeground( Color, gray);bombButtonj . addActionListener(this);bombButtonj . addMouseListener(this);for(int i = 0 *i (int)Math, sqrt(BlockNum) : i+ )for(int j = 0 ; j (int)Math, sqrt(BlockNum) ; j+ )mainPaneL add(bombButton i j );c, add(mainPanel, Center);startBombO ;se
12、tSize (400,400):$etLocation(350, 200);setRe$izable(false):/*布雷本/public void startBombO for(int i=0;iBombNum;i+)int x =(int)(Math, random()*(int)(Math, sqrt(BlockNum)-l); int y =(int) (Math, random()*(int) (Math. sqrt(BlockNum)-l);if(bombButton x y L isBomb=true) i;elsebombButton x y isBomb=truc ;重新开
13、始*/public void replay()nowBomb. setText (当前雷数+ *+BombNum+*);for(int i = 0 ; i (int)Math, sqrt(BlockNum) : i+)for(int j = 0 ; j (int)Math. sqrt(BlockNum) : j+)bombButtonfj . isBomb=false;bombButton i j . isClicked=false;bombButton i j . setEnabled(true);bombButton i j . setText(*);bombButton i j . se
14、tlcon(nul1);startBombO ;/*是否挖完了所有的雷*/public void isWinOint findBomb=0; 找到的地雷数for(int i =0;i (int)Math. sqrt(BlockNum); i+)for(int j=0;j (int)Math, sqrt(BlockNum ); j+)”.isRightif (bombButton i j isBomb = true & bombButton i =true)findBomb+;if ( findBomb = Integer, parselnt (text. getTextO. trimO)JOp
15、tionPane msg = new JOptionPane0;JOptionPane. showMessageDialog(this,您挖完了所有的雷,您胜利了!,您 胜利了 ,2):1/*计算方块周围雷数*/public void CountRoundBomb()Ifor (int i=0; i (int)Math, sqrt(BlockNum); i+)for (int j = 0; j = 0) & (j - 1 = 0) if (bombButtoni - 1j - 1.isBomb = true) count += 1; /检测左上方空格是否是地雷if ( (i - 1 =() i
16、f (bombButtoni - 1 j , isBomb = true) count += 1: 检测上方空格是否为地雷if (i - l = 0) M (j + 1 = 0) & (j + 1 = 0) & (i + 1 = (int)Math, sqrt(BlockNum)-l) if (bombButtoni + 1j - 1. isBomb = true) count +- 1; 左下if ( (i + 1 = (int)Math, sqrt(BlockNum)-l) if (bombButtoni + 1J j . isBomb true) count += 1; 下if ( (j
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 应用技术 报告

限制150内