0

My problem is that I need:

  • One thread to receive UDP data containing cartesian coordinates.
  • JavaFX thread (function?!) to print these coordinates on the scene.

So I continuously receive the data from the UDP Socket and I save the data in a BlockingQueue. The queue is common to JavaFX thread and UDP_read thread. Now, I need a JavaFX scene (function?!) that continuously takes the data from the queue and print (render/ illustrate) these coordinates (with rectangles or other shapes) on the scene, but I can't use a While-loop in a JavaFX thread.

I've tried with the Task class but it seems to be useful only when the task is limited in time and not continuous.

Any help is appretiated

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • [InvokeLater](https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable))? – Neijwiert Mar 04 '16 at 10:52
  • doesn't work with a while loop – franktiello Mar 04 '16 at 12:59
  • How is that not working with a while loop? – Neijwiert Mar 04 '16 at 13:28
  • if i put a while loop inside invoke later: Platform.runLater(new Runnable() { @Override public void run() { while(true){....print coordinate....} the main gui freeze – franktiello Mar 04 '16 at 14:23
  • `InvokeLater` is for Swing, not JavaFX, so it is not applicable here. However [Platform.runLater](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-) is essentially equivalent and should work (though you should not place `while(true)` inside the content of a runLater call). Perhaps you might want to supply an [mcve](http://stackoverflow.com/help/mcve). – jewelsea Mar 04 '16 at 18:40
  • You can achieve what you want by using the techniques in [Most efficient way to log messages to JavaFX TextArea via threads with simple custom logging frameworks](http://stackoverflow.com/questions/24116858/most-efficient-way-to-log-messages-to-javafx-textarea-via-threads-with-simple-cu), though a solution along those lines is almost certainly overkill for your task and you can likely accomplish what you need in a simpler manner. – jewelsea Mar 04 '16 at 18:45

1 Answers1

0

From what you wrote, I can assume the following:

  1. You're receiving coordinates data from some other thread OR over the network to your application.
  2. You want to illustrate the coordinates data on a JavaFX application window.

I am well aware that there might be better solutions out there, however, I would solve in the following manner:

  1. Have global variables on your JavaFX class. These variables are used to store and get the coordinates data from.
  2. Have an inner class in the JavaFX class that listens/ receives the UDP data. In this class, you will run the thread that listens to your UDP connection and you store that coordinates data inside the global variables from step 1.
  3. Use the AnimationTimer class to be able to translate/ update the shapes that you want to be illustrated onto your application window.

It's IMPORTANT to have synchronization blocks around the global variables so that you don't face data inconsistencies. Hopefully, this is of some help.

A. Yassin
  • 75
  • 1
  • 2
  • 10