1、 Java语言及面向对象技术 课程设计报告 ( 2015 -- 2016年度第 1 学期) 题目 专业 软件工程 学生姓名 班级 软件141 学号 指导教师 徐 静 完成日期 2016/1/22 31 / 33 目 录 1 概 述 1 1.1 课程设计目的 1 1.2 课程设计内容 1 2 系统需求分析 1 2.1 系统目标 1 2.2 开发环境 1 3 系统概要设计 1 3.1迷宫文件 1 3.2 类的设计 2 4 系统详细设计 3 4
2、1 编写迷宫文件 3 4.2 MAZEWINDOW类 3 4.3 MAZE类 6 4.4 WALLORROAD类 8 4.5 MAZEPOINT类 10 4.6 PERSONINMAZE类 11 4.7 HANDLEMOVE类 12 4.8 RECORD类 14 4.9 SHOWRECORD类 16 4.10 SOUND类 17 4.11 所需图像和音乐 17 5 测试 18 5.1 测试方案 18 5.2 测试结果 18 6 小结 24 参考文献 25 附 录 26 附录 源程序清单 26 走迷宫 1 概 述
3、 1.1 课程设计目的 1.2 课程设计内容 2 系统需求分析 2.1 系统目标 2.2 开发环境 JDK ,Eclipse 3 系统概要设计 3.1 3.2 类的设计 3.2.1类的组合 3.2.2 MazeWindow.java类(主类) 3.2.3 Maze.java 3.2.4 WallOrRoad.java 3.2.5 MazePoint.java 3.2.6 PersonInMaze.java 3.2.7 HandleMove.java 3.2.8 Record.java 3.3.9 ShowReco
4、rd.java 3.3.10 Sound.java 4 系统详细设计 6 小结 参考文献 [1]耿祥义,张跃平. Java2实用教程(第三版)[M] . 北京:清华大学出版社, 2006 [2]耿祥义. Java课程设计(第三版)[M] . 北京:清华大学出版社, 2004 [3]张跃平. Java2实用教程实验指导及习题解答(第三版)[M]. 北京:清华大学出版社, 2004 ……… 附 录 附录 源程序清单 MazeWindow类 package 走迷宫Java; import javax.
5、swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Hashtable; import javax.swing..*; public class MazeWindow extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 3362664598156059146L; Maze maze; JM
6、enuBar bar; JMenu menuChoice,menuImage,hero; JMenuItem wallImage,roadImage,personImage,defaultImage,heroItem,clearHero; ; File 英雄榜=new File("英雄榜.txt"); JButton renew; ShowRecord showRecord; static String[] mazeName; static String currentMap; @SuppressWarnings("rawtypes") H
7、ashtable hashtable=null; @SuppressWarnings({ "rawtypes", "unchecked" }) MazeWindow(){ wallImage File("wall.jpg");//创建wall.jpg的新文件 roadImage File("road.jpg");//创建road.jpg的新文件 bar=new JMenuBar(); menuChoice=new JMenu("选择迷宫"); File("."); File file[]=dir.listFiles(new ()
8、{
public boolean accept( name){
return name.endsWith("maze");
}
});
mazeName=new String[];
for(int i=0;i<)
{
mazeName[i]=file[i].getName().substring(0, file[i].getName().length()-5);//substring(截取的起始位置,截取的长度);
}
for(int i=0;i 9、nuItem item =new JMenuItem(mazeName[i]);
item.addActionListener(this);//MazeWindow类的实例(当前窗口)做监视器
menuChoice.add(item);
}
maze [0].getName());
currentMap=mazeName[0];
init();
menuImage=new JMenu("选择墙、路和走迷宫者的图像(JPG,GIF)");
wallImage=new JMenuItem("墙的图像");
road 10、Image=new JMenuItem("路的图像");
personImage=new JMenuItem("走迷宫者的图像");
defaultImage=new JMenuItem("墙、路的默认图像");
hero=new JMenu("英雄榜");
heroItem=new JMenuItem("查看英雄榜");
clearHero=new JMenuItem("清空英雄榜");
hero.add(clearHero);
hero.add(heroItem);
bar.add(hero);
menuImage.a 11、dd(wallImage);
menuImage.add(roadImage);
menuImage.add(personImage);
menuImage.add(defaultImage);
bar.add(menuChoice);
bar.add(menuImage);
setJMenuBar(bar);//将菜单条添加到窗口中
wallImage.addActionListener(this);
roadImage.addActionListener(this);
personImage.addActionList 12、ener(this);
heroItem.addActionListener(this);
clearHero.addActionListener(this);
defaultImage.addActionListener(this);
renew=new JButton("重新开始");
renew.addActionListener(this);
add(maze,BorderLayout.CENTER);
add(renew,BorderLayout.SOUTH);
hashtable=new Hashtable();
13、 for(int i=0;i<)
{
hashtable.put(mazeName[i]+"1",mazeName[i]+"#"+100+"#匿名1");
hashtable.put(mazeName[i]+"2",mazeName[i]+"#"+100+"#匿名2");
hashtable.put(mazeName[i]+"3",mazeName[i]+"#"+100+"#匿名3");
}
if(!英雄榜.exists()) {//英雄榜是File类对象; boolean exists() 测试此抽象路径名表示的文件或 14、目录是否存在。
try{ out=new (英雄榜);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(hashtable); //void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。
objectOut.close(); //void close() 关闭流。
out.close(); 15、 //java.io 类 的方法 void close() 关闭此文件输出流并释放及此流有关的所有系统资源。
}
catch(IOException e){}
}
showRecord =new ShowRecord(this,mazeName.length);
setVisible(true);
setBounds(60,60,510,480);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
pub 16、lic void init(){
if(maze!=null){
remove(maze);
remove(maze.getHandleMove());
}
maze=new Maze();//创建Maze类的对象
maze.setWallImage(wallImageFile);
maze.setRoadImage(roadImageFile);
maze.person.setPersonImage(personImageFile);
maze.setMaze);
add(maze,BorderLayout. 17、CENTER);
add(maze.getHandleMove(),BorderLayout.NORTH);
validate();
}
public void actionPerformed(ActionEvent e){//重写接口中的方法
if(e.getSource()==roadImage){
J chooser=new J();
filter=new ("JPG&GIF Images","jpg","gif");
chooser.set(filter);
int state=chooser.show 18、OpenDialog(null);
();
if(){
roadImage;
maze.setRoadImage(roadImageFile);
}
}
else if(e.getSource()==wallImage){
J chooser=new J();
filter=new ("JPG&GIF Images","jpg","gif");
chooser.set(filter);
int state=chooser.showOpenDialog(null);
();
19、
if(){
wallImage;
maze.setWallImage(wallImageFile);
}
}
else if(e.getSource()==personImage){
J chooser=new J();
filter=new ("JPG&GIF Images","jpg","gif");
chooser.set(filter);
int state=chooser.showOpenDialog(null);
();
if(){
20、 personImage;
maze.person.setPersonImage(personImageFile);
}
}
else if(e.getSource()==defaultImage){
wallImage File("wall.jpg");
roadImage File("road.jpg");
maze.setWallImage(wallImageFile);
maze.setRoadImage(roadImageFile);
}
else if(e.getSource( 21、)==heroItem){
showRecord.setMazeName(mazeName);
showRecord.readAndShow(hashtable);
showRecord.setVisible(true);
}
else if(e.getSource()==clearHero){
File("英雄榜.txt");
f.delete();
JFrame t=new 22、JFrame("提示!");
t.setFont(new Font("楷体",Font.BOLD,30));
t.setSize(300,215);
t.setLocationRelativeTo(null);
JPanel pan=new JPanel();
t.setContentPane(pan);
BorderLayout bLayout=new BorderLayout(20,20);
pan.setLayout(bLayout);
23、 JLabel leb=new JLabel("英雄榜已清空,请重新载入游戏。");
pan.add(leb,BorderLayout.CENTER);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
else if(e.getSource()==renew){
init();
}
else{ JMenuItem item=(JMenuItem)e.getSource();
maze ()+".maz 24、e");
currentMap=item.getText();
maze.getHandleMove().setMap(currentMap);
init();
}
}
public static void main(String[] args){
new MazeWindow();
}
}
Maze类
package 走迷宫Java;
import java.awt.*;
import javax.swing.*;
//import java.awt.event. 25、
import java.io.*;
public class Maze extends JLayeredPane{
/**
*
*/
private static final long serialVersionUID = 43668678L;
;
MazePoint [][] mazePoint;
WallOrRoad [][] wallOrRoad;
PersonInMaze person;
HandleMove handleMove;
;
int distance=26,m=0,n=0;
public Maze(){
26、setLayout(null);
wallImage=new File("wall.jpg");
roadImage=new File("road.jpg");
person=new PersonInMaze();
handleMove=new HandleMove();
handleMove.initSpendTime();
person.addKeyListener(handleMove);
setLayer(person,JLayeredPane.DRAG_LAYER);//设置组件person在分 27、层窗口的最上面的一层
}
public void setMaze f){
mazeFile=f;
char [][]a;
RandomAccess;
//String lineWord=null;
try{ in=new RandomAccess,"r");
long length=in.length();//获取文件mazeFlie的长度
long position=0;
in.seek(position);//将读取位置定位到文件的起始
while(posi 28、tion 29、position);//将读取位置定位到文件的起始
m=0;
while(position 30、i 31、][j]=='0'){
wallOrRoad[i][j].setIsRoad(true);
wallOrRoad[i][j].setRoadImage(roadImage);
wallOrRoad[i][j].repaint();
}
else if(a[i][j]=='*'){
wallOrRoad[i][j].setIsEnter(true);
wallOrRoad[i][j].setIsRoad(true);
32、 wallOrRoad[i][j].repaint();
}
else if(a[i][j]=='#'){
wallOrRoad[i][j].setIsOut(true);
wallOrRoad[i][j].setIsRoad(true);
wallOrRoad[i][j].repaint();
}
}
}
mazePoint=new MazePoint[m][n];
int 33、 Hspace=distance,Vspace=distance;
for(int i=0;i 34、 add(wallOrRoad[i][j]);
wallOrRoad[i][j].setSize(distance,distance); wallOrRoad[i][j].setLocation(mazePoint[i][j].getX(),mazePoint[i][j].getY());
wallOrRoad[i][j].setAtMazePoint(mazePoint[i][j]);
mazePoint[i][j].setWallOrRoad(wallOrRoad[i][j]);
mazePoint[i][j].setI 35、sWallOrRoad(true);
if(wallOrRoad[i][j].getIsEnter()){
person.setAtMazePoint(mazePoint[i][j]);
add(person);
person.setSize(distance,distance);
person.setLocation(mazePoint[i][j].getX(),mazePoint[i][j].getY());
person.requestFocus();
person.repaint();
36、 }
}
}
handleMove.setMazePoint(mazePoint);
}
catch(IOException exp){
JButton mess =new JButton("无效的迷宫文件");
add(mess);
mess.setBounds(30,30,100,100);
mess.setFont(new Font("宋体",Font.BOLD,30));
System.out.println(exp+"mess");
}
}
public voi 37、d setWallImage(File f){
wallImage=f;
for(int i=0;i 38、
roadImage=f;
for(int i=0;i 39、 handleMove;
}
}
WallOrRoad类
package 走迷宫Java;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.io.*;
public class WallOrRoad extends JPanel{
/**
*
*/
private static final long serialVersionUID = 48249320L;
boolean isRoad,isWall,i 40、sEnter,isOut,havePassed;
MazePoint point;
;
Toolkit tool;
WallOrRoad(){
tool=getToolkit();
}
public void setIsEnter(boolean boo){
isEnter=boo;
if(isEnter==true)
add(new JLabel("入口"));//定义迷宫的入口
}
public boolean getIsEnter(){
re 41、turn isEnter;
}
public void setIsOut(boolean boo){
isOut=boo;
if(isOut==true)
add(new JLabel("出口"));//定义迷宫的出口
}
public boolean getIsOut(){
return isOut;
}
public void setIsRoad(boolean boo){
isRoad=boo;
42、 if(isRoad==true){
setBorder(null);//置此组件的边框,无
}
}
public boolean getIsRoad(){
return isRoad;
}
public void setIsWall(boolean boo){
isWall=boo;
if(isWall==true)
setBorder(new SoftBevelBorder(BevelBorder.RAISED));/* SoftBevelBorder 43、是实现某种斜面的类,这种斜面要么凸出要么凹入且拐角圆滑,BevelBorder该类实现简单的双线斜面边框,RAISED:凸出斜面类型。*/
}
public boolean getIsWall(){
return isWall;
}
public void setAtMazePoint(MazePoint p){
point=p;
}
public MazePoint getAtMazePoint(){
return point;
}
public void setWallImag 44、e(File f){
wallImage=f;
}
public void setRoadImage(File f){
roadImage=f;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int w=getBounds().width;
int h=getBounds().height;
try{
45、 if(isRoad==true){
Image image=tool.getImage(roadImage.toURI().toURL());
g.drawImage(image,0,0,w,h,this);
}
else if(isWall=true){
Image image=tool.getImage(wallImage.toURI().toURL());
g.drawImage(image,0 46、0,w,h,this);
}
}
catch(Exception exp){}
}
}
MazePoint类
package 走迷宫Java;
public class MazePoint{
int x,y;
boolean haveWallOrRoad;
WallOrRoad wallOrRoad=null;
public MazePoint(int x,int y){
this.x=x;
this.y=y;
}
public 47、boolean isHaveWallOrRoad(){
return haveWallOrRoad;
}
public void setIsWallOrRoad(boolean boo){
haveWallOrRoad=boo;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public boolean equals(MazePoint p){
if(p.g 48、etX()==this.getX()&&p.getY()==this.getY())
return true;
else
return false;
}
public void setWallOrRoad(WallOrRoad obj){
wallOrRoad=obj;
}
public WallOrRoad getWallOrRoad(){
return wallOrRoad;
}
}
PersonInMaze类
package 走迷宫Java;
import 49、 javax.swing.*;
import java.awt.*;
import java.io.File;
public class PersonInMaze extends JTextField{
/**
*
*/
private static final long serialVersionUID = 1L;
MazePoint point;
String person="person.jpg";
Toolkit tool;
;
PersonInMaze(){
tool=getToolkit();
setEditable(fa 50、lse);
setBorder(null);
setOpaque(false);
setToolTipText("单击我,然后按键盘方向键");
}
public void setAtMazePoint(MazePoint p){
point=p;
}
public MazePoint getAtMazePoint(){
return point;
}
public void setPersonImage(File f){
personImage=f;
}
public void paintComponent(Graphi






