I make an animation of the search for an exit from the maze.

In the paintComponent() method, draw the entire matrix. Well and for animation I cause repaint() . If the grid is 50х50 , then the redrawing is relatively fast, it is pleasant to observe. But I downloaded the maze 1000х1000 and I understand that even without delay the animation will spin for 2 weeks ...

What is the way out? For example, draw only what really changed, and not all cells.

  • The solution, for example, is to draw only every hundredth step. And how much time is the algorithm for a 1000x1000 matrix without drawing? - Markov
  • Full bypass BFS or DFS is very fast, a second or two. And I would like to draw all the same at every step ... - Oleksiy Morenets

1 answer 1

Look, the second for the algorithm is not fast, the second is the whole processor core occupied in a second, billions of cycles. Now imagine that every step of your algorithm, which takes one million seconds, must be weighted by redrawing the entire field, which is a hard ui-operation. Another experiment - count the total number of steps that the algorithm needs, divide it into 30 (30 frames per second - the maximum that can catch human eyes). So, if you really want to show the user every step, you will get the time it takes, and it will be huge. So without fear of skipping steps, and draw every hundredth / thousandth step, you can do a specific highlight so that, for example, the user sees all the cells counted for missing steps at once, for example - highlighted in a different color.

Well, if you really just want to optimize - instead of redrawing the entire field, you can redraw only the updated cell.

  • instead of redrawing the entire field, you can redraw only the updated cell --- how ??? - Olexiy Morenets
  • If you don’t call super () in paintComponent (), then the old state will not be cleared - you can draw the necessary pieces over the old ones (these pieces need to be memorized somewhere when rendering). Plus - if only a specific square piece is redrawn - you can call a repeint of only this area docs.oracle.com/javase/8/docs/api/java/awt/… , but this will not give a special increase, if not combined with the previous approach - markov
  • Oh, it might work. I suppose that if you move / resize a window during an animation, the old content is not drawn. I'm right? - Olexiy Morenets
  • When reside - it is better to redraw the entire canvas - markov
  • Have you estimated the number of steps when calculating the maze 1000x1000? - Markov