迷宫可视化
直接上结果图片如下:
代码分析:
迷宫思路实现:
①MazeData类:
变量:
1、需要入口和出口坐标
2、记录访问过的路径
3、记录走过的路径
4、绘制迷宫地图二维数组
5、迷宫标记墙和路径
初始化:
1、传入文件路径,转化为输入流
2、实例化变量
3、遍历,存入maze二维数组。
方法实现:
1、getMaze(i,j)在二维数组里面获取任意一个值
2、InArear(x,y)判断有没有在二维数组内
3、print()打印出来
②AlgoFrame类:
变量:
1、画布的宽和高
2、存储已经画好的迷宫地图二维数组
初始化:
1、添加自定义的画布框架、画布宽高、框架的的自动适应屏幕大小
2、自定义的画布框架AlgoCanvas的paintComponent方法是主要的
方法实现
1、render(data) 传入二维数组迷宫地图,repaint()方法进行绘制
2、paintComponent方法有抗锯齿作业、把已经画好的二维迷宫地图,
通过AlgoVisHelper工具类A绘制为可视化的地图;
③AlgoVisHelper类辅助工具类
变量:
1、颜色
方法实现:
1、绘制颜色、绘制空心矩形、绘制实心矩形等等
代码实现:
1、dao
package Maze;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
/*
* dao
*/
public class MazeData {
// 定义存放迷宫文件的数组
public static void main(String[] args) {
String filename="F:\\Java\\Eclipse\\GUI\\bin\\maze_101_101.txt";
MazeData mazeData = new MazeData(filename);
mazeData.print();
}
// 定义入口坐标为 entranceX,entranceY,出口坐标为 exitX,exitY
private static int entranceX;
private int entranceY;
private int exitX;
private static int exitY;
// 定义 visited 二维数组用于存放该点是否已经访问过,true 为访问过,false 为未访问过。
public boolean[][] visited;
// 定义 path 二维数组用于存放走过的路径,并用于显性的展现路径的颜色。
public static boolean[][] path;
private static char[][] maze;
// 定义迷宫文件n行m列
private static int N;
private static int M;
// 定义sacnner 类获取文件输入
private Scanner scanner;
// 声明"#"对应的是 wall(墙)," "为 road(路)
public static final char Wall='#';
public static final char Path=' ';
public MazeData(String filename) {
//文件名不能为空
try {
if(filename==null){
throw new IllegalArgumentException("filename can not be empty");
}
File file =new File(filename);
//判断文件是否存在
if(!file.exists()){
throw new IllegalArgumentException("file is not exists");
}
//将文件对象file存放如输入流
FileInputStream fis =new FileInputStream(file);
scanner =new Scanner(new BufferedInputStream(fis), "utf-8");
//读取第一行数据
String nmline =scanner.nextLine();
String[] nm=nmline.trim().split("\\s");
//获取迷宫的行数N
N =Integer.parseInt(nm[0]);
//获取迷宫的列数M
M=Integer.parseInt(nm[1]);
//获取了行数和列数后,即可实现maze数组
maze= new char[N][M];
visited = new boolean[N][M]; //实例化
path=new boolean[N][M];
entranceX=1;
entranceY=0;
exitX=N-2;
exitY=M-1;
//读取后续的N行
for(int i=0;i<N;i++){
String line =scanner.nextLine();
//检查每一行是否有M个字符
if(line.length()!=M){
throw new IllegalArgumentException("file is false");
}
for(int j=0;j<M;j++){
maze[i][j]=line.charAt(j);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 关闭文件
finally {
if(scanner!=null){
scanner.close();
}
}
}
public static int getEntranceX() {
return entranceX;
}
public void setEntranceX(int entranceX) {
this.entranceX = entranceX;
}
public int getEntranceY() {
return entranceY;
}
public void setEntranceY(int entranceY) {
this.entranceY = entranceY;
}
public int getExitX() {
return exitX;
}
public void setExitX(int exitX) {
this.exitX = exitX;
}
public static int getExitY() {
return exitY;
}
public void setExitY(int exitY) {
this.exitY = exitY;
}
//接口方法
public static int N(){
return N;
}
public static int M(){
return M;
}
//获取maze某一位置的值
public static char getMaze(int i,int j){
//索引是否合法
if(!inArea(i,j)){
throw new IllegalArgumentException("i or j is illegal index");
}
return maze[i][j];
}
//判断当期的索引是否在迷宫的区域内
public static boolean inArea(int x, int y) {
// TODO Auto-generated method stub
return x>=0 &&x<N&&y>=0&&y<M;
}
//在 MazeData 类中创建打印输出 txt 文件的方法,检查是否能完整读取迷宫文件数据。
public void print(){
System.out.println(N+" "+ M);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
System.out.print(maze[i][j]);
}
System.out.println();
}
System.out.println("绘制完成:"+getMaze(20, 20));
}
}
2、JFrme
package Maze;
import java.awt.*;
import javax.swing.*;
/*
* jsp
*/
public class AlgoFrame extends JFrame{
private int canvasWidth;
private int canvasHeight;
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
super(title);
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
AlgoCanvas canvas = new AlgoCanvas();
setContentPane(canvas);
setResizable(false);
pack(); // 在setResizable(false)后进行pack(),防止在Windows下系统修改frame的尺寸
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public AlgoFrame(String title){
this(title, 1024, 768);
}
// 接口方法
public int getCanvasWidth(){return canvasWidth;}
public int getCanvasHeight(){return canvasHeight;}
// TODO: 设置自己的数据
private MazeData data;
public void render(MazeData data){
this.data = data;
repaint();
}
private class AlgoCanvas extends JPanel{
public AlgoCanvas(){
// 双缓存
super(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// 抗锯齿
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.addRenderingHints(hints);
// 具体绘制
// TODO: 绘制自己的数据data
int h =canvasHeight/MazeData.N();
int w =canvasWidth/MazeData.M();
for(int i=0;i<MazeData.N();i++){
for(int j=0;j<MazeData.M();j++){
if(MazeData.getMaze(i, j)==MazeData.Wall){//如果遇到的是#号表示墙
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
}else{
AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
}
//如果当前绘制的矩形是在走的路径中,则用黄色笔绘制出来.
if(MazeData.path[i][j]){
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Pink);
}
// 绘制实心矩形
//绘制实心矩形
AlgoVisHelper.fillRectangle(g2d, j*w, i*h, w, h);
}
}
}
@Override
public Dimension getPreferredSize(){
return new Dimension(canvasWidth, canvasHeight);
}
}
}
3、控制层
package Maze;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/*
* 控制层servlet
*/
public class AlgoVisualizer {
// TODO: 创建自己的数据
// 创建一个方向的二维数组 d
private static final int d[][]={{-1,0},{0,1},{1,0},{0,-1}};
private MazeData data; // 数据
private AlgoFrame frame; // 视图
private static int blockside = 6;
// 延时
private static int Delay = 5;
public AlgoVisualizer(String filename) {
// 初始化数据
// TODO: 初始化数据
data =new MazeData(filename);//把画好的二维迷宫图帮过来
// 定义画布的大小
int sceneWidth = data.M() * blockside;
int sceneHeight =data.N() * blockside;
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Welcome", sceneWidth, sceneHeight);
// TODO: 根据情况决定是否加入键盘鼠标事件监听器
//frame.addKeyListener(new AlgoKeyListener());
//frame.addMouseListener(new AlgoMouseListener());
new Thread(() -> {
run();
}).start();
});
}
// 动画逻辑
private void run() {
setData(-1,-1);
// 递归的走迷宫,初始参数为入口的坐标
go(data.getEntranceX(), data.getEntranceY());
setData(-1,-1);
}
//private void go(int x, int y) {
private boolean go(int x, int y) {
// TODO Auto-generated method stub
if (!data.inArea(x, y)) {
throw new IllegalArgumentException("x,y are out of Area");
}
// 设置当前该店已经被访问过
data.visited[x][y] = true;
setData(x,y);
// 递归的基本问题,x,y 为出口的坐标点值则递归终止
if (x == data.getExitX() && y == data.getExitY()) {
// return ;
return true;
}
// 对左下右上四个方向依次遍历,递归的实现走迷宫。
for (int i = 0; i < 4; i++) {
int newX = x + d[i][0];
int newY = y + d[i][1];
// 当 newX,newY 满足在面板范围内,且是可以走的路,且 newX,newY 没有被访问过,才可以走向 newX,newY
if (data.inArea(newX, newY) && data.getMaze(newX, newY) ==
MazeData.Path && !data.visited[newX][newY]) {
//go(newX, newY);
if(go(newX,newY)){
return true;
};
}
}
data.path[x][y]=false;
//setData(x, y);
return false;
}
//渲染
private void setData(int x,int y) {
// TODO Auto-generated method stub
if (data.inArea(x, y)) {
data.path[x][y] = true;
}
frame.render(data);
AlgoVisHelper.pause(Delay);
}
// TODO: 根据情况决定是否实现键盘鼠标等交互事件监听器类
private class AlgoKeyListener extends KeyAdapter {
}
private class AlgoMouseListener extends MouseAdapter {
}
public static void main(String[] args) {
//int sceneWidth = 800;
//int sceneHeight = 800;
// TODO: 根据需要设置其他参数,初始化visualizer
//AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight);
String filename="F:\\Java\\Eclipse\\GUI\\bin\\maze_101_101.txt";//二维码的地图路径
AlgoVisualizer al=new AlgoVisualizer(filename);
}
}
4、辅助工具类
package Maze;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.lang.InterruptedException;
/*
* 工具类 ,封装了画笔的方法
*/
public class AlgoVisHelper {
private AlgoVisHelper(){}
// 常用颜色
public static final Color Red = new Color(0xF44336);
public static final Color Pink = new Color(0xE91E63);
public static final Color Purple = new Color(0x9C27B0);
public static final Color DeepPurple = new Color(0x673AB7);
public static final Color Indigo = new Color(0x3F51B5);
public static final Color Blue = new Color(0x2196F3);
public static final Color LightBlue = new Color(0x03A9F4);
public static final Color Cyan = new Color(0x00BCD4);
public static final Color Teal = new Color(0x009688);
public static final Color Green = new Color(0x4CAF50);
public static final Color LightGreen = new Color(0x8BC34A);
public static final Color Lime = new Color(0xCDDC39);
public static final Color Yellow = new Color(0xFFEB3B);
public static final Color Amber = new Color(0xFFC107);
public static final Color Orange = new Color(0xFF9800);
public static final Color DeepOrange = new Color(0xFF5722);
public static final Color Brown = new Color(0x795548);
public static final Color Grey = new Color(0x9E9E9E);
public static final Color BlueGrey = new Color(0x607D8B);
public static final Color Black = new Color(0x000000);
public static final Color White = new Color(0xFFFFFF);
// 绘制空心圆
public static void strokeCircle(Graphics2D g, int x, int y, int r){
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
g.draw(circle);
}
// 绘制实心圆
public static void fillCircle(Graphics2D g, int x, int y, int r){
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
g.fill(circle);
}
// 绘制空心矩形
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
g.draw(rectangle);
}
// 绘制实心矩形
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
g.fill(rectangle);
}
// 设置画笔颜色
public static void setColor(Graphics2D g, Color color){
g.setColor(color);
}
// 设置画笔粗细
public static void setStrokeWidth(Graphics2D g, int w){
int strokeWidth = w;
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
// 暂停t毫秒
public static void pause(int t) {
try {
Thread.sleep(t);
}
catch (InterruptedException e) {
System.out.println("Error sleeping");
}
}
// 放置图片
public static void putImage(Graphics2D g, int x, int y, String imageURL){
ImageIcon icon = new ImageIcon(imageURL);
Image image = icon.getImage();
g.drawImage(image, x, y, null);
}
// 放置文本
public static void drawText(Graphics2D g, String text, int centerx, int centery){
if(text == null)
throw new IllegalArgumentException("Text is null in drawText function!");
FontMetrics metrics = g.getFontMetrics();
int w = metrics.stringWidth(text);
int h = metrics.getDescent();
g.drawString(text, centerx - w/2, centery + h);
}
}
附加迷宫地图
101 101
#####################################################################################################
# # # # # # # # # # # # # # # #
# # # ### # ############# ### # ##### ##### ##### ### ##### # # ####### ####### # # # # ### ### ### #
# # # # # # # # # # # # # # # # # # # # # # # # #
### # # ### ####### ### ######### ####### # ### # ######### ### ######### # ############### # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
### ####### # # # ### # ### ### # ### ##### ######### # ### # ### # # # # ##### # # ##### ###########
# # # # # # # # # # # # # # # # # # # # # # # #
# # # ####### ### ### ### ### # ##### ############### # ##### ### # ### ####### # # # # # ##### # ###
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
### # ### ### ### ### ### ### ########### ### # # # # ##### ### ### # # ### # # # # ### # ####### # #
# # # # # # # # # # # # # # # # # # #
# # ### ### # ### ### # ### ##### # # ####### # # # ##### # ##### ######### # # # # ### # ####### # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # ### # # ####### # # # ##### ### # # ####### ### # # ### ### ##### # ######### # # # ### # # ###
# # # # # # # # # # # # # # # # # # # # # # # # #
# ### ####### ##### # # # # ######### ### # ### ####### # ######### ##### ####### # # # # ### # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # ### ##### # ####### ##### # ### # ##### # ####### ########### # ##### # ######### #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # ### # # ### # ### # ### # # ### ######### # # # ### ######### # ############# # # ### ######### #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # ##### # # # ##### # ### # # ### # # ##### # ### # ######### # # ### # ##### # # # # # ### #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # ##### # # # # # ### # # # ### ####### # ### # ### # # # ### # # # ### ####### # ### ##### ###
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ##### ### # # ##### # ### ### # ########### ######### ### ####### # # # # ##### ##### ##### # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# ### ##### ### ##### # ##### ### # # ### # ####### # # ##### # ##### # ### # ### ##### ### ### # # #
# # # # # # # # # # # # # # # # # # # # # # #
### ####### ### # ################### ### ####### ####### ### ### # # # # # # ##### ##### # #########
# # # # # # # # # # # # # # # # # # #
# # ####### ##### # # ### # # # # ### ### ### ##### # ##### # ############### # ### # ### # ### #####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # ####### ######### ##### # ### # # ### # # # ##### # ### # ### # # # ### ### # ##### # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
##### ##### # ##### # # # # # # ##### ##### ### ##### # ##### # # # # ################# ### ####### #
# # # # # # # # # # # # # # # # # # # # # # # #
### ### # # # ### # ####### ##### ### # ##### # # ##### # # # # # ### ##### ##### ##### ### # ### ###
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ##### ##### ### # # ##### # ##### # # # ##### # ##### # # ######### # ##### # ####### ### # ##### #
# # # # # # # # # # # # # # # # # # # # # # # # #
# ### # ##### ####### ####### ################# # ####### # ######### # # ##### # ############# #####
# # # # # # # # # # # # # # # # # # # # # # # #
# ### ### # # # # # # ##### ### # # ### # ### ### # # ##### ##### ### ### ############### ####### # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# ### ####### ### # ####### ### # ####### # ######### ### ##### ##### ############# ##### ##### # ###
# # # # # # # # # # # # # # # # # # #
# ##### # ### ##### ### ### ##### # ### ### ##### # # # # # # # ##### # # # # ### ########### # #####
# # # # # # # # # # # # # # # # # # # # # # # # #
# ##### # # # # # # ####### # ##### ### # ##################### # # ########### # ##### ### ### # ###
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
### ### ### ### # # # ### # # # ### # # # ####### # # # # # ########### # # # ### # ### ### ##### # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
####### ### # ### ####### ##### # # # ### # # ##### ### # ####### ####### # # ### # # # ########### #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
####### ### # ### ### ### # ####### # # # # # # ### ### ##### # ##### ####### ### # # # ####### # ###
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
### ### ######### # # ####### ##### # # # ##### ##### ### # # # ##### # # ### ######### ### # # #####
# # # # # # # # # # # # # # # # # # # # # # #
##### # # # # ### # # ##### # ##### ##### ### # ### # ####### ##### ### # ####### # # ### ##### #####
# # # # # # # # # # # # # # # # # # # # # # # # # # #
##### ####### # ### # ##### # # ####### # # ### ### # ### ### ### # ####### ##### # ####### # # ### #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # ##### ####### ### # # # ### ### # # # # # ################### # # ##### # # ### # # #########
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ##### ##### ##### # ##### ### ########### # # # # ### # ### # ##### # # # # # # ####### ##### # ###
# # # # # # # # # # # # # # # # # #
# ##### # ########### # # # # # ##### ### # ### # # # # ######### # # # ### ### # ### ### ### # #####
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
### ### # ### # ############# # # # ########### # ### # # ### ##### # # # # ##### ### ### ##### #####
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
### # # # # ### # # # ##### ### ### ### # ##### ##### # # # ############### # ######### ### # ##### #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # ### ########### # # ########### # # # # # ##### # # ### ##### ####### # ### # # # ### ### ##### #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # ### ### ### # ##### # # # ##### # ### ### ### # # # ##### # # # ##### # ### # ##### # ##### #####
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# ##### ##### ### ##### ##### ##### ##### # # ### ### # # ####### # ##### # # ##### ### # # # ##### #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # ### # ##### # # # ### ####### # # # # # ### ####### # ######### # # # ### ##### # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
##### # # # ####### # ######### ### ####### # # ######### # # ### ####### ### # ######### ###########
# # # # # # # # # # # # # # # # # # # # # # # # #
# # ### ### # ##### ### # # ### ######### ##### # # ##### ####### # ### # ##### # ### ### # ### # ###
# # # # # # # # # # # # # # # # # # # # # #
# ######### # ##### # # # ##### # ### ####### # # # ##### # ####### ##### ### # # ### ### # # # # ###
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
### # ### # # ####### # # # ######### ############# ##### # ####### ##### # ### ### ######### # # # #
# # # # # # # # # # # # # # # # # # # # # # #
### ######### ##### ### ### # # ### # ######### ### # # ### # ### # # ### # ### # # ######### #######
# # # # # # # # # # # # # # # # # # # # # # # #
############# ### # # # ### # # # ### ### ##### ################### ##### # ### ### ### # ### # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
### # ##### # # # ##### ### # # # # # ####### # ### ### # # ### # # # ##### # # ### # # # # ### # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
### ##### # # ##### # ##### # ### # # # # # # # ### # # # # ##### # # # ####### # ##### ### # ##### #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
### # # # ### ####### ### # # ##### ##### # ### ####### # # # # ### # # # ##### # # ### ### # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#####################################################################################################