Trouble using Thread.sleep()......

L

Lee Garrington

Hello,

Basically this is my problem. I am programming minesweeper in java. I have
a panel in the background on which I draw all the mines/numbers etc. and
then I have an array of buttons that sit on top of them.

I am developing a solver and need a pause between each move the algorithm
makes. I know from my past experience that you have to use something along
the lines of :-

Graphics g = board.getGraphics();
board.paintComponent(g);
g.dispose();

to get the board to update after each successive move but I seem to be
having problems getting it working properly.

- If I use the above to repaint the background then all the buttons
disappear.
- If I use the above to repaint all the buttons it doesnt actually repaint
them, it just goes grey.
-If I try and repaint both, wherever moves are made by the computer you can
see the relevant square in the background but none of the other buttons are
drawn and its just grey instead.

After completeing the algorithm it all redraws as normal.

I just want everything to refresh as normal when each move by the computer
is made, can anyone help me out?

Thx in advance
 
S

Steve W. Jackson

Lee Garrington said:
:Hello,
:
:Basically this is my problem. I am programming minesweeper in java. I have
:a panel in the background on which I draw all the mines/numbers etc. and
:then I have an array of buttons that sit on top of them.
:
:I am developing a solver and need a pause between each move the algorithm
:makes. I know from my past experience that you have to use something along
:the lines of :-
:
:Graphics g = board.getGraphics();
: board.paintComponent(g);
: g.dispose();
:
:to get the board to update after each successive move but I seem to be
:having problems getting it working properly.
:
:- If I use the above to repaint the background then all the buttons
:disappear.
:- If I use the above to repaint all the buttons it doesnt actually repaint
:them, it just goes grey.
:-If I try and repaint both, wherever moves are made by the computer you can
:see the relevant square in the background but none of the other buttons are
:drawn and its just grey instead.
:
:After completeing the algorithm it all redraws as normal.
:
:I just want everything to refresh as normal when each move by the computer
:is made, can anyone help me out?
:
:Thx in advance
:

You should never call paintComponent() directly. You should instead
always call repaint(), which posts an event that calls paint(), which in
turn calls paintComponent() (among others). This is all Swing, of
course. But perhaps this is behind the problem, since you're
interfering with the normal behavior of the Event Dispatching mechanism.

= Steve =
 
L

Lee Garrington

Yeah I know I shouldnt directly call it but in the past that is the only way
I have ever managed to prevent the gui from freezing.

repaint() just doesnt work. It doesnt repaint anything unfortunately and I
dont exactly know why.

Maybe there is another solution to this type of problem?
 
S

Steve W. Jackson

Lee Garrington said:
:Yeah I know I shouldnt directly call it but in the past that is the only way
:I have ever managed to prevent the gui from freezing.
:
:repaint() just doesnt work. It doesnt repaint anything unfortunately and I
:dont exactly know why.
:
:Maybe there is another solution to this type of problem?
:
::> In article <[email protected]>,
:>
:> >:Hello,
:> >:
:> >:Basically this is my problem. I am programming minesweeper in
:> >:java. I
:have
:> >:a panel in the background on which I draw all the mines/numbers
:> >:etc. and then I have an array of buttons that sit on top of them.
:> >:
:> >:I am developing a solver and need a pause between each move the
:algorithm
:> >:makes. I know from my past experience that you have to use
:> >:something
:along
:> >:the lines of :-
:> >:
:> >:Graphics g = board.getGraphics();
:> >: board.paintComponent(g); g.dispose();
:> >:
:> >:to get the board to update after each successive move but I seem
:> >:to be having problems getting it working properly.
:> >:
:> >:- If I use the above to repaint the background then all the
:> >:buttons disappear. - If I use the above to repaint all the
:> >:buttons it doesnt actually
:repaint
:> >:them, it just goes grey. -If I try and repaint both, wherever
:> >:moves are made by the computer you
:can
:> >:see the relevant square in the background but none of the other
:> >:buttons
:are
:> >:drawn and its just grey instead.
:> >:
:> >:After completeing the algorithm it all redraws as normal.
:> >:
:> >:I just want everything to refresh as normal when each move by the
:computer
:> >:is made, can anyone help me out?
:> >:
:> >:Thx in advance
:> >:
:>
:> You should never call paintComponent() directly. You should instead
:> always call repaint(), which posts an event that calls paint(), which in
:> turn calls paintComponent() (among others). This is all Swing, of
:> course. But perhaps this is behind the problem, since you're
:> interfering with the normal behavior of the Event Dispatching mechanism.
:>
:> = Steve =
:> --
:> Steve W. Jackson
:> Montgomery, Alabama

The GUI will freeze if you're doing things on the Event Dispatching
Thread. You shouldn't do that, in general. Instead, put your
activities in separate threads, have them draw and do their other work
as needed, and then have that thread call repaint(). The repaint()
method does nothing more than post a notice for the EDT to repaint the
specified component -- which means those events will be coalesced if
multiples exist. I've had really good luck with GUI responsiveness once
I learned the secret of doing most everything in a thread other than the
EDT.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top