• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lighting the fires of Gondor  RSS feed

 
Ernest Myers
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again. This sounds a GUI-related question, so I shall move it to our gUIs foru,m.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!