0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Code Hard
  • 43
  • 4
  • https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html – Christophe Roussy Dec 03 '20 at 10:13
  • Does this answer your question? [Using sleep in JPanel](https://stackoverflow.com/questions/41247668/using-sleep-in-jpanel) – R4yY Dec 03 '20 at 10:14
  • You should not use sleep() in the central gui thread. Instead, use another thread as a Timer. – NomadMaker Dec 03 '20 at 10:16
  • using sleep, pauses my main thread, and UI does not show up(only white screen). And when using a new thread specifically for Sleep method, code dealing with graphics does not work inside this new Thread(but regular output print code does work tho) – Code Hard Dec 03 '20 at 12:24
  • Look below. Don't use `Thread.sleep(int)`. – TimonNetherlands Dec 03 '20 at 13:02
  • See an example of updating a grid using SwingWorker [here](https://stackoverflow.com/a/50781879/3992939). It can also be achieved using a Swing Timer as TimonNetherlands suggested. **_"it seemed i was not clear with my question"_** : for more help post [mre]. – c0der Dec 04 '20 at 15:28

1 Answers1

0

You'll need a timer, otherwise you'll block your main thread. This should work, but improve it to fit your needs:

// add necessary imports first:
import javax.swing.Timer;
import java.awt.event.ActionListener;
int milliseconds = 500;
for(Cell c:Maze_Generator.list) {
    if(c.visited()) {
        fillRect(milliseconds, g, c.x()+1, c.y()+1, c.length()-1, c.length()-1);
        milliseconds += 500;
    }
}

Add this method:

    private void fillRect( int delay, Graphics2D g, int x, int y, int width, int height ) {
        Timer timer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                g.setColor(Color.cyan);
                g.fillRect(x, y, width, height);
                g.setColor(Color.black);
            }
        });
        timer.setRepeats( false );
        timer.start();
    }
TimonNetherlands
  • 1,033
  • 1
  • 6
  • 6