Starting a new Java Thread for an animation loop

D

Doug Miller

I'm trying to update a graph several times (sort of like an animation
problem) in response to an AWT event from a button action. I think I need
to start a new Java Thread to do this so that the animation loop isn't
running from inside the AWT thread, but I'm having trouble figuring out how
to do this.

Here's the code that's giving me the problem. When I get the event, I run
startAnimate(). In startAnimate() I can run the new thread immediately, but
that seems to execute from inside the AWT thread, since it only gets
displayed after the final loop. Instead, I'm trying to get the thread
itself to run the thread, but I get no display at all this way. Any
pointers on what I should be doing instead?

Thanks,
Doug

public void startAnimate() {
wfallThread = new WfallAnimation();
WfallAnimationIsRunning = true;
//wfallThread.run();
}

private class WfallAnimation extends Thread implements Runnable {
// The constructor
public void WfallAnimation() {
if (WfallAnimationIsRunning) {
this.run();
}
}

public void run() {
double specdBarray[];
double[][] wfallarray;
wfallarray = new double[16][FFTLENGTH];

for (int k=0;k<int32array.length;k+=16*FFTLENGTH) {

// Plot 16 more spectrum plots
plotPanel.removeAll();
for (int i=0;i<16;i++) {
specdBarray = calcOneFFT(k+i*FFTLENGTH, FFTLENGTH);
for(int j=0;j<FFTLENGTH;j++) {
wfallarray[j] = specdBarray[j] + 5.0*i;
}
plotPanel.addCurve(wfallarray);
}

plotPanel.repaint();

System.out.println("restartPlot(): repaint");

try {
Thread.sleep(100);
}
catch (Exception e) {
System.out.println(e);
}

} //end for
WfallAnimationIsRunning = false;

} // end run()
} // end WfallAnimation class
 
P

Paul

Use start() instead of run() to begin your thread.
Don't start it in the constructor.
You don't need to override start(), it will call your run() automatically.

You could also make your flag for whether the thread is still running a
local variable to the thread with an accessor.

Try these changes, and also look for some sample code on how to use Threads
in Java.
--Paul
 
D

David Hilsee

Doug Miller said:
I'm trying to update a graph several times (sort of like an animation
problem) in response to an AWT event from a button action. I think I need
to start a new Java Thread to do this so that the animation loop isn't
running from inside the AWT thread, but I'm having trouble figuring out how
to do this.

Here's the code that's giving me the problem. When I get the event, I run
startAnimate(). In startAnimate() I can run the new thread immediately, but
that seems to execute from inside the AWT thread, since it only gets
displayed after the final loop. Instead, I'm trying to get the thread
itself to run the thread, but I get no display at all this way. Any
pointers on what I should be doing instead?
[code snipped]

First of all, if you're going to use a background thread to populate a GUI,
then you can't directly access the visible GUI components fro the background
thread. You must instead execute the code in the event thread using
something like SwingUtilities.invokeLater. See
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html for more
details.

Secondly, what you are doing looks suspiciously like a poor man's timer.
Look at javax.swing.Timer for an easy way to update a GUI periodically and
do so without having to worry about multithreading issues.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top