0

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?

  • 1
    You can't update the UI from a background thread. – James_D Apr 22 '18 at 12:47
  • @James_D Why then can I change the position of the circle from a background thread? – LaTeXEnthusiast Apr 22 '18 at 12:53
  • You can't - in the sense that the outcome of doing that is not defined. – James_D Apr 22 '18 at 12:54
  • But this does not throw any exceptions - only changing the text property of the text field does. – LaTeXEnthusiast Apr 22 '18 at 12:57
  • 1
    Not all coding errors result in exceptions. The documentation clearly states in many places (e.g. [here](https://docs.oracle.com/javase/9/docs/api/javafx/application/Application.html), under "Threading") that modifications to the anything in the scene graph must happen on the FX Application Thread. – James_D Apr 22 '18 at 13:01
  • 1
    Put your `while` loop *inside* your `try` block, so an interrupt will cause it to gracefully exit. An interrupt is an explicit request by other code for your thread to terminate; ignoring it makes your thread a rogue thread that effectively cannot be halted. – VGR Apr 22 '18 at 13:04
  • Consider using the [Animation API](https://docs.oracle.com/javase/9/docs/api/javafx/animation/package-summary.html), e.g. an [`AnimationTimer`](https://docs.oracle.com/javase/9/docs/api/javafx/animation/AnimationTimer.html), to avoid using threads entirely for functionality like this. – James_D Apr 22 '18 at 13:07
  • I have done as you advised me, @James_D and it works perfectly well, thank you! Besides, I do not think that this question is a pure duplicate, nevertheless, such answer as your - about AnimationTimer does not appear there, – LaTeXEnthusiast Apr 22 '18 at 13:31

0 Answers0