The API for
Node clearly states that one should not modify a live Node outside of the JavaFX Application
Thread:
Node objects may be constructed and modified on any thread as long they are not yet attached to a Scene. An application must attach nodes to a Scene, and modify nodes that are already attached to a Scene, on the JavaFX Application Thread.
I came across this rule via a closely related forum question. Is this rule overly conservative? I've not managed to find any discussion on the matter. It seems to me there are cases where one can modify a property of a Node subclass with no repercussions. One example that seems perfectly fine is to modify the opacity value of a Group--I've been doing this, with no problems, from an AnimationTimer thread.
The project where I am doing it is described on this
blog post. The jar file for the post is
here.
My AnimationTime is simple:
I refer to a Group:
TitleBlocks that is a live Node, in an update() method with the code line:
The above is working fine, so far.
Here's an example of a way to schedule commands on the FX Application thread:
I worry that for more involved graphics, the FX Application thread could become overwhelmed if I every instance of an opacity value change (and other changes, such as icon and sprite movements) all have to be scheduled this way.
My guess is that for certain types of properties, the code in the JavaFX Application Thread that handles the Nodes only does a read of the current value, and if the property value is changed after the fact, this is simply ignored until the next iteration. But without having this practice sanctioned (such as with the fades I employ on my game-to-be), I'm nervous about leaving my code as is.
Following is another example to consider (the one that inspired me to try writing a game with JavaFX). This particle generator application modifies 20,000 objects at the rate of 60 fps. Each object is a subclass of Node when you trace the parentage. the modifications are being done on the AnimationTimer thread, not the FX Application thread. Why doesn't this program exhibit any problems? Doesn't it violate the rule about only modifying nodes via the FX Application thread? Or am I misreading the code?
Blogpost (describes code, has video and link to code):
http://wecode4fun.blogspot.co.at/2015/07/particles.html
Code (so you can jump to it directly, if desired):
https://gist.github.com/Roland09/71ef45f14d0ec2a353e6