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

📄 maze.java

📁 A Maze created in Java. Great algorithms
💻 JAVA
字号:
import java.awt.BorderLayout;
//import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.util.Scanner;
import java.util.Stack;
import java.util.Random;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import com.sun.media.jai.widget.DisplayJAI;
public class Maze {

	
	private JPanel setup = new JPanel();
	private JLabel rowLabel = new JLabel("Row");
	private JLabel columnLabel = new JLabel("Column");
	private JTextField row = new JTextField(4);
	private JTextField column = new JTextField(4);
	private JButton mazePlay = new JButton("play");
	private PlanarImage im_blank = JAI.create("fileload", "blank.gif");
	private PlanarImage im_black = JAI.create("fileload", "black.gif");
	private JFrame frame = new JFrame();
	private JPanel panel = new JPanel(); 
	//private Container contentPane;
	//private Graphics g;
	
	//private JFrame ff = new JFrame();
	
	private class Link
	{
		private int fa, fb, ta, tb;
		private Link(int x, int y, int p, int q)
		{
			fa = x;
			fb = y;
			ta = p;
			tb = q;
		}
		public int getfa()
		{
			return fa;
		}
		public int getfb()
		{
			return fb;
		}
		public int getta()
		{
			return ta;
		}
		public int gettb()
		{
			return tb;
		}
	}

	/**
	 * @param args
	 */
	
	
	private int m , n ;	
	private int[][] maze;
	private int[][] maze2;
	private int[] four = new int[4];
	private Stack<Link> stack = new Stack<Link>();
	
	
	
	private class mazePlayListener implements ActionListener
	{
		public void actionPerformed( ActionEvent e ) 
		{
			
			this.run();
			//maze.printMaze();
		}
		private void run()
		{
			//Scanner scan = new Scanner(System.in);
			//m = scan.nextInt();
			//n = scan.nextInt();
			m = new Integer(row.getText()).intValue();
			n = new Integer(column.getText()).intValue();
			maze = new int[m][n];
			maze2 = new int[2*m][2*n];
			
			
			for(int i = 0; i < m; i++)
			{
				for (int j = 0; j < n; j++)
				{
					maze[i][j] = 0;
					maze2[2 * i][2 * j] = 1;
				}
			}
			maze[m / 2 ][n / 2 ] = 1;
			maze2[2 * m - 1][2 * n - 2] = 1;
			stack.push( new Link( m / 2 , n / 2 , m / 2  + 1, n / 2 ));
			//stack.push( new Link( 0, 0, 0, 1));

			Random gen = new Random();
			while(!stack.isEmpty())
			{
				for(int i = 0; i < 4; i++)
					four[i] = 0;
				Link pp = stack.pop();
				
				int fa = pp.getfa();
				int fb = pp.getfb();
				int ta = pp.getta();
				int tb = pp.gettb();
				int temp;
				if(maze[ta][tb] == 0)
				{
					maze[ta][tb] = 1;
					maze2[fa + ta][fb + tb] = 1;
					int count = 4;
					while( count > 0 )
					{
						temp = gen.nextInt(4) ;
						while( four[temp] == 1) temp = (temp + 1) % 4;
						if ( temp == 0)
						{
							if(check(ta, tb+1))
							{
								stack.push(new Link(ta, tb, ta, tb + 1));
							}
							four[temp] = 1;
							count--;
						}
						if ( temp == 1)
						{
							if(check(ta+1, tb))
							{
								stack.push(new Link(ta, tb, ta + 1, tb));
							}
							four[temp] = 1;
							count--;
						}
						if ( temp == 2)
						{
							if(check(ta, tb-1))
							{
								stack.push(new Link(ta, tb, ta, tb - 1));
							}
							four[temp] = 1;
							count--;
						}
						if ( temp == 3)
						{
							if(check(ta-1, tb))
							{
								stack.push(new Link(ta, tb, ta - 1, tb));
							}
							four[temp] = 1;
							count--;
						}
					}	
				}
			}
			
			//panel.setLayout(new GridLayout(2*m + 1, 2*n + 1));
			panel.removeAll();
			panel.setLayout(new GridLayout(2*m + 1, 2*n + 1));
			DisplayJAI white = new DisplayJAI(im_blank);
			DisplayJAI black = new DisplayJAI(im_black);
			panel.add(black);
			panel.add(white);
			for(int k = 0; k < 2 * n - 1 ; k++) panel.add(new DisplayJAI(im_black));
			for(int i = 0; i < 2 * m; i++)
			{
				panel.add(new DisplayJAI(im_black));
				for(int j = 0; j < 2 * n; j++)
				{
					
					if(maze2[i][j] == 0)
					{
						panel.add(new DisplayJAI(im_black));
					}
					else
					{
						panel.add(new DisplayJAI(im_blank));
					}
					
				}	
			}
			//System.out.println("m =" + m);
			
			//panel.repaint(2, 0, 0, panel.getWidth(), panel.getHeight());
			//frame.repaint();
			frame.setVisible(true);
			//ff.add(panel);
			//ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			//ff.setSize(800,800); // adjust the frame size.
			//ff.setVisible(true); // show the frame.
		}
	}
	

	
	private void init()
	{
		mazePlay.addActionListener(new mazePlayListener());
		setup.add(rowLabel);
		setup.add(row);
		setup.add(columnLabel);
		setup.add(column);
		setup.add(mazePlay);

		
		frame.setTitle("DisplayJAI: Maze");
		//contentPane = frame.getContentPane();
		frame.setLayout(new BorderLayout());
		
		//panel.setLayout(new GridLayout(2*m + 1, 2*n + 1));
		
		/*DisplayJAI white = new DisplayJAI(im_blank);
		DisplayJAI black = new DisplayJAI(im_black);
		panel.add(black);
		panel.add(white);
		*/
		frame.add(setup, BorderLayout.NORTH);
		frame.add(panel, BorderLayout.CENTER);
		//frame.add(panel, BorderLayout.CENTER);
		
		
		// Set the closing operation so the application is finished.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800,900); // adjust the frame size.
		frame.setVisible(true); // show the frame.
	}
	
	
		

		
		
		
		
		
		
	
	
	private void printMaze()
	{
		System.out.print("0   ");
		for(int k = 2; k < 2 * n + 1; k++) System.out.print("0 ");
		System.out.println("");
		for(int i = 0; i < 2 * m; i++)
		{
			System.out.print("0 ");
			for(int j = 0; j < 2 * n; j++)
			{
				if(maze2[i][j]==1) System.out.print("  ");
				else System.out.print("0 ");
			}
			System.out.println("");
		}
	}
	
	public static void main(String[] args)
	{
		Maze test = new Maze();
		
		test.init();
		//test.printMaze();
	}
	
	private boolean check (int i, int j)
	{
		if(i >= 0 && i < m && j >= 0 && j < n && maze[i][j] ==0)
			return true;
		else return false;
	}

}

⌨️ 快捷键说明

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