1、/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,
2、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() wall
3、Image 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 ) mazeNamei=filei、getName()、substring(0, filei、getName()、length()-5);/substring(截取得起
4、始位置,截取得长度); for(int i=0;i mazeName、length;i+) JMenuItem item=new JMenuItem(mazeNamei); item、addActionListener(this); menuChoice、add(item); maze 0、getName(); currentMap=mazeName0; init(); menuImage=new JMenu(选择墙与路得图像); wallImage=new JMenuItem(墙得图像);/创建菜单项对象 roadImage=new JMenuItem(路得图像); playerImage1
5、=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(heroc
6、lear); bar、add(menuChoice); bar、add(menuImage);/菜单添加到菜单条上,如图3-5bar、add(hero); setJMenuBar(bar); wallImage、addActionListener(this);/为菜单项添加监听器 roadImage、addActionListener(this); playerImage1、addActionListener(this); defaultImage、addActionListener(this); heroItem、addActionListener(this); heroclear、addA
7、ctionListener(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
8、 Hashtable(); for(int i=0;i) hashtable、put(mazeNamei+1,mazeNamei+#+100+#匿名1); hashtable、put(mazeNamei+2,mazeNamei+#+100+#匿名2); hashtable、put(mazeNamei+3,mazeNamei+#+100+#匿名3); if(!英雄榜、exists() /英雄榜就是File类对象; boolean exists() 测试此抽象路径名表示得文件或目录就是否存在。 try out=new (英雄榜); ObjectOutputStream objectOut=new
9、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); setVisi
10、ble(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、s
11、etMaze); 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
12、、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、getSourc
13、e()=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(wallImag
14、eFile); 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(提示!);
15、 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、setDefaultCloseOper
16、ation(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 jav
17、ax、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()
18、; 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就是用来访问那些保存数据记
19、录得文件得,这样您就可以用seek( )方法来访问记录,并进行读写了*/ long length=in、length(); long position=0; in、seek(position); while(position=n) n=str、length(); position=in、get(); m+; a=new charmn; position=0; in、seek(position); m=0; while(positionlength) String str=in、readLine(); am=str、toCharArray(); position=in、get(); m+; in
20、、close(); wallOrRoad=new WallOrRoadmn; for(int i=0;im;i+) for(int j=0;jn;j+) wallOrRoadij=new WallOrRoad(); if(aij=1) wallOrRoadij、setIsWall(true); wallOrRoadij、setWallImage(wallImage); wallOrRoadij、repaint(); else if(aij=0) wallOrRoadij、setIsRoad(true); wallOrRoadij、setRoadImage(roadImage); wallOrR
21、oadij、repaint(); else if(aij=*) wallOrRoadij、setIsEnter(true); wallOrRoadij、setIsRoad(true); wallOrRoadij、repaint(); else if(aij=#) wallOrRoadij、setIsOut(true); wallOrRoadij、setIsRoad(true); wallOrRoadij、repaint(); mazePoint=new MazePointmn; int Hspace=distance,Vspace=distance; for(int i=0;im;i+) fo
22、r(int j=0;jn;j+) mazePointij=new MazePoint(Hspace,Vspace); Hspace=Hspace+distance; Hspace=distance; Vspace=Vspace+distance; for(int i=0;im;i+) for(int j=0;jn;j+) add(wallOrRoadij); wallOrRoadij、setSize(distance,distance); wallOrRoadij、setLocation(mazePointij、getX(),mazePointij、getY(); wallOrRoadij、s
23、etAtMazePoint(mazePointij); mazePointij、setWallOrRoad(wallOrRoadij); mazePointij、setIsWallOrRoad(true); if(wallOrRoadij、getIsEnter() person、setAtMazePoint(mazePointij); add(person); person、setSize(distance,distance); person、setLocation(mazePointij、getX(),mazePointij、getY(); person、requestFocus(); pe
24、rson、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;im;i+) for(int j=0;j
25、n;j+) if(wallOrRoadij、getIsWall() wallOrRoadij、setWallImage(wallImage); wallOrRoadij、repaint(); public void setRoadImage(File f) roadImage=f; for(int i=0;im;i+) for(int j=0;jn;j+) if(wallOrRoadij、getIsRoad() wallOrRoadij、setRoadImage(roadImage); wallOrRoadij、repaint(); public HandleMove getHandleMov
26、e() 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(boolea
27、n 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)
28、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() re
29、turn 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、paintp
30、onent(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) /Maze
31、Point类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
©2010-2024 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100