走迷宫游戏的JAVA实现86630.docx

上传人:b****6 文档编号:4049788 上传时间:2022-11-27 格式:DOCX 页数:16 大小:17.68KB
下载 相关 举报
走迷宫游戏的JAVA实现86630.docx_第1页
第1页 / 共16页
走迷宫游戏的JAVA实现86630.docx_第2页
第2页 / 共16页
走迷宫游戏的JAVA实现86630.docx_第3页
第3页 / 共16页
走迷宫游戏的JAVA实现86630.docx_第4页
第4页 / 共16页
走迷宫游戏的JAVA实现86630.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

走迷宫游戏的JAVA实现86630.docx

《走迷宫游戏的JAVA实现86630.docx》由会员分享,可在线阅读,更多相关《走迷宫游戏的JAVA实现86630.docx(16页珍藏版)》请在冰豆网上搜索。

走迷宫游戏的JAVA实现86630.docx

走迷宫游戏的JAVA实现86630

//MazeWindow.java

importjava.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

importjavax.swing.filechooser.*;

publicclassMazeWindowextendsJFrameimplementsActionListener{

Mazemaze;

JMenuBarbar;

JMenumenuChoice,menuImage;

JMenuItemwallImage,roadImage,defaultImage;

FilemazeFile,wallImageFile,roadImageFile;

JButtonrenew;

MazeWindow(){

wallImageFile=newFile("wall.jpg");

roadImageFile=newFile("road.jpg");

bar=newJMenuBar();

menuChoice=newJMenu("选择迷宫");

Filedir=newFile(".");

Filefile[]=dir.listFiles(newFilenameFilter(){

publicbooleanaccept(Filedir,Stringname){

returnname.endsWith("maze");

}

})

for(inti=0;i

{JMenuItemitem=newJMenuItem(file[i].getName());

item.addActionListener(this);

menuChoice.add(item);

}

mazeFile=newFile(file[0].getName());

init();

menuImage=newJMenu("选择墙和路的图像(JPG,GIF)");

wallImage=newJMenuItem("墙的图像");

roadImage=newJMenuItem("路的图像");

defaultImage=newJMenuItem("墙和路的默认图像");

menuImage.add(wallImage);

menuImage.add(roadImage);

menuImage.add(defaultImage);

bar.add(menuChoice);

bar.add(menuImage);

setJMenuBar(bar);

wallImage.addActionListener(this);

roadImage.addActionListener(this);

defaultImage.addActionListener(this);

renew=newJButton("重新开始");

renew.addActionListener(this);

add(maze,BorderLayout.CENTER);

add(renew,BorderLayout.SOUTH);

setVisible(true);

setBounds(60,60,510,480);

validate();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicvoidinit(){

if(maze!

=null){

remove(maze);

remove(maze.getHandleMove());

}

maze=newMaze();

maze.setWallImage(wallImageFile);

maze.setRoadImage(roadImageFile);

maze.setMazeFile(mazeFile);

add(maze,BorderLayout.CENTER);

add(maze.getHandleMove(),BorderLayout.NORTH);

validate();

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==roadImage){

JFileChooserchooser=newJFileChooser();

FileNameExtensionFilterfilter=newFileNameExtensionFilter(

"JPG&GIFImages","jpg","gif");

chooser.setFileFilter(filter);

intstate=chooser.showOpenDialog(null);

Filefile=chooser.getSelectedFile();

if(file!

=null&&state==JFileChooser.APPROVE_OPTION){

roadImageFile=file;

maze.setRoadImage(roadImageFile);

}

}

elseif(e.getSource()==wallImage){

JFileChooserchooser=newJFileChooser();

FileNameExtensionFilterfilter=newFileNameExtensionFilter(

"JPG&GIFImages","jpg","gif");

chooser.setFileFilter(filter);

intstate=chooser.showOpenDialog(null);

Filefile=chooser.getSelectedFile();

if(file!

=null&&state==JFileChooser.APPROVE_OPTION){

wallImageFile=file;

maze.setWallImage(wallImageFile);

}

}

elseif(e.getSource()==defaultImage){

wallImageFile=newFile("wall.jpg");

roadImageFile=newFile("road.jpg");

maze.setWallImage(wallImageFile);

maze.setRoadImage(roadImageFile);

}

elseif(e.getSource()==renew){

init();

}

else{JMenuItemitem=(JMenuItem)e.getSource();

mazeFile=newFile(item.getText());

init();

}

}

publicstaticvoidmain(Stringargs[]){

newMazeWindow();

}

}

//Maze.java

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjava.io.*;

publicclassMazeextendsJLayeredPane{

FilemazeFile;

MazePoint[][]mazePoint;

WallOrRoad[][]wallOrRoad;

PersonInMazeperson;

HandleMovehandleMove;

FilewallImage,roadImage;

intdistance=26,m=0,n=0;

publicMaze(){

setLayout(null);

wallImage=newFile("wall.jpg");

roadImage=newFile("road.jpg");

person=newPersonInMaze();

handleMove=newHandleMove();

handleMove.initSpendTime();

person.addKeyListener(handleMove);

setLayer(person,JLayeredPane.DRAG_LAYER);

}

publicvoidsetMazeFile(Filef){

mazeFile=f;

char[][]a;

RandomAccessFilein=null;

StringlineWord=null;

try{

in=newRandomAccessFile(mazeFile,"r");

longlength=in.length();

longposition=0;

in.seek(position);

while(position

Stringstr=in.readLine().trim();

if(str.length()>=n)

n=str.length();

position=in.getFilePointer();

m++;

}

a=newchar[m][n];

position=0;

in.seek(position);

m=0;

while(position

Stringstr=in.readLine();

a[m]=str.toCharArray();

position=in.getFilePointer();

m++;

}

in.close();

wallOrRoad=newWallOrRoad[m][n];

for(inti=0;i

for(intj=0;j

wallOrRoad[i][j]=newWallOrRoad();

if(a[i][j]=='1'){

wallOrRoad[i][j].setIsWall(true);

wallOrRoad[i][j].setWallImage(wallImage);

wallOrRoad[i][j].repaint();

}

elseif(a[i][j]=='0'){

wallOrRoad[i][j].setIsRoad(true);

wallOrRoad[i][j].setRoadImage(roadImage);

wallOrRoad[i][j].repaint();

}

elseif(a[i][j]=='*'){

wall0rRoad[i][j].setIsEnter(true);

wall0rRoad[i][j].setIsRoad(true);

wall0rRoad[i][j].repaint();

}

elseif(a[i][j]=='#'){

wallOrRoad[i][j].setIsOut(true);

wallOrRoad[i][j].setIsRoad(true);

wallOrRoad[i][j].repaint();

}

}

}

mazePoint=newMazePoint[m][n];

intHspace=distance,Vspace=distance;

for(inti=0;i

for(intj=0;j

mazePoint[i][j]=newMazePoint(Hspace,Vspace);

Hspace=Hspace+distance;

}

Hspace=distance;

Vspace=Vspace+distance;

}

for(inti=0;i

for(intj=0;j

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].setIsWallOrRoad(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();

}

}

}

handleMove.setMazePoint(mazePoint);

}

catch(IOExceptionexp){

JButtonmess=newJButton("无效的迷宫文件");

add(mess);

mess.setBounds(30,30,100,100);

mess.setFont(newFont("宋体",Font.BOLD,30));

System.out.println(exp+"mess");

}

}

publicvoidsetWallImage(Filef){

wallImage=f;

for(inti=0;i

for(intj=0;j

if(wallOrRoad[i][j].getIsWall())

wallOrRoad[i][j].setWallImage(wallImage);

wallOrRoad[i][j].repaint();

}

}

}

publicvoidsetRoadImage(Filef){

roadImage=f;

for(inti=0;i

for(intj=0;j

if(wallOrRoad[i][j].getIsRoad())

wallOrRoad[i][j].setRoadImage(roadImage);

wallOrRoad[i][j].repaint();

}

}

}

publicHandleMovegetHandleMove(){

returnhandleMove;

}

}

 

//WallOrRoad.java

importjavax.swing.*;

importjava.awt.*;

importjavax.swing.border.*;

importjava.io.*;

publicclassWallOrRoadextendsJPanel{

booleanisRoad,isWall,isEnter,isOut;

MazePointpoint;

FilewallImage,roadImage;

Toolkittool;

WallOrRoad(){

tool=getToolkit();

}

publicvoidsetIsEnter(booleanboo){

isEnter=boo;

if(isEnter==true)

add(newJLabel("入口"));

}

publicbooleangetIsEnter(){

returnisEnter;

}

publicvoidsetIsOut(booleanboo){

isOut=boo;

if(isOut==true)

add(newJLabel("出口"));

}

publicbooleangetIsOut(){

returnisOut;

}

publicvoidsetIsRoad(booleanboo){

isRoad=boo;

if(isRoad==true){

setBorder(null);

}

}

publicbooleangetIsRoad(){

returnisRoad;

}

publicvoidsetIsWall(booleanboo){

isWall=boo;

if(isWall==true)

setBorder(newSoftBevelBorder(BevelBorder.RAISED));

}

publicbooleangetIsWall(){

returnisWall;

}

publicvoidsetAtMazePoint(MazePointp){

point=p;

}

publicMazePointgetAtMazePoint(){

returnpoint;

}

publicvoidsetWallImage(Filef){

wallImage=f;

}

publicvoidsetRoadImage(Filef){

roadImage=f;

}

publicvoidpaintComponent(Graphicsg){

super.paintComponent(g);

intw=getBounds().width;

inth=getBounds().height;

try{

if(isRoad==true){

Imageimage=tool.getImage(roadImage.toURI().toURL());

g.drawImage(image,0,0,w,h,this);

}

elseif(isWall==true){

Imageimage=tool.getImage(wallImage.toURI().toURL());

g.drawImage(image,0,0,w,h,this);

}

}

catch(Exceptionexp){}

}

}

//MazePoint.java

publicclassMazePoint{

intx,y;

booleanhaveWallOrRoad;

WallOrRoadwallOrRoad=null;

publicMazePoint(intx,inty){

this.x=x;

this.y=y;

}

publicbooleanisHaveWallOrRoad(){

returnhaveWallOrRoad;

}

publicvoidsetIsWallOrRoad(booleanboo){

haveWallOrRoad=boo;

}

publicintgetX(){

returnx;

}

publicintgetY(){

returny;

}

publicbooleanequals(MazePointp){

if(p.getX()==this.getX()&&p.getY()==this.getY())

returntrue;

else

returnfalse;

}

publicvoidsetWallOrRoad(WallOrRoadobj){

wallOrRoad=obj;

}

publicWallOrRoadgetWallOrRoad(){

returnwallOrRoad;

}

}

//PersonInMaze.java

importjavax.swing.*;

importjava.awt.*;

publicclassPersonInMazeextendsJTextField{

MazePointpoint;

Toolkittool;

PersonInMaze(){

tool=getToolkit();

setEditable(false);

setBorder(null);

setOpaque(false);

setToolTipText("单击我,然后按键盘方向键");

}

publicvoidsetAtMazePoint(MazePointp){

point=p;

}

publicMazePointgetAtMazePoint(){

returnpoint;

}

publicvoidpaintComponent(Graphicsg){

super.paintComponent(g);

intw=getBounds().width;

inth=getBounds().height;

Imageimage=tool.getImage("person.gif");

g.drawImage(image,0,0,w,h,this);

}

}

//HandleMove.java

importjava.awt.event.*;

importjava.awt.*;

importjavax.swing.*;

publicclassHandleMoveextendsJPanelimplementsKeyListener,ActionListener{

MazePoint[][]p;

intspendTime=0;

javax.swing.TimerrecordTime;

JTextFieldshowTime;

Toolkittool;

HandleMove(){

recordTime=newjavax.swing.Timer(1000,this);

showTime=newJTextField(16);

tool=getToolkit();

showTime.setEditable(false);

showTime.setHorizontalAlignment(JTextField.CENTER);

showTime.setFont(newFont("楷体",Font.BOLD,16));

JLabelhitMess=newJLabel

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 政史地

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1