This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Program logic runs but graphics freeze Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Program logic runs but graphics freeze" Watch "Program logic runs but graphics freeze" New topic
Author

Program logic runs but graphics freeze

richard rehl
Ranch Hand

Joined: May 21, 2007
Posts: 36
Hi there,

Here's a really pointless piece of code (hey, it was suggested by Deitel in JHTP!) that has a quirk I can't figure out. Basically, it's the old tortoise and hare race, with lovely icons running up the hill... so I got the program logic to work first, then the graphics, then implemented a JButton with an ActionListener which is supposed to restart the run() method. And, it does... I can see by the terminal output that the program is running again. However, the graphics freeze and don't restart. Any suggestions? I tried to attach the graphics as a zipped file but am told I can't. Thanks for wasting your time!!!


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
Read the secton from the Swing tutorial on "Concurrency".

When you press the button to run the game, the code executes on the EDT which is the Thread that Swing uses to repaint the components. You keep telling the Thread to sleep so it can't repaint anything until the whole loop finishes executing. The System.out.println(...) executes in a separate Thread so the text is still displayed on the terminal.

When you start the program and your code is executing in a normal AWT Thread so the sleep doesn't NOT cause the EDT to sleep so the GUI can repaint itself.

A better way to write the code is to start a Swing Timer to schedule the animation. The tutorial also has a section on using Timers.
 
Don't get me started about those stupid light bulbs.
 
subject: Program logic runs but graphics freeze
 
Similar Threads
tortoise and hare race java applet not working properly?
Making Typing "Game", any tips or pointers before I start?
How to handle events in java
Issues when running Applet
a very elusive bug