⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze.java

📁 采用stack算法的DFS搜索
💻 JAVA
字号:
	//G5BADS Maze Assignment
	//Huang Li Wen
	//Nov 12th, 2004
	
	import java.awt.*;
	import javax.swing.*;
	import java.awt.event.*;
	import java.util.*;
	import java.util.Hashtable;
	
	public class Maze
	{
		public static void main(String[] args) 
		{
	         GameFrame f = new GameFrame();   
	         f.setSize(600,500);
	         f.setVisible(true);
	    }
	    
	}
	
	
	class GameFrame extends JFrame implements ActionListener
	{
	    
	    DFS dfs = null;                     
	    //initialize DFS since later will call the methods inside
	     
	     
	    jpMainPanel mp = new jpMainPanel();
	    JButton jbStart = new JButton("Start");    //is used to start the maze
		
	    public GameFrame()
	    {
	     	super("Maze");
	 
	 
	        // Set the layout of the frame
	        JPanel jpCtrl = new JPanel();
	        jpCtrl.setLayout(new FlowLayout());
	        jpCtrl.add(jbStart);
	        getContentPane().setLayout(new BorderLayout());
	        getContentPane().add(mp, BorderLayout.CENTER);
	        getContentPane().add(jpCtrl,BorderLayout.SOUTH);        
	      
	        
	        //Action Listener for exit button on the frame
	        this.addWindowListener(
				new WindowAdapter()
				{
					public void windowClosing(WindowEvent evt) 
					{
						mp=null;
	                	System.exit(0);
	            	}
				}
			);
			
			//Action Listener for "Start" button
			jbStart.addActionListener(this);
	 
	    }
	     
	     
	     //Events for "Start" button
	    public void actionPerformed(ActionEvent e)
		{	
		    Graphics g1;
	    	String s = "";	  
	        //temporarily store a string that is 
	        //converted from an object from the vector
	  
		    Coordinate co = new Coordinate();
		    //the coordinate of the letter drawn during the program
		    
		    jbStart.setEnabled(false);
		    // set it disabled when being pressed
		    
	        dfs = new DFS(); 
	        dfs.runDFS(); // run dfs
	        
	        
	        Vector v1 = Graph.get_Vector();
	        //get the paths has been taken during the dfs, 
	        //in which the paths store
	        
	        
	        for (int n =0; n<v1.size();n++)
	        {         
	   	        s=v1.elementAt(n).toString();
	            //temporarily store a string that is 
	            //converted from an object from the vector
	   	        
	   	        
	   	        co =(Coordinate)(DFS.get_Hashtable().get(s));
	   	        //find the corresponding coordinate in the hash table defined before 
	   	        //based on the key of the hashtable
	   	        
	   	        
	   	        g1=getGraphics();
	   	        Font f=new Font("Arial",Font.BOLD,20);
			     	
	         	g1.setFont(f);
	            g1.setColor(Color.white); 
	            //to see clearly the process set the letter to be white
	            g1.drawString(s,co.getX(),co.getY());
	            
	            
	            //let the letter be drawn delay 1 second
	          	try
	            {
	           	    Thread.sleep(1000);
	            }  
			    catch (Exception e1){}
	        }
	        
	        
	        JOptionPane.showMessageDialog(null,"The goal is reached.");
	        //the goal has been reached
	        
	        repaint();
	     } 
	}
	
	
	
	class jpMainPanel extends JPanel
	{	
	    public void paint(Graphics g)  
	    {
	    	//Draw the GUI
	     	 super.paint(g);
	     	 Font f=new Font("Arial",Font.BOLD,20);
			     	
	     	 g.setFont(f);
	         g.drawString("A",50,100);
	         g.drawString("B",150,100); 
	         g.drawString("C",375,100);
	         g.drawString("D",475,100);
	         g.drawString("E",150,150);
	         g.drawString("F",150,250);
	         g.drawString("G",300,250);
	         g.drawString("H",150,350);
	         g.drawString("I",150,400);
	         g.drawString("J",300,350); 
	         g.drawString("K",300,400);
	         g.drawString("L",425,350);
	         g.drawString("M",425,225);
	         g.drawString("N",425,400);
	         g.drawString("O",500,350);
	          
	         g.drawString("",150,50);
	         g.drawString("",375,50);
	         g.drawString("",300,150);
	         g.drawString("",375,250);
	        
	         g.drawLine(50,100,150,100);
	         g.drawLine(150,100,150,50);
	         g.drawLine(150,50,375,50); 
	         g.drawLine(375,50,375,100);  
	         g.drawLine(375,100,475,100);
	         g.drawLine(150,100,150,150);
	         g.drawLine(150,150,300,150);
	         g.drawLine(300,150,300,250);    
	         g.drawLine(300,250,375,250);
	         g.drawLine(375,100,375,250);
	         g.drawLine(150,250,300,250);
	         g.drawLine(150,150,150,250);
	         g.drawLine(150,250,150,350); 
	         g.drawLine(150,350,150,400);
	         g.drawLine(150,350,300,350);
	         g.drawLine(300,350,300,400);
	         g.drawLine(300,350,425,350);
	         g.drawLine(425,350,425,225);
	         g.drawLine(425,350,425,400);
	         g.drawLine(425,350,500,350);
	
	    }
	     
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -