编写以下程序:基于JavaFX和Java 用prim算法生成可视化迷宫 生成过程实时刷新
时间: 2024-03-12 12:43:27 浏览: 71
MazeGenerator:具有可视化功能的Java迷宫生成算法
以下是一个基于JavaFX和Java实现可视化迷宫生成的程序,同时支持实时刷新过程:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class VisualMaze extends Application {
private static final int MAZE_SIZE = 25; // 迷宫大小
private static final int CELL_SIZE = 20; // 格子大小
private static final int BORDER_WIDTH = 4; // 迷宫边框宽度
private static final int DELAY = 50; // 刷新延迟
private Canvas canvas; // 画布
private GraphicsContext gc; // 画笔
private int[][] maze; // 迷宫数组
private List<int[]> walls; // 墙列表
private boolean[][] visited; // 格子访问情况
private Random rand; // 随机数生成器
private Timeline timeline; // 时间轴
@Override
public void start(Stage primaryStage) throws Exception {
maze = new int[MAZE_SIZE][MAZE_SIZE];
walls = new ArrayList<>();
visited = new boolean[MAZE_SIZE][MAZE_SIZE];
rand = new Random();
// 初始化迷宫数组
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
maze[i][j] = 15;
visited[i][j] = false;
}
}
// 初始化墙列表
for (int i = 0; i < MAZE_SIZE; i++) {
for (int j = 0; j < MAZE_SIZE; j++) {
if (i > 0) walls.add(new int[] { i, j, 0 }); // 上墙
if (j > 0) walls.add(new int[] { i, j, 1 }); // 左墙
}
}
// 初始化画布
canvas = new Canvas(MAZE_SIZE * CELL_SIZE + 2 * BORDER_WIDTH, MAZE_SIZE * CELL_SIZE + 2 * BORDER_WIDTH);
gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setStroke(Color.BLACK);
// 初始化时间轴
timeline = new Timeline(new KeyFrame(Duration.millis(DELAY), e -> {
if (!walls.isEmpty()) {
int index = rand.nextInt(walls.size());
int[] wall = walls.remove(index);
int i = wall[0];
int j = wall[1];
int dir = wall[2];
int ni = i + (dir == 0 ? -1 : 0);
int nj = j + (dir == 1 ? -1 : 0);
if (!visited[ni][nj]) {
visited[ni][nj] = true;
maze[i][j] &= ~(1 << dir);
maze[ni][nj] &= ~(1 << (dir ^ 1));
drawCell(i, j);
drawCell(ni, nj);
}
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
// 初始化界面
BorderPane root = new BorderPane(canvas);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Visual Maze Generator");
primaryStage.show();
}
// 绘制单个格子
private void drawCell(int i, int j) {
gc.setFill(Color.WHITE);
gc.fillRect(j * CELL_SIZE + BORDER_WIDTH, i * CELL_SIZE + BORDER_WIDTH, CELL_SIZE, CELL_SIZE);
gc.setStroke(Color.BLACK);
if ((maze[i][j] & 1) == 0) gc.strokeLine(j * CELL_SIZE + BORDER_WIDTH, i * CELL_SIZE + BORDER_WIDTH,
(j + 1) * CELL_SIZE + BORDER_WIDTH, i * CELL_SIZE + BORDER_WIDTH); // 上墙
if ((maze[i][j] & 2) == 0) gc.strokeLine((j + 1) * CELL_SIZE + BORDER_WIDTH, i * CELL_SIZE + BORDER_WIDTH,
(j + 1) * CELL_SIZE + BORDER_WIDTH, (i + 1) * CELL_SIZE + BORDER_WIDTH); // 右墙
if ((maze[i][j] & 4) == 0) gc.strokeLine(j * CELL_SIZE + BORDER_WIDTH, (i + 1) * CELL_SIZE + BORDER_WIDTH,
(j + 1) * CELL_SIZE + BORDER_WIDTH, (i + 1) * CELL_SIZE + BORDER_WIDTH); // 下墙
if ((maze[i][j] & 8) == 0) gc.strokeLine(j * CELL_SIZE + BORDER_WIDTH, i * CELL_SIZE + BORDER_WIDTH,
j * CELL_SIZE + BORDER_WIDTH, (i + 1) * CELL_SIZE + BORDER_WIDTH); // 左墙
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序使用了Prim算法生成迷宫,并通过JavaFX实现了可视化效果。在程序中,我们使用了Canvas组件来绘制迷宫,使用Timeline来实现实时刷新效果。
阅读全文