分享
分销 收藏 举报 申诉 / 22
播放页_导航下方通栏广告

类型JAVA实现走迷宫.doc

  • 上传人:快乐****生活
  • 文档编号:4374949
  • 上传时间:2024-09-14
  • 格式:DOC
  • 页数:22
  • 大小:196KB
  • 下载积分:10 金币
  • 播放页_非在线预览资源立即下载上方广告
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    JAVA 实现 迷宫
    资源描述:
    //MazeWindow 主类 import javax、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{ Maze maze; JMenuBar bar;//声明菜单条对象 JMenu menuChoice,menuImage,hero;//声明菜单对象 JMenuItem wallImage,roadImage,defaultImage,playerImage1,heroItem,heroclear;//声明菜单项对象 ; JButton renew;//声明按钮对象 String player="person、gif"; ShowRecord showRecord; //ShowRecord类 创建一个类对象 File 英雄榜=new File("英雄榜、txt"); File file[]=null; static String[] mazeName; static String currentMap; Hashtable hashtable=null; //哈希表 MazeWindow(){ wallImage File("wall、gif"); roadImage File("road、jpg"); bar=new JMenuBar();//创建菜单条对象 menuChoice=new JMenu("选择迷宫"); File("、"); (new (){ 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< mazeName、length;i++) { JMenuItem item=new JMenuItem(mazeName[i]); item、addActionListener(this); menuChoice、add(item); } maze [0]、getName()); currentMap=mazeName[0]; init(); menuImage=new JMenu("选择墙与路得图像"); wallImage=new JMenuItem("墙得图像");//创建菜单项对象 roadImage=new JMenuItem("路得图像"); playerImage1=new JMenuItem("玩家图像"); defaultImage=new JMenuItem("墙与路得默认图像"); menuImage、add(wallImage);//菜单项添加到菜单上,如图3-5 menuImage、add(roadImage); menuImage、add(playerImage1); menuImage、add(defaultImage); hero=new JMenu("英雄榜"); heroItem=new JMenuItem("查瞧英雄榜"); heroclear=new JMenuItem("清空英雄榜"); hero、add(heroItem); hero、add(heroclear); bar、add(menuChoice); bar、add(menuImage);//菜单添加到菜单条上,如图3-5 bar、add(hero); setJMenuBar(bar); wallImage、addActionListener(this);//为菜单项添加监听器 roadImage、addActionListener(this); playerImage1、addActionListener(this); defaultImage、addActionListener(this); heroItem、addActionListener(this); heroclear、addActionListener(this); renew=new JButton(new ImageIcon("龙珠、gif"));//为“重新开始”按钮添加监听器 Font font=new Font("楷体",Font、BOLD,30); renew、setFont(font); renew、setVerticalAlignment(SwingConstants、TOP); renew、addActionListener(this); add(maze,BorderLayout、CENTER);//默认布局,如图3-7 add(renew,BorderLayout、SOUTH); hashtable=new Hashtable(); 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() 测试此抽象路径名表示得文件或目录就是否存在。 try{ out=new (英雄榜); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut、writeObject(hashtable); //void writeObject(Object obj) 将指定得对象写入 ObjectOutputStream。 objectOut、close(); //void close() 关闭流。 out、close(); //java、io 类 得方法 void close() 关闭此文件输出流并释放与此流有关得所有系统资源。 } catch(IOException e){} } showRecord =new ShowRecord(this,mazeName、length); setVisible(true); setBounds(60,60,1012,890); validate(); setDefaultCloseOperation(JFrame、EXIT_ON_CLOSE);/*EXIT_ON_CLOSE:退出应用程序后得默认窗口关闭操作*/ } public void init(){ if(maze!=null){ remove(maze); remove(maze、getHandleMove()); } maze=new Maze(); maze、setWallImage(wallImageFile); maze、setRoadImage(roadImageFile); maze、setMaze); maze、person、setImage(player); add(maze,BorderLayout、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、showOpenDialog(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); (); if(){ wallImage; maze、setWallImage(wallImageFile); } } else if(e、getSource()==playerImage1) { J chooser=new J(); filter=new ( "JPG&GIF Images","jpg","gif"); chooser、set(filter); int state=chooser、showOpenDialog(null); (); if(){ player=(); maze、person、setImage(player); } } else if(e、getSource()==defaultImage){ wallImage File("wall、gif"); roadImage File("road、jpg"); maze、setWallImage(wallImageFile); maze、setRoadImage(roadImageFile); } else if(e、getSource()==renew){ init(); } else if(e、getSource()==heroItem) //点击英雄榜 { showRecord、setMazeName(mazeName); showRecord、readAndShow(hashtable); showRecord、setVisible(true); } else if(e、getSource()==heroclear){ File("英雄榜、txt"); f、delete(); JFrame t=new 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); JLabel leb=new JLabel("英雄榜已清空,请重新载入游戏。"); pan、add(leb,BorderLayout、CENTER); t、setDefaultCloseOperation(JFrame、EXIT_ON_CLOSE); t、setVisible(true); } else{ JMenuItem item=(JMenuItem)e、getSource(); maze ()+"、maze"); currentMap=item、getText(); maze、getHandleMove()、setMap(currentMap); init(); } } public static void main(String args[]){ new MazeWindow(); } } //Maze类 import java、awt、*; import java、awt、event、*; import javax、swing、*; import java、io、*; public class Maze extends JLayeredPane{ ; MazePoint[][] mazePoint; WallOrRoad[][] wallOrRoad; PersonInMaze person; HandleMove handleMove; ; int distance=45,m=0,n=0; public Maze(){ setLayout(null); wallImage=new File("wall、gif"); roadImage=new File("road、jpg"); person=new PersonInMaze(); handleMove=new HandleMove(); handleMove、initSpendTime(); person、addKeyListener(handleMove); setLayer(person,JLayeredPane、DRAG_LAYER); } public void setMaze f){ mazeFile=f; char [][]a; RandomAccess; String lineWord=null; try{ in=new RandomAccess,"r"); //创建RandomAccessFile类得只读对象 /*RandomAccessFile就是用来访问那些保存数据记录得文件得,这样您就可以用seek( )方法来访问记录,并进行读写了*/ long length=in、length(); long position=0; in、seek(position); while(position<length){ String str=in、readLine()、trim(); if(str、length()>=n) n=str、length(); position=in、get(); m++; } a=new char[m][n]; position=0; in、seek(position); m=0; while(position<length){ String str=in、readLine(); a[m]=str、toCharArray(); position=in、get(); m++; } in、close(); wallOrRoad=new WallOrRoad[m][n]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ wallOrRoad[i][j]=new WallOrRoad(); if(a[i][j]=='1'){ wallOrRoad[i][j]、setIsWall(true); wallOrRoad[i][j]、setWallImage(wallImage); wallOrRoad[i][j]、repaint(); } else if(a[i][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); 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 Hspace=distance,Vspace=distance; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ mazePoint[i][j]=new MazePoint(Hspace,Vspace); Hspace=Hspace+distance; } Hspace=distance; Vspace=Vspace+distance; } for(int i=0;i<m;i++){ for(int j=0;j<n;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(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 void setWallImage(File f){ wallImage=f; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(wallOrRoad[i][j]、getIsWall()) wallOrRoad[i][j]、setWallImage(wallImage); wallOrRoad[i][j]、repaint(); } } } public void setRoadImage(File f){ roadImage=f; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(wallOrRoad[i][j]、getIsRoad()) wallOrRoad[i][j]、setRoadImage(roadImage); wallOrRoad[i][j]、repaint(); } } } public HandleMove getHandleMove(){ return handleMove; } } //WallOrRoad类 import javax、swing、*; import java、awt、*; import javax、swing、border、*; import java、io、*; public class WallOrRoad extends JPanel{ boolean isRoad,isWall,isEnter,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(){ return 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; 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就是实现某种斜面得类,这种斜面要么凸出要么凹入且拐角圆滑,BevelBorder该类实现简单得双线斜面边框,RAISED:凸出斜面类型。*/ } public boolean getIsWall(){ return isWall; } public void setAtMazePoint(MazePoint p){ point=p; } public MazePoint getAtMazePoint(){ return point; } public void setWallImage(File f){ wallImage=f; } public void setRoadImage(File f){ roadImage=f; } public void setPassed(boolean b) { havePassed=true; } public void paintponent(Graphics g){ super、paintponent(g); int w=getBounds()、width; int h=getBounds()、height; try{ 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,0,w,h,this); } } catch(Exception exp){} } } //MazePoint类 public class MazePoint{ int x,y; boolean haveWallOrRoad; WallOrRoad wallOrRoad=null; public MazePoint(int x,int y){ this、x=x; this、y=y; } public 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、getX()==this、getX()&&p、getY()==this、getY())/*如果MazePoint得对象p得x、y值与上面返回得x、y值相同,则返回true,否则返回false*/ return true; else return false; } public void se
    展开阅读全文
    提示  咨信网温馨提示:
    1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
    2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
    3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
    4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
    5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
    6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

    开通VIP折扣优惠下载文档

    自信AI创作助手
    关于本文
    本文标题:JAVA实现走迷宫.doc
    链接地址:https://www.zixin.com.cn/doc/4374949.html
    页脚通栏广告

    Copyright ©2010-2026   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


    关注我们 :微信公众号  抖音  微博  LOFTER               

    自信网络  |  ZixinNetwork