Re: Animation Problem
James Sarjeant wrote:
I have created a scrollForward() method which updates everything just once
for the next step in the sequence and for the playAnim() method I call
scrollForward(), then wait for a delay (using the system time and a speed
variable) then scrollForward() again until the end is reached. The only
problem is that the scrollForward() method works fine but the playAnim()
method simply calculates all the steps but only shows the final state of the
images with no intermediate steps. Does anyone have any ideas of how to get
this to work? Due to the nature of the program I cant use a thread, hence
the system time wait method but it just doesnt put the pictures on the
screen. The two methods are below if they provide any insights.
No need to skip every other line in your Usenet source listings. It doesn't
actually enhance readability - quite the contrary, actually. Generally on
Usenet you can and should reduce, but not eliminate, whitespace to balance
compactness and readability. Your indent level of 4 is acceptable, but you
can get away with one less indent level for method bodies, i.e., align the
method-body braces with the method signature and everything within moves one
level left also.
public void scrollForward()
{
if( (programCount + 1) < numInstructions)
{
if(compiled)
{
updatePC(true);
updateImages();
}
else
{
showError(
"Your machine code must be compiled before you can scroll.",
"Compile error");
}
}
else
{
showError(
"No more instructions to show.",
"Input error");
}
}
public void playAnim()
{
while (running)
{
scrollForward();
long current = System.currentTimeMillis();
long start = current + (speed * 250);
while(System.currentTimeMillis() <= start)
{
// wait and do nothing
}
if(programCount < numInstructions-1)
{
running = false;
}
}
System.out.println("end Anim");
}
The scrollForward works fine but as soon as it is run in a loop it works but
doesnt show the images. Any ideas are welcome.
Whatever thread runs playAnim() is going to choke the system, needlessly.
Don't do busy-wait loops like that. Use a Timer, javax.swing.Timer if this is
a Swing app.
If this is a Swing app, then what's with the System.out.println() call, mm?
Not to digress too much, but logging should use a logging library
(java.util.logging, org.apache.log4j) and log things that matter, at the
appropriate Levels.
Is this a Swing app?
Assuming that it is, if you try to update components off the Event Dispatch
Thread, the EDT, you likely won't see any updates of the screen until after
the program ends. If you try to perform extensive calculations or other
activities on the EDT, you likely won't see any updates of the screen. What
you must do is calculate, retrieve, transform, fold, mutilate and spindle off
the EDT, then post /faits accomplis/ to the EDT for nothing but display
updates, i.e., paintComponent() calls. (Event handling I leave as a separate
topic.) With judicious use of a javax.swing.Timer you can force periodic
updates to the display of image during the background construction of the image.
OK, that does pull you into the world of event handling. You're going to have
fun.
--
Lew
This post contains two questions, but really both ask the same thing in a
different way, so only one request for information.