• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Lighting the fires of Gondor

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm having trouble getting my animation to pause at the right time.
I've created the class SignalTower as an extension of GCompound from acm.graphics.*. In my program BeaconsOfGondor.java, I create 9 SignalTower objects that are linked together and drawn in a GraphicsProgram from acm.program.*. The purpose of the program is to "light" the tower the user clicks on with the mouse, and then "light" every tower after it with a 0.5 second pause in between.

This is the code in BeaconsOfGondor.java that programs the mouse click to light the first tower:


This is the code in SignalTower.java that is called:



My problem is that when a tower is clicked, all the pauses happen at once, followed by all the towers immediately being filled with red, instead of the towers filling with red one at a time with a 0.5 second pause in between. I've tried placing the pause at all the possible places among the statements within signal() and lightCurrentTower(), but they all give the same response.
Can anyone help me understand why the program is not pausing in-between filling each tower instead of collecting the pauses into one? I would really appreciate the insight.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're desired lighting sequence suggests a loop:



It's not clear from the code provided how your code decides the next next tower (and beyond) and lights them, but based on your result, it must all occur after the pause you've shown us without any other pausing. In other words, not in a loop as shown above.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, from one Ernest to another!

Java has a special thread called the Event Dispatch Thread. It has two important jobs: one is to call event handlers like your mouseClicked method, and the other is to call paint() methods to repaint the screen. All of these things happen in the same thread. As long as an event handler method is running, no paint methods can be called, and vice versa. That explains why your pauses happen "all at once" -- you don't see any screen updates until your event handler returns.

Therefore it's important that such methods run quickly and return as soon as possible. If you have to do ongoing work -- as you always must when doing animation -- you must do that ongoing work in a separate thread. Here is an article about using a thread to do animation in Java.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again. This sounds a GUI-related question, so I shall move it to our gUIs foru,m.
 
reply
    Bookmark Topic Watch Topic
  • New Topic