




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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("墙和路的默认图像"); 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("无效的迷宫文件"); 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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 关于机关食堂管理合同标准文本
- 兽药供货合同标准文本
- 剧组宣传推广合同范例
- 出售库存酒合同范例
- 个人防水施工合同标准文本
- 公园物业合同范本
- 企业涨薪合同范例
- 代理进货合同范例
- 光伏发电建设工程合同范例
- 共同经营厂合同标准文本
- 2025届浙江省宁波市“十校”高三下学期3月联考语文试题含答案
- 山东2025年山东科技职业学院招聘33人笔试历年参考题库附带答案详解
- 2024山西云时代技术有限公司社会招聘59人笔试参考题库附带答案详解
- (一模)2025年聊城市高三高考模拟历史试题卷(含答案)
- 交友主题班会-远离“背后蛐蛐”课件
- 25春形势与政策-专题测验1-5-国开-参考资料
- 2025年大学生信息素养大赛培训考试题库500题(附答案)
- 浙江省温州市2024届高三下学期三模数学试题 含解析
- 富锂锰基正极材料氧空位可控构筑及性能研究
- cissp中文版考试真题题库500题(导出附答案)
- 2024成人肥胖食养指南
评论
0/150
提交评论