Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie with question about repaint() and sleep()  RSS feed

 
Paul Yelton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolute newbie to Java coming over from C++ just picked up the book today. I decided to try the old Towers of Hanoi problem with the new stuff I learned and am confused how to get around a problem. Basically I want the computer to make the next move, redraw the screen to show the new move, wait for a second, and then continue. What instead happens everytime I use either thread.sleep(1000) or a for loop delay is the screen never updates until all of the moves have been made and all of the pauses added up. I'm not using threads, just a simple applet. Am I doing something wrong? The program DOES work if I use a input dialog box in the same spot requiring the user to hit enter. Here is the snippet of my code:

 
Magnus Andersson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure it works, but try doing the repaint() in an own thread as a java SwingWorker.

Ref http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

/Dime
www.poker-beginner.com
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

The problem is symptomatic of putting the event dispatch thread to sleep, i.e. it's snoozing so it can't repaint the window.

Magnus's suggestion is one way to go. Basically, you're not using threads and you should be. You might want to look at the javax.swing.Timer class too.

Newbie, eh? Jump in the deep-end, why don't ya?!

Jules
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!