Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Trouble using Thread.sleep()......

Reply
Thread Tools

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

 
 
Lee Garrington
Guest
Posts: n/a
 
      10-01-2003
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




 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      10-01-2003
In article <3f7b24b7$0$11001$(E-Mail Removed)>,
"Lee Garrington" <(E-Mail Removed)> wrote:

>: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
 
Reply With Quote
 
 
 
 
Lee Garrington
Guest
Posts: n/a
 
      10-01-2003
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?

"Steve W. Jackson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-berlin.de...
> In article <3f7b24b7$0$11001$(E-Mail Removed)>,
> "Lee Garrington" <(E-Mail Removed)> wrote:
>
> >: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



 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      10-01-2003
In article <3f7b2976$0$10979$(E-Mail Removed)>,
"Lee Garrington" <(E-Mail Removed)> wrote:

>: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?
>:
>:"Steve W. Jackson" <(E-Mail Removed)> wrote in message
>:news:(E-Mail Removed)-berlin.de...
>:> In article <3f7b24b7$0$11001$(E-Mail Removed)>,
>:> "Lee Garrington" <(E-Mail Removed)> wrote:
>:>
>:> >: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 =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble using a Printer on a Wireless Home Network =?Utf-8?B?TGVzbGllIFVuZGVyd29vZA==?= Wireless Networking 2 12-26-2007 07:52 AM
i have no trouble to send , ihave trouble reciving mail --any ideas John Penney Computer Support 4 08-29-2006 08:45 PM
Trouble using a hostname in the address field for the Radius client in IAS Ned Cisco 0 08-11-2005 02:59 PM
Trouble using the vbTab in string F. Algr°y ASP .Net 1 01-06-2004 01:39 AM
trouble with caching or caching the trouble Hypo ASP .Net 6 08-01-2003 07:11 AM



Advertisments