资源描述
佛山科学技术学院
《可视化编程技术》课程设计汇报
计算器软件设计
学生姓名: 吴晓莉
学 号: 914210
年级专业:11级教育技术学2班
指导老师: 容汝佳
学 院: 教育科学学院
广东★佛山
提交日期:6月
目 录
1. 序言………………………………………………………………………………2
2.概要设计…………………………………………………………………………3
2.1 开发环境 ……………………………………………………………………3
2.2 游戏功效 ……………………………………………………………………3
2.3 界面设计 ……………………………………………………………………3
2.4 类框架结构图 ……………………………………………………………4
3. 具体设计 ………………………………………………………………………4
3.1 使用JAVA类说明 ……………………………………………………4
3.2 类关键方法 ………………………………………………………………5
3.2.1 处理按了运算符后计算 ……………………………………………5
3.2.2 处理输入字符 …………………………………………………………6
3.2.3 进制转换 ……………………………………………………………6
3.2.4 设计总体认识 …………………………………………………………7
3.2.5 程序步骤图 ……………………………………………………………8
4. 运行结果…………………………………………………………………………8
5. 测试分析…………………………………………………………………………9
5.1 程序运行情况………………………………………………………………9
5.2 程序异常处理………………………………………………………………9
6. 源程序……………………………………………………………………………9
参考文件……………………………………………………………………………27
设计总结……………………………………………………………………………27
摘要:该程序是一个扫雷小游戏,含有赶快简练游戏界面,玩者只要是曾经玩过扫雷游戏,那么我这个游戏是很轻易上手。该游戏含有三种模式:简单模式,中等模式和复杂模式。游戏界面上有一个显示剩下雷数目标小方框,还有一个计时小方框。中间是一个“重新玩按钮”,选择菜单栏有重新玩、能够选择模式、还能退出。
关键字:java扫雷,游戏类,方法,事件
1 序言
学生学习压力越来越大,应该有适时娱乐放松,,所以我在学习了java语言以后,决定用java语言编写一个简单扫雷小游戏,能够实现简单娱乐功效,且比一般扫雷游戏增加部分功效。
Java是由Sun企业开发新一代纯面向对象网络编程语言。其目标是建立一个在任意种机器、任一个操作系统网络环境中运行软件,实施所谓“程序写一次,四处运行”目标。正因为如此,Java已成为当今Internet上最流行、最受欢迎一个程序开发语言。
Java开发小组把Java按特征分为基础版、移动版、企业版,每个版本有一个软件开发包。Java基础版本叫Java 2标准版(Java 2 Standard Edition,J2SE),它包含建立Java应用程序或是Applet所需应用程序编程接口(API)。Java 2移动版(The Java 2 Mobile Edition,J2ME)包含创建无线Java应用程序API。还有Java 2企业版(The Java 2 Enterprise,J2EE)是J2SE增强版本,包含建立多层架构应用程序API。
Java语言是由C++语言发展起而来,是一个根本面向对象程序设计语言。作为一个纯面向对象程序设计语言,它很适合大型软件开发。Java语言去掉了C++语言部分轻易引发错误特征。Java语言特点有:面向对象、跨平台、安全性、多线程和图形功效强。
2 概要设计
2.1开发环境
开发平台:Microsoft Windows XP Professional Service Pack 2
开发工具:JBuilder + JDK 1.6.0_02
2.2游戏功效
功效 1:打开游戏以后,会默认进入疯狂模式,能够经过菜单栏选择不一样游戏模式。
功效 2:游戏界面左边有个小框,用来显示目前界面剩下雷数目。
功效 3:游戏界面右边有一个小框,用来显示目前游戏已经进行了时间(秒)。
功效 4:游戏界面中间有一个按钮,点击能够重新开始游戏。
功效 5:菜单栏二级菜单有选项能够退出游戏。
2.3界面设计
图2.3.1 游戏面板(疯狂模式)
图2.3.2 游戏面板(中等模式)
图2.3.3 游戏面板(简单模式)
2.4类框架结构图
BottomPanel 图 2.4.1 所表示
CenterPanel 图 2.4.2 所表示
Display 图 2.4.3 所表示
Field 图 2.4.4 所表示
Digit 图 2.4.5 所表示
J_sweep 图 2.4.6 所表示
NorthPanel 图 2.4.7 所表示
BottomPanel
属性
private static final long serialVersionUID
操作
public BottomPanel()
图 2.4.1
CenterPanel
属性
private static final long serialVersionUID = 1L;
private int countX, countY, fieldsLeft;
private Field[][] fields;
private Difficulty difficulty;
private Random random;
private boolean gameOver;
private J_sweep minesweeper;
public CenterPanel(J_sweep minesweeper, Difficulty difficulty)
public void setMinesweeper(Difficulty difficulty)
private void setFields()
public void paint(Graphics g)
private void setGameOver()
private void setWon()
private void performFieldClick(int x, int y)
private void performFieldClickFlagged(int x, int y)
private boolean isNeighbourMinesFlagged(int x, int y)
public void mousePressed(MouseEvent e)
图 2.4.2
Display
属性
private static final long serialVersionUID = 1L;
private Digit digit0, digit1, digit2;
private int value;
操作
public Display(int value)
public void setValue(int value)
public int getValue()
public void paint(Graphics g)
图 2.4.3
Field
属性
private int x, y, neighbourMines;
private boolean clicked, mine, flag, mineClicked;
private Color color1, color2, color3, color4,color5, color6, color7, color8;
操作
public Field(int x, int y)
public boolean setClicked()
public boolean isClicked()
public void setMine(boolean mine)
public void incrementNeighbourMines()
public int getNeighbourMines()
public int setFlag()
public boolean isFlag()
public void setGameOver()
public void setWon()
public void paint(Graphics g)
图 2.4.4
Digit
属性
private int digit, x, y, width, height;
private Color darkRed;
操作
public Digit(int digit, int x, int y)
public void setDigit(int digit)
public void paint(Graphics g)
图 2.4.5
J_sweep
属性
private static final long serialVersionUID
private NorthPanel northPanel;
private CenterPanel centerPanel;
private BottomPanel bottomPanel;
private JCheckBoxMenuItem miBeginner,miIntermediate, miExpert;
private Difficulty difficulty;
public J_sweep()
public void startTime()
public void stopTime()
public void setMines(int value)
public void setMines(int value, boolean real)
public void setMainMenuDifficulty()
public void actionPerformed(ActionEvent e)
public static void main(String[] args)
图 2.4.6
NorthPanel
private static final long serialVersionUID
private J_sweep minesweeper;
private Display mineDisplay, timeDisplay;
private long begin;
private Timer timer;
public NorthPanel(J_sweep minesweeper, Difficulty difficulty)
public void setMinesweeper(Difficulty difficulty)
public void setMines(int value, boolean real)
public void startTime()
public int stopTime()
public void actionPerformed(ActionEvent e)
图 2.4.7
3具体设计
3.1 扫雷游戏使用Java类说明
类名: BottomPanel
作用: 这是设计整个界面底部颜色
继承父类: JPanel类
实现接口: 没有
public class BottomPanel extends JPanel {
private static final long serialVersionUID = -29266788L;
public BottomPanel() {
super();
setBackground(Color.white);//这是设计整个界面底部颜色
}
}
类名: J_sweep
作用: 主类。
继承父类: JFrame类
实现接口: ActionListener类
public class J_sweep extends JFrame implements ActionListener
{
private static final long serialVersionUID = 5319706L;//生成序列号,不用管它
private NorthPanel northPanel;
private CenterPanel centerPanel;
private BottomPanel bottomPanel;
private JCheckBoxMenuItem miBeginner, miIntermediate, miExpert;
private Difficulty difficulty;
public J_sweep()
{
super("wxl_扫雷");//软件标题
setDefaultCloseOperation(EXIT_ON_CLOSE);//退出按钮
/*
resizeable值为true时,表示在生成窗体能够自由改变大小;
resizeable值为false时,表示生成窗体大小是由程序员决定,用户不能够自由改变该窗体大小
*/
setResizable(false);
difficulty = Difficulty.疯狂模式;//默认定义最高难度
JMenuBar menuBar = new JMenuBar();//菜单栏控件
JMenu menu = new JMenu("文件");//菜单栏菜单项
JMenuItem menuItem = new JMenuItem("重新玩");//菜单栏下拉菜单
menuItem.addActionListener(this);//给下拉框加上事件捕捉
menu.add(menuItem);//下拉框放入下拉菜单控件
menu.addSeparator();//加个空格线
miBeginner = new JCheckBoxMenuItem("简单模式");//选择框 下面一样,不解释!
miBeginner.addActionListener(this);
menu.add(miBeginner);
miIntermediate = new JCheckBoxMenuItem("中等模式");
miIntermediate.addActionListener(this);
menu.add(miIntermediate);
miExpert = new JCheckBoxMenuItem("疯狂模式", true);
miExpert.addActionListener(this);
menu.add(miExpert);
menu.addSeparator();
menuItem = new JMenuItem("退出");
menuItem.addActionListener(this);
menu.add(menuItem);
menuBar.add(menu);
setJMenuBar(menuBar);//把菜单栏加入面板
northPanel = new NorthPanel(this, Difficulty.疯狂模式);//定义一个面板
add(northPanel, BorderLayout.NORTH);//把这个面板放在上面
centerPanel = new CenterPanel(this, Difficulty.疯狂模式);
add(centerPanel, BorderLayout.CENTER);//把这个面板放在中间
/*调整此窗口大小,以适合其子组件首选大小和布局 XJQ*/
bottomPanel = new BottomPanel();
add(bottomPanel , BorderLayout.SOUTH);
pack();
//设置窗口相对于指定组件位置
setLocationRelativeTo(null);
//设置可视化
setVisible(true);
}
类名: CenterPanel
作用: 设置中间面板
继承父类: JPanel类
实现接口: MouseListener 类
public class CenterPanel extends JPanel implements MouseListener
{
private static final long serialVersionUID = 1L;
private int countX, countY, fieldsLeft;
private Field[][] fields;// 用数组定义界面横向和纵向
private Difficulty difficulty;//定义难度
private Random random;
private boolean gameOver;
private J_sweep minesweeper;
}
类名: Display
作用: 计时
继承父类: JPanel类
实现接口:没有
public class Display extends JPanel
{
private static final long serialVersionUID = 1L;
private Digit digit0, digit1, digit2;//定义了一个表示雷有、无、不确定量
private int value;
}
类名: Field
作用: 定义数字颜色
继承父类: 没有
实现接口:没有
public class Field
{
private int x, y, neighbourMines;
private boolean clicked, mine, flag, mineClicked;
private Color color1, color2, color3, color4, color5, color6, color7, color8;
// 定义了从1——8之间,八个数字颜色
}
类名NorthPanel
作用: 定义中间面板
继承父类: JPanel类
实现接口: ActionListener 类
public class NorthPanel extends JPanel implements ActionListener
{
private static final long serialVersionUID = 040336L;
private J_sweep minesweeper;
private Display mineDisplay, timeDisplay;
private long begin;
private Timer timer;
}
类名Digit
作用: 统计剩下雷数目
继承父类: 没有
实现接口:没有
public class Digit
{
private int digit, x, y, width, height;
private Color darkRed;//定义接下来用来绘制数字颜色
}
3.2类关键方法
3.2.1设计整个界面底部颜色:
public BottomPanel() {
super();
setBackground(Color.white);
}
3.2.2添加鼠标监听器:
public CenterPanel(J_sweep minesweeper, Difficulty difficulty)
{
super();
this.setBackground(Color.LIGHT_GRAY);
addMouseListener(this);
this.minesweeper = minesweeper;
this.difficulty = difficulty;
random = new Random();
setFields();
}
3.2.3 设置三种模式界面大小:
private void setFields()
{
int mines = 0;
switch (difficulty)
{
case 简单模式:
countX = countY = 12;//这是设置简单模式界面横竖全部有12个格子
mines = 15;//这是设置简单模式雷个数
break;
case 中等模式:
countX = countY = 16;//这是设置中等模式界面横竖全部有16个格子
mines = 40;//这是设置中等模式雷个数
break;
case 疯狂模式:
countX = 30;//这是设置疯狂模式界面横向有30个格子
countY = 16;//这是设置疯狂模式界纵向全部有16个格子
mines = 99;//这是设置疯狂模式雷个数
break;
}
3.2.4 绘制统计雷数目标数字
public Digit(int digit, int x, int y)
{
this.digit = digit;
this.x = x;
this.y = y;
width = 13;
height = 23;
darkRed = new Color(128, 0, 0);
}
public void setDigit(int digit)
{
this.digit = digit;
}
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(x, y, width, height);
switch (digit)
{
case 0: case 2: case 3: case 5: case 6: case 7: case 8: case 9:
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: case 1: case 4:
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;
}
switch (digit)
{
case -1: case 2: case 3: case 4: case 5: case 6: case 8: case 9:
g.setColor(Color.RED);
g.drawLine(x + 3, y + 10, x + 9, y + 10);
g.drawLine(x + 2, y + 11, x + 10, y + 11);
g.drawLine(x + 3, y + 12, x + 9, y + 12);
break;
case -2: case 0: case 1: case 7:
g.setColor(darkRed);
g.fillRect(x + 4, y + 10, 1, 1);
g.fillRect(x + 6, y + 10, 1, 1);
g.fillRect(x + 8, y + 10, 1, 1);
g.fillRect(x + 3, y + 11, 1, 1);
g.fillRect(x + 5, y + 11, 1, 1);
g.fillRect(x + 7, y + 11, 1, 1);
g.fillRect(x + 9, y + 11, 1, 1);
g.fillRect(x + 4, y + 12, 1, 1);
g.fillRect(x + 6, y + 12, 1, 1);
g.fillRect(x + 8, y + 12, 1, 1);
break;
}
switch (digit)
{
case 0: case 2: case 3: case 5: case 6: case 8: case 9:
g.setColor(Color.RED);
g.drawLine(x + 4, y + 19, x + 8, y + 19);
g.drawLine(x + 3, y + 20, x + 9, y + 20);
g.drawLine(x + 2, y + 21, x + 10, y + 21);
break;
case -2: case -1: case 1: case 4: case 7:
g.setColor(darkRed);
g.fillRect(x + 5, y + 19, 1, 1);
g.fillRect(x + 7, y + 19, 1, 1);
g.fillRect(x + 4, y + 20, 1, 1);
g.fillRect(x + 6, y + 20, 1, 1);
g.fillRect(x + 8, y + 20, 1, 1);
g.fillRect(x + 3, y + 21, 1, 1);
g.fillRect(x + 5, y + 21, 1, 1);
g.fillRect(x + 7, y + 21, 1, 1);
g.fillRect(x + 9, y + 21, 1, 1);
break;
}
switch (digit)
{
case 0: case 4: case 5: case 6: case 8: case 9:
g.setColor(Color.RED);
g.drawLine(x + 1, y + 2, x + 1, y + 10);
g.drawLine(x + 2, y + 3, x + 2, y + 9);
g.drawLine(x + 3, y + 4, x + 3, y + 8);
break;
case -2: case -1: case 1: case 2: case 3: case 7:
g.setColor(darkRed);
g.fillRect(x + 1, y + 2, 1, 1);
g.fillRect(x + 1, y + 4, 1, 1);
g.fillRect(x + 1, y + 6, 1, 1);
g.fillRect(x + 1, y + 8, 1, 1);
g.fillRect(x + 1, y + 10, 1, 1);
g.fillRect(x + 2, y + 3, 1, 1);
g.fillRect(x + 2, y + 5, 1, 1);
g.fillRect(x + 2, y + 7, 1, 1);
g.fillRect(x + 2, y + 9, 1, 1);
g.fillRect(x + 3, y + 4, 1, 1);
g.fillRect(x + 3, y + 6, 1, 1);
g.fillRect(x + 3, y + 8, 1, 1);
break;
}
switch (digit)
{
case 0: case 2: case 6: case 8:
g.setColor(Color.RED);
g.drawLine(x + 1, y + 12, x + 1, y + 20);
g.drawLine(x + 2, y + 13, x + 2, y + 19);
g.drawLine(x + 3, y + 14, x + 3, y + 18);
break;
case -2: case -1: case 1: case 3: case 4: case 5: case 7: case 9:
g.setColor(darkRed);
g.fillRect(x + 1, y + 12, 1, 1);
g.fillRect(x + 1, y + 14, 1, 1);
g.fillRect(x + 1, y + 16, 1, 1);
g.fillRect(x + 1, y + 18, 1, 1);
g.fillRect(x + 1, y + 20, 1, 1);
g.fillRect(x + 2, y + 13, 1, 1);
g.fillRect(x + 2, y + 15, 1, 1);
g.fillRect(x + 2, y + 17, 1, 1);
g.fillRect(x + 2, y + 19, 1, 1);
g.fillRect(x + 3, y + 14, 1, 1);
g.fillRect(x + 3, y + 16, 1, 1);
g.fillRect(x + 3, y + 18, 1, 1);
break;
}
switch (digit)
{
case 0: case 1: case 2: case 3: case 4: case 7: case 8: case 9:
g.setColor(Color.RED);
g.drawLine(x + 11, y + 2, x + 11, y + 10);
g.drawLine(x + 10, y + 3, x + 10, y + 9);
g.drawLine(x + 9, y + 4, x + 9, y + 8);
break;
case -2: case -1: case 5: case 6:
g.setColor(darkRed);
g.fillRect(x + 11, y + 2, 1, 1);
g.fillRect(x + 11, y + 4, 1, 1);
g.fillRect(x + 11, y + 6, 1, 1);
g.fillRect(x + 11, y + 8, 1, 1);
g.fillRect(x + 11, y + 10, 1, 1);
g.fillRect(x + 10, y + 3, 1, 1);
g.fillRect(x + 10, y + 5, 1, 1);
g.fillRect(x + 10, y + 7, 1, 1);
g.fillRect(x + 10, y + 9, 1, 1);
g.fillRect(x + 9, y + 4, 1, 1);
g.fillRect(x + 9, y + 6, 1, 1);
g.fillRect(x + 9, y + 8, 1, 1);
break;
}
switch (digit)
{
case 0: case 1: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
g.setColor(Color.RED);
g.drawLine(x + 11, y + 12, x + 11, y + 20);
g.drawLine(x + 10, y + 13, x + 10, y + 19);
g.drawLine(x + 9, y + 14, x + 9, y + 18);
break;
case -2: case -1: case 2:
g.setColor(darkRed);
g.fillRect(x + 11, y + 12, 1, 1);
g.fillRect(x + 11, y + 14, 1, 1);
g.fillRect(x + 11, y + 16, 1, 1);
g.fillRect(x + 11, y + 18, 1, 1);
g.fillRect(x + 11, y + 20, 1, 1);
g.fillRect(x + 10, y + 13, 1, 1);
g.fill
展开阅读全文