版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/MazeWindow.javaimport java.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.filechooser.*;public class MazeWindow extends JFrame implements ActionListener Maze maze; JMenuBar bar; JMenu menuChoice,menuImage; JMenuItem wallImage,roadImage,defaultImage; File mazeF
2、ile,wallImageFile,roadImageFile; JButton renew; MazeWindow() wallImageFile=new File("wall.jpg"); roadImageFile=new File("road.jpg"); bar=new JMenuBar(); menuChoice=new JMenu("選擇迷宮"); File dir=new File("."); File file=dir.listFiles(new FilenameFilter() public b
3、oolean accept(File dir,String name) return name.endsWith("maze"); ) for(int i=0;i<file.length;i+) JMenuItem item=new JMenuItem(filei.getName(); item.addActionListener(this); menuChoice.add(item); mazeFile=new File(file0.getName(); init(); menuImage=new JMenu("選擇墻和路的圖像(JPG,GIF)"
4、;); wallImage=new JMenuItem("墻的圖像"); roadImage=new JMenuItem("路的圖像"); defaultImage=new JMenuItem("墻和路的默認(rèn)圖像"); menuImage.add(wallImage); menuImage.add(roadImage); menuImage.add(defaultImage); bar.add(menuChoice); bar.add(menuImage); setJMenuBar(bar); wallImage.addActionL
5、istener(this); roadImage.addActionListener(this); defaultImage.addActionListener(this); renew=new JButton("重新開始"); renew.addActionListener(this); add(maze,BorderLayout.CENTER); add(renew,BorderLayout.SOUTH); setVisible(true); setBounds(60,60,510,480); validate(); setDefaultCloseOperation(J
6、Frame.EXIT_ON_CLOSE); public void init() if(maze!=null) remove(maze); remove(maze.getHandleMove(); maze=new Maze(); maze.setWallImage(wallImageFile); maze.setRoadImage(roadImageFile); maze.setMazeFile(mazeFile); add(maze,BorderLayout.CENTER); add(maze.getHandleMove(),BorderLayout.NORTH); validate();
7、 public void actionPerformed(ActionEvent e) if(e.getSource()=roadImage) JFileChooser chooser=new JFileChooser(); FileNameExtensionFilter filter=new FileNameExtensionFilter( "JPG & GIF Images","jpg","gif"); chooser.setFileFilter(filter); int state=chooser.showOpenDia
8、log(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 FileNameExtensionFilter( "
9、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
10、()=defaultImage) wallImageFile=new File("wall.jpg"); roadImageFile=new File("road.jpg"); maze.setWallImage(wallImageFile); maze.setRoadImage(roadImageFile); else if(e.getSource()=renew) init(); else JMenuItem item=(JMenuItem)e.getSource(); mazeFile=new File(item.getText(); init()
11、; public static void main(String args) new MazeWindow(); /Maze.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class Maze extends JLayeredPane File mazeFile; MazePoint mazePoint; WallOrRoad wallOrRoad; PersonInMaze person; HandleMove handleMove; File wallIm
12、age,roadImage; int distance=26,m=0,n=0; public Maze() 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,JLayeredPa
13、ne.DRAG_LAYER); public void setMazeFile(File f) mazeFile=f; char a; RandomAccessFile in=null; String lineWord=null; try in=new RandomAccessFile(mazeFile,"r"); long length=in.length(); long position=0; in.seek(position); while(position<length) String str=in.readLine().trim(); if(str.leng
14、th()>=n) n=str.length(); position=in.getFilePointer(); m+; a=new charmn; position=0; in.seek(position); m=0; while(position<length) String str=in.readLine(); am=str.toCharArray(); position=in.getFilePointer(); m+; in.close(); wallOrRoad=new WallOrRoadmn; for(int i=0;i<m;i+) for(int j=0;j<
15、;n;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.repaint(); else if(aij='*') wall0rRo
16、adij.setIsEnter(true); wall0rRoadij.setIsRoad(true); wall0rRoadij.repaint(); else if(aij='#') wallOrRoadij.setIsOut(true); wallOrRoadij.setIsRoad(true); wallOrRoadij.repaint(); mazePoint=new MazePointmn; int Hspace=distance,Vspace=distance; for(int i=0;i<m;i+) for(int j=0;j<m;j+) mazeP
17、ointij=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(wallOrRoadij); wallOrRoadij.setSize(distance,distance); wallOrRoadij.setLocation(mazePointij.getX(),mazePointij.getY(); wallOrRoadij.setAtMazePoint(
18、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.repaint()
19、; handleMove.setMazePoint(mazePoint); catch(IOException exp) JButton mess=new JButton("無(wú)效的迷宮文件"); 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=
20、0;i<m;i+) for(int j=0;j<n;j+) if(wallOrRoadij.getIsWall() wallOrRoadij.setWallImage(wallImage); wallOrRoadij.repaint(); public void setRoadImage(File f) roadImage=f; for(int i=0;i<m;i+) for(int j=0;j<n;j+) if(wallOrRoadij.getIsRoad() wallOrRoadij.setRoadImage(roadImage); wallOrRoadij.rep
21、aint(); public HandleMove getHandleMove() return handleMove; /WallOrRoad.javaimport javax.swing.*;import java.awt.*;import javax.swing.border.*;import java.io.*;public class WallOrRoad extends JPanel boolean isRoad,isWall,isEnter,isOut; MazePoint point; File wallImage,roadImage; Toolkit tool; WallOr
22、Road() 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;
23、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); public boolean getIsWall() return isWall; public void setAtMazeP
24、oint(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 paintComponent(Graphics g) super.paintComponent(g); int w=getBounds().width; int h=getBounds().height; try if(isRoad=true
25、) 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.javapublic class MazePoint int x,y; boolean haveWallOrRoad;WallOrRoad wal
26、lOrRoad=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()&
27、;&p.getY()=this.getY() return true; else return false; public void setWallOrRoad(WallOrRoad obj) wallOrRoad=obj; public WallOrRoad getWallOrRoad() return wallOrRoad; /PersonInMaze.javaimport javax.swing.*;import java.awt.*;public class PersonInMaze extends JTextField MazePoint point; Toolkit too
28、l; PersonInMaze() tool=getToolkit(); setEditable(false); setBorder(null); setOpaque(false); setToolTipText("單擊我,然后按鍵盤方向鍵"); public void setAtMazePoint(MazePoint p) point=p; public MazePoint getAtMazePoint() return point; public void paintComponent(Graphics g) super.paintComponent(g); int w
29、=getBounds().width; int h=getBounds().height; Image image=tool.getImage("person.gif"); g.drawImage(image,0,0,w,h,this); /HandleMove.javaimport java.awt.event.*;import java.awt.*;import javax.swing.*;public class HandleMove extends JPanel implements KeyListener,ActionListener MazePoint p; i
30、nt spendTime=0; javax.swing.Timer recordTime; JTextField showTime; Toolkit tool; HandleMove() recordTime=new javax.swing.Timer(1000,this); showTime=new JTextField(16); tool=getToolkit(); showTime.setEditable(false); showTime.setHorizontalAlignment(JTextField.CENTER); showTime.setFont(new Font("
31、楷體_GB2312",Font.BOLD,16); JLabel hitMess=new JLabel("單擊走迷宮者,按鍵盤方向鍵",JLabel.CENTER); hitMess.setFont(new Font("楷體_GB2312",Font.BOLD,18); add(hitMess); add(showTime); setBackground(Color.cyan); public void setMazePoint(MazePoint point) p=point; public void initSpendTime() reco
32、rdTime.stop(); spendTime=0; showTime.setText(null); public void keyPressed(KeyEvent e) recordTime.start(); PersonInMaze person=null; person=(PersonInMaze)e.getSource(); int m=-1,n=-1; MazePoint startPoint=person.getAtMazePoint(); for(int i=0;i<p.length;i+) for(int j=0;j<pi.length;j+) if(startPoint.equals(pij) m=i; n=j; break; if(e.getKeyCode()=KeyEvent.VK_UP) int k=Math.max(m-1,0); if(pkn.getWakkOrRoad().getIsRoad() tool.beep(); person.s
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年全球及中國(guó)瓦楞紙板輸送帶行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2025-2030全球RF IC 設(shè)計(jì)服務(wù)行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025年全球及中國(guó)拖拽式滴鹽撒播機(jī)行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2025年全球及中國(guó)運(yùn)水式模溫機(jī)行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 中國(guó)居民膳食指南準(zhǔn)則一食物多樣合理搭配講解
- 作用于中樞神經(jīng)系統(tǒng)的藥物講解
- 2025軟件產(chǎn)品代理版合同書
- 安防設(shè)備采購(gòu)政府采購(gòu)合同
- 2025房屋抵押貸款的合同范本
- 2025承運(yùn)合同書范本范文
- 民辦幼兒園務(wù)工作計(jì)劃
- 2025年華僑港澳臺(tái)生聯(lián)招考試高考地理試卷試題(含答案詳解)
- 中國(guó)革命戰(zhàn)爭(zhēng)的戰(zhàn)略問題(全文)
- 《數(shù)學(xué)歸納法在中學(xué)解題中的應(yīng)用研究》9000字(論文)
- 《大學(xué)英語(yǔ)四級(jí)詞匯大全》
- 第六章-1八綱辨證
- 《工業(yè)機(jī)器人系統(tǒng)維護(hù)(ABB模塊)》試卷10套
- 危險(xiǎn)性化合物的微生物降解-中國(guó)石油大學(xué)環(huán)境生物工程
- 浙江省名校新2025屆高一數(shù)學(xué)第一學(xué)期期末達(dá)標(biāo)檢測(cè)試題含解析
- 學(xué)習(xí)2024《關(guān)于加強(qiáng)社會(huì)組織規(guī)范化建設(shè)推動(dòng)社會(huì)組織高質(zhì)量發(fā)展的意見》解讀課件
- 2024年縣全民健身活動(dòng)狀況調(diào)查活動(dòng)方案
評(píng)論
0/150
提交評(píng)論