repaint problem

后端 未结 1 1700
孤独总比滥情好
孤独总比滥情好 2021-01-28 05:34

I have a problem with my repaint in the method move. I dont know what to doo, the code is below

import java.awt.*;
import java.io.*;
import java.text         


        
相关标签:
1条回答
  • 2021-01-28 06:18

    Assuming that you see the progress in the println statements but not on the screen, it is because a call to repaint is not synchronous. Swing has a special thread for handling UI - called Event Dispatch Thread. A call to repaint is handled by that thread, but asynchronously - after all the current events scheduled on that thread have been processed.

    When you call hanoi in your actionPerformed, that is done on the same UI thread. What happens is that until your recursion is fully done, repaint() calls are just queued. Once the recursion completes (and all stacks have been moved in the model), the UI thread processes all repaint() requests, painting - what i assume - the final state.

    What you need to do is to separate the model processing into a separate worker thread. On every recursion step, issue a repaint() call and sleep for a few hundred milliseconds. This will allow the UI thread to repaint the current state of the model and let the user actually trace the progress.

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