Issue
I am making a simulation within JavaFX using the scene graph, (not canvas) and am having issues drawing only what I need on the screen.
This simulation has 10 million+ nodes within it, but the user only needs to see a small fraction of those nodes at the same time on screen (max 160,000 nodes). All the nodes that I am concerned about are 400x400 ImageViews
.
Each node is a member of a Group
(node chunk) that holds roughly 40,000 nodes, so 4 or less of these 'node chunks' need to be displayed at a time. For these 'node chunks' to be displayed they are added onto a static Pane
and that pane is in the root node, which is a Group
.
So my graph from the first parent to the last child looks like so:
Root node Group
\ Display Pane
\ (many) Node ChunksGroup
\<= 40,000 ImageViews
Since the display pane is constantly moving around (panning and rescaling) based off of user input, and there are so many nodes, the application doesn't run at the speed I would like it. It makes sense that JavaFX has trouble keeping track of 10 million+ nodes at the same time, so my solution has been to remove all 'node chunks' from the display pane; saving them in a hash map until I need them to be drawn.
Each 'node chunk' has had its LayoutX
and LayoutY
s set to be uniformly distributed across the display pane in a grid like so:
In this example I would need to grab and display 'node chunk' 7, 8, 12, and 13 since that is what the user is seeing.
Here is a screenshot with 'node chunk' 0 manually added. The greenish yellow color is where 'node chunks' 1, 5, and 6 would be placed.
My issue is: Since the 'node chunks' are not added into the display pane until they are needed, I cannot reference their layout bounds with respect to the constantly changing section of the display pane that users are seeing, so I do not know which 'node chunks' need to be displayed.
Is there an easy way to solve this? Or am I down the wrong path? (or both) Thanks.
Solution
I ended up creating a rectangle for each 'node chunk', set the opacity to zero, and then checked for collision with the section of the display pane that the user is seeing. When a collision with a rectangle occurred, the corresponding 'node chunk' is then added to the display pane.
Answered By - NotZack