版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、/MazeWindow 主類import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;import javax.swing.filechooser.*;public class MazeWindow extends JFrame implements ActionListener Maze maze; JMenuBar bar;/聲明菜單條對象 JMenu menuChoice,menuImage,hero;/聲明菜單對象 JMenuIte
2、m wallImage,roadImage,defaultImage,playerImage1,heroItem,heroclear;/聲明菜單項(xiàng)對象 File mazeFile,wallImageFile,roadImageFile; JButton renew;/聲明按鈕對象 String player=person.gif; ShowRecord showRecord; /ShowRecord類 創(chuàng)建一個(gè)類對象 File 英雄榜=new File(英雄榜.txt); File file=null; static String mazeName; static String current
3、Map; Hashtable hashtable=null; /哈希表 MazeWindow() wallImageFile=new File(wall.gif); roadImageFile=new File(road.jpg); bar=new JMenuBar();/創(chuàng)建菜單條對象 menuChoice=new JMenu(選擇迷宮); File dir=new File(.); file=dir.listFiles(new FilenameFilter() public boolean accept(File dir,String name) return name.endsWith(
4、maze); ); mazeName=new Stringfile.length; for(int i=0;i file.length;i+) mazeNamei=filei.getName().substring(0, filei.getName().length()-5);/substring(截取的起始位置,截取的長度);1 / 25 for(int i=0;i mazeName.length;i+) JMenuItem item=new JMenuItem(mazeNamei); item.addActionListener(this); menuChoice.add(item); m
5、azeFile=new File(file0.getName(); currentMap=mazeName0; init(); menuImage=new JMenu(選擇墻和路的圖像); wallImage=new JMenuItem(墻的圖像);/創(chuàng)建菜單項(xiàng)對象 roadImage=new JMenuItem(路的圖像); playerImage1=new JMenuItem(玩家圖像); defaultImage=new JMenuItem(墻和路的默認(rèn)圖像); menuImage.add(wallImage);/菜單項(xiàng)添加到菜單上,如圖3-5 menuImage.add(roadIma
6、ge);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-5bar.add(hero); setJMenuBar(bar); wallImage.addActionListener
7、(this);/為菜單項(xiàng)添加監(jiān)聽器 roadImage.addActionListener(this); playerImage1.addActionListener(this); defaultImage.addActionListener(this); heroItem.addActionListener(this); heroclear.addActionListener(this); renew=new JButton(new ImageIcon(龍珠.gif);/為“重新開始”按鈕添加監(jiān)聽器 Font font=new Font(楷體,Font.BOLD,30); renew.set
8、Font(font); renew.setVerticalAlignment(SwingConstants.TOP); renew.addActionListener(this); add(maze,BorderLayout.CENTER);/默認(rèn)布局,如圖3-7 add(renew,BorderLayout.SOUTH); hashtable=new Hashtable(); for(int i=0;ifile.length;i+) hashtable.put(mazeNamei+1,mazeNamei+#+100+#匿名1); hashtable.put(mazeNamei+2,mazeN
9、amei+#+100+#匿名2); hashtable.put(mazeNamei+3,mazeNamei+#+100+#匿名3); if(!英雄榜.exists() /英雄榜是File類對象; boolean exists() 測試此抽象路徑名表示的文件或目錄是否存在。 try FileOutputStream out=new FileOutputStream(英雄榜); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(hashtable); /void writeObject(O
10、bject obj) 將指定的對象寫入 ObjectOutputStream。 objectOut.close(); /void close() 關(guān)閉流。 out.close(); /java.io 類 FileOutputStrea的方法 void close() 關(guān)閉此文件輸出流并釋放與此流有關(guān)的所有系統(tǒng)資源。 catch(IOException e) showRecord =new ShowRecord(this,mazeName.length); setVisible(true); setBounds(60,60,1012,890); validate(); setDefaultClo
11、seOperation(JFrame.EXIT_ON_CLOSE);/*EXIT_ON_CLOSE:退出應(yīng)用程序后的默認(rèn)窗口關(guān)閉操作*/ public void init() if(maze!=null) remove(maze); remove(maze.getHandleMove(); maze=new Maze(); maze.setWallImage(wallImageFile); maze.setRoadImage(roadImageFile); maze.setMazeFile(mazeFile); maze.person.setImage(player); add(maze,Bo
12、rderLayout.CENTER); add(maze.getHandleMove(),BorderLayout.NORTH); validate(); public void actionPerformed(ActionEvent e) if(e.getSource()=roadImage) JFileChooser chooser=new JFileChooser();/文件對話框 FileNameExtensionFilter filter=new FileNameExtensionFilter( JPG&GIF Images,jpg,gif); chooser.setFileFilt
13、er(filter); int state=chooser.showOpenDialog(null); File file=chooser.getSelectedFile(); if(file!=null&state=JFileChooser.APPROVE_OPTION) roadImageFile=file; maze.setRoadImage(roadImageFile); else if(e.getSource()=wallImage) JFileChooser chooser=new JFileChooser(); FileNameExtensionFilter filter=new
14、 FileNameExtensionFilter( JPG&GIF Images,jpg,gif); chooser.setFileFilter(filter); int state=chooser.showOpenDialog(null); File file=chooser.getSelectedFile(); if(file!=null&state=JFileChooser.APPROVE_OPTION) wallImageFile=file; maze.setWallImage(wallImageFile); else if(e.getSource()=playerImage1) JF
15、ileChooser chooser=new JFileChooser(); FileNameExtensionFilter filter=new FileNameExtensionFilter( JPG&GIF Images,jpg,gif); chooser.setFileFilter(filter); int state=chooser.showOpenDialog(null); File file=chooser.getSelectedFile(); if(file!=null&state=JFileChooser.APPROVE_OPTION) player=file.getName
16、(); maze.person.setImage(player); else if(e.getSource()=defaultImage) wallImageFile=new File(wall.gif); roadImageFile=new File(road.jpg); maze.setWallImage(wallImageFile); maze.setRoadImage(roadImageFile); else if(e.getSource()=renew) init(); else if(e.getSource()=heroItem) /點(diǎn)擊英雄榜 showRecord.setMaze
17、Name(mazeName); showRecord.readAndShow(hashtable); showRecord.setVisible(true); else if(e.getSource()=heroclear) File f=new 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.setConten
18、tPane(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(); mazeFile=new File(item.getText
19、()+.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 File mazeFile; MazePoint mazePoin
20、t; WallOrRoad wallOrRoad; PersonInMaze person; HandleMove handleMove; File wallImage,roadImage; 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(); pers
21、on.addKeyListener(handleMove); setLayer(person,JLayeredPane.DRAG_LAYER); public void setMazeFile(File f) mazeFile=f; char a; RandomAccessFile in=null; String lineWord=null; try in=new RandomAccessFile(mazeFile,r); /創(chuàng)建RandomAccessFile類的只讀對象 /*RandomAccessFile是用來訪問那些保存數(shù)據(jù)記錄的文件的,這樣你就可以用seek( )方法來訪問記錄,并進(jìn)
22、行讀寫了*/ long length=in.length(); long position=0; in.seek(position); while(position=n) n=str.length(); position=in.getFilePointer(); m+; a=new charmn; position=0; in.seek(position); m=0; while(positionlength) String str=in.readLine(); am=str.toCharArray(); position=in.getFilePointer(); m+; in.close()
23、; 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); wallOrRoadij.re
24、paint(); 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+) for(int j=
25、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.setAtMaze
26、Point(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(); person.rep
27、aint(); 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;jn;j+) if
28、(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 getHandleMove() retu
29、rn 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; File wallImage,roadImage,personImage; Toolkit tool; WallOrRoad() tool=getToolkit(); p
30、ublic 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)
31、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是實(shí)現(xiàn)某種斜面的類,這種斜面要么凸出要么凹入且拐角圓滑,BevelBorder該類實(shí)現(xiàn)簡單的雙線斜面邊框,RAISED:凸出斜面類型。*/ pu
32、blic 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 paintComp
33、onent(Graphics g) super.paintComponent(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,
34、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 in
35、t 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 setWallOrRoad(WallOrRoad obj) wallOrRoad=obj; public WallOrRoad getWallOrRo
36、ad() return wallOrRoad; /PersonInMaze類import javax.swing.*;import java.awt.*;import javax.swing.filechooser.*;import java.io.*;public class PersonInMaze extends JTextField MazePoint point; Toolkit tool; String person=person1.gif; PersonInMaze() tool=getToolkit(); /獲得工具包 setEditable(false);/不可見 setBo
37、rder(null);/無邊框 setOpaque(false);/非透明的 setToolTipText(單擊我,然后按鍵盤方向鍵);/提示信息,如圖3-1所示。 public void setAtMazePoint(MazePoint p) point=p; public MazePoint getAtMazePoint() return point; public void paintComponent(Graphics g)/設(shè)置走迷宮者的圖片 super.paintComponent(g); int w=getBounds().width; int h=getBounds().hei
38、ght; Image image=tool.getImage(person);/獲得image圖像 g.drawImage(image,0,0,w,h,this);/繪制image圖像 public void setImage(String person) this.person=person;/HandleMove類import java.awt.event.*;import java.awt.*;import javax.swing.*;public class HandleMove extends JPanel implements KeyListener,ActionListener MazePoint p; int spendTime=0; javax.swing.Timer recordTime; JTextField showTime; Toolkit tool; String maze=MazeWindow.cur
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度智能門禁系統(tǒng)安裝與保安勞務(wù)合作合同3篇
- 2024幼兒園合伙人品牌共建與發(fā)展合作協(xié)議范本3篇
- 中小學(xué)生心理健康教育現(xiàn)狀分析及對策建議
- 2024年銷售人員入職權(quán)益保障及業(yè)績考核合同3篇
- 2024年航空公司機(jī)票預(yù)訂系統(tǒng)租賃合同
- 2025年度冷鏈物流鮮豬肉配送服務(wù)合同5篇
- 大三期末離散數(shù)學(xué)試卷
- 四川省西昌市禮州中學(xué)2025屆中考生物模擬試題含解析
- 江蘇省南京市江寧區(qū)湖熟片達(dá)標(biāo)名校2025屆中考生物全真模擬試卷含解析
- 小學(xué)生數(shù)學(xué)學(xué)習(xí)興趣與德育培養(yǎng)的研究
- 血液透析SOP2021完整版課件
- (高速公路)工程施工便道施工方案-
- 電磁閥培訓(xùn)(精選)課件
- 家禽常用原料代謝能估測表
- 古代詩歌鑒賞思想內(nèi)容ppt
- 初一上學(xué)期期末測試卷英語
- 上海沃陸變頻器VL600型變頻器說明書概要
- 2023年高考物理一輪復(fù)習(xí):拋體運(yùn)動與圓周運(yùn)動(附答案解析)
- VRV空調(diào)技術(shù)要求和質(zhì)量標(biāo)準(zhǔn)
- Q∕GDW 10721-2020 電力通信現(xiàn)場標(biāo)準(zhǔn)化作業(yè)規(guī)范
- 公安警察工作匯報(bào)PPT模板課件
評論
0/150
提交評論