I am writing a simple projectile motion simulation, I would like to update information about distance covered and current height in real-time. I have a text field called distanceCovered
, a function updating state and a thread running this function. I include the source codes:
Updating function:
public void updateAnimation()
{
object.setCenterX(model.getCurrentDisplacement());
object.setCenterY(model.getCurrentHeight());
int distInfo = (int)model.getCurrentDisplacement();
String text1 = Integer.toString(distInfo);
distanceCovered.setText(text1);
}
Thread:
private class Timer extends Thread
{
public synchronized void run()
{
while(model.getCurrentHeight() < 400)
{
try
{
double dt = model.get_dt();
wait((long) (1000 * dt));
model.update();
updateAnimation();
}
catch (InterruptedException ex)
{
}
}
}
}
When I remove the function updating the text of the TextField, everything works perfectly well. When this function is there in the code, it updates the text property but only about 60 times, then, it throws the Exception.
Should I include anything more from my code?