Grid DFS visualization

后端 未结 1 1401
梦如初夏
梦如初夏 2021-01-29 10:20

Hello guys I am working on a project where I am trying to create a maze generator.
So far I have a gird that is a 2D array of a Cell class and a JPanel that paints the gri

相关标签:
1条回答
  • 2021-01-29 11:12

    The following code demonstrates the use of SwingWorker to demonstrate performing long tasks (in your case dfs search) which update the gui.
    Specific dfs information was removed because they are not relevant:

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingWorker;
    
    public class Maze extends JPanel {
    
        private final Cell [][] maze;
        private final int dims;
        private static final int SIZE = 35;
    
        public Maze(int dim) {
            setPreferredSize(new Dimension( dim*SIZE,dim*SIZE));
            dims = dim;
            maze = new Cell[dims][dims];
        }
    
        public void generator()
        {
            for(int i = 0; i < maze.length; i++)
            {
                for (int j = 0;j < maze[0].length; j++)
                {
                    maze[i][j] = new Cell(i,j);
                    //set some arbitrary initial date
                    if(i%2 ==0 && j%2 ==0) {
                        maze[i][j].setVisited(true);
                    }
                }
            }
        }
    
        public void DFS()
        {
            new DFSTask().execute();
        }
    
        @Override
        public void paintComponent(Graphics g) //override paintComponent not paint
        {
            super.paintComponent(g);
            for (int row = 0; row < maze.length; row++)
            {
                for (int col = 0; col < maze[0].length; col++)
                {
                    g.drawRect(SIZE*row, SIZE * col , SIZE, SIZE);
                    if(maze[row][col].visited)
                    {
                        g.fillRect(SIZE*row, SIZE * col , SIZE, SIZE);
                    }
                }
            }
        }
    
        public static void main(String[] args)
        {
            Maze p = new Maze(10);
            p.generator();
            JFrame f = new JFrame();
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(p);
            f.pack();
            f.setVisible(true);
            p.DFS();
        }
    
        //use swing worker perform long task
        class DFSTask extends SwingWorker<Void,Void> {
    
            private static final long DELAY = 1000;
            private final Random rand = new Random();
    
            @Override
            public Void doInBackground() {
                dfs();
                return null;
            }
    
            @Override
            public void done() { }
    
            void dfs() { //simulates long process that repeatedly updates gui
    
                while (true){ //endless loop, just for demonstration
                    //update info
                    int row = rand.nextInt(dims);
                    int col = rand.nextInt(dims);
                    maze[row][col].setVisited(! maze[row][col].isVisited());
                    repaint(); //update jpanel
                    try {
                        Thread.sleep(DELAY); //simulate long process
                    } catch (InterruptedException ex) { ex.printStackTrace();}
                }
            }
        }
    }
    
    class Cell{
    
        private final int row, column;
        boolean visited;
    
        public Cell(int i, int j)
        {
            row = i;
            column = j;
            visited = false;
        }
    
        int getRow() {  return row; }
    
        int getColumn() {return column; }
    
        boolean isVisited() { return visited; }
    
        void setVisited(boolean visited) {  this.visited = visited;}
    
        @Override
        public String toString()
        {
            return row + " " + column;
        }
    }
    

    For more information about using SwingWorker see doc


    0 讨论(0)
提交回复
热议问题