我正試圖將一個迷宮生成器可視化,我不知道如何在每次訪問單元格/節點時進行延遲。我讀到過,你不應該在GUI應用程式中使用Thread.sleep(),因為它會擾亂事件調度執行緒,所以我試著利用Swing.Timer,然而這對我來說也是不可行的。每當我嘗試時,程式就會凍結,彈出的視窗是空白的。
這是我用來顯示尋路方法的每一步的GUI類:
這是我用來顯示尋路方法的每一步的GUI類。
import java.awt.BasicStro;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font。
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Jpanel;
import javax.swing.Timer。
public class GUI extends JPanel{
static int frameWidth = 1920;
static int frameHeight = 1080;
定時器定時器。
int CELL_SIZE = 30;
int STROKE_WIDTH = 4;
int x = 60;
int y = 30;
boolean solve = false;
MazeWilson m;
public GUI() {
timer = new Timer(1000,this) 。
Timer.start();
m = new MazeWilson(x, y)。
m.generatePath()。
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g)。
this.setBackground(Color.WHITE)。
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(STROKE_WIDTH))。
g2.setColor(Color.BLACK)。
for (int i = 0; i< m.height; i ) {
for (int j = 0; j < m.width; j ) {
g2.drawRect(10 CELL_SIZE * j, 10 CELL_SIZE * i, CELL_SIZE, CELL_SIZE) 。
}
}
g.setColor(Color.WHITE)。
for (int i = 0; i< m.height; i ) {
for (int j = 0; j < m.width; j ) {
if (!m.field[i][j].wall[0] ) {
g2. drawLine(10 STROKE_WIDTH CELL_SIZE * j, 10 CELL_SIZE * (i 1)。10 - STROKE_WIDTH CELL_SIZE * (j 1), 10 CELL_SIZE * (i 1))。
}
if (!m.field[i][j].wall[1]) {
g2. drawLine(10 CELL_SIZE * j, 10 STROKE_WIDTH CELL_SIZE * i。10 CELL_SIZE * j, 10 - STROKE_WIDTH CELL_SIZE * (i 1))。)
}
if (!m.field[i][j].wall[2] ) {
g2. drawLine(10 STROKE_WIDTH CELL_SIZE * j, 10 CELL_SIZE * i。10 - STROKE_WIDTH CELL_SIZE * (j 1), 10 CELL_SIZE * i)。)
}
if (!m.field[i][j].wall[3] ) {
g2. drawLine(10 CELL_SIZE * (j 1), 10 STROKE_WIDTH CELL_SIZE * i, 10 CELL_SIZE * (j 1), 10 - STROKE_WIDTH CELL_SIZE * (i 1))。
}
}
if (solve) {
drawSolution(g2);
}
}
public static void main(String[] args){
JFrame f = new JFrame("Maze") 。
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
GUI g = new GUI() 。
f.add(g)。
JButton solveMaze = new JButton("L?sen") 。
solveMaze.setFont(new Font("Arial", Font.PLAIN, 40) )。)
solveMaze.setPreferredSize(new Dimension(30, 30) )。
solveMaze.setBackground(Color.GREEN)。
solveMaze.setOpaque(true)。
f.add(solveMaze, BorderLayout.SOUTH);
f.setExtendedState(JFrame.MAXIMIZED_BOTH)。
f.getContentPane().setBackground(Color.BLACK)。
f.setVisible(true)。
f.toFront()。
}
public void drawSolution(Graphics g) {
Cell c = m.findPath()。
g.setColor(Color.CYAN)。
for (; c.parent != null; c = c.parent) {
g.fillRect(10 STROKE_WIDTH/2 c.x * CELL_SIZE. x * CELL_SIZE, 10 STROKE_WIDTH/2 c.y * CELL_SIZE, CELL_SIZE - STROKE_WIDTH, CELL_SIZE - STROKE_WIDTH)。)
switch (c.parent.y - c.y) {
case 1: {
g.fillRect(10 STROKE_WIDTH/2 c.x * CELL_SIZE, 10 STROKE_WIDTH/2 c. y * CELL_SIZE, CELL_SIZE - STROKE_WIDTH, CELL_SIZE CELL_SIZE/4 - STROKE_WIDTH)。)
break。
}
case -1: {
g.fillRect(10 STROKE_WIDTH/2 c.x * CELL_SIZE, 10 STROKE_WIDTH/2 c. parent.y * CELL_SIZE, CELL_SIZE - STROKE_WIDTH, CELL_SIZE CELL_SIZE/4 - STROKE_WIDTH)。)
break。
}
}
switch (c.parent.x - c.x ) {
case 1: {
g.fillRect(10 STROKE_WIDTH/2 c.x * CELL_SIZE, 10 STROKE_WIDTH/2 c. y * CELL_SIZE, CELL_SIZE CELL_SIZE/4 - STROKE_WIDTH, CELL_SIZE - STROKE_WIDTH)。)
break。
}
case -1: {
g.fillRect(10 STROKE_WIDTH/2 c.father.x * CELL_SIZE, 10 STROKE_WIDTH/2 c. y * CELL_SIZE, CELL_SIZE CELL_SIZE/4 - STROKE_WIDTH, CELL_SIZE - STROKE_WIDTH)。)
break。
}
}
}
g.fillRect(10 STROKE_WIDTH/2 c。 x * CELL_SIZE, 10 STROKE_WIDTH/2 c.y * CELL_SIZE, CELL_SIZE - STROKE_WIDTH, CELL_SIZE - STROKE_WIDTH)。)
}
}
generatePath-Method生成了迷宮,findPath-Method解決了迷宮。我希望drawSolution-函式在每次迭代中都有一個延遲,這樣它就會逐步顯示在這個程序中被訪問的每個單元。
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310274.html
標籤:
上一篇:MouseListener不呼叫mouseClicked方法
下一篇:設定一個不透明的內部私有面板

