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
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