I was trying build a Maze_Generator using DFS. I have implemented the algorithm successfully, but it is not visual (i.e. as soon as I run the program, the complete grid is coloured, but I want to see each box getting coloured in real time).
Below is Draw class, which draws the Grid and colours them, if the box has been marked visited(by algorithm).
class Draw extends JPanel{
public Draw() {}
public void paintComponent(Graphics g1) {
super.paintComponent(g1);
Graphics2D g= (Graphics2D) g1;
//Code Below makes the grid
for(Cell c: Maze_Generator.list) { //Maze_Generator.list, this is ArrayList, storing all the cells/boxes objects
//Cell c =(Cell)a;
if(c.top())
g.drawLine(c.x(), c.y(), c.x()+c.length(), c.y());
if(c.bottom())
g.drawLine(c.x(), c.y()+c.length(),c.x()+c.length(),c.y()+c.length());
if(c.left())
g.drawLine(c.x(), c.y(), c.x(), c.y()+c.length());
if(c.right())
g.drawLine(c.x()+c.length(), c.y(), c.x()+c.length(), c.y()+c.length());
}
//Code below colours the boxes if marked visited
for(Cell c:Maze_Generator.list) {
if(c.visited()) {
g.setColor(Color.cyan);
g.fillRect(c.x()+1, c.y()+1, c.length()-1, c.length()-1);
g.setColor(Color.black);
}
}
}
}
What and how should I do to slow down/delay the code when colouring the boxes? Any suggestions?