JavaFX 2 path drawing performance

2019-07-01 14:52发布

I have a JavaFX Group with a Path Node added to it, to which I add data approximately 30 times per second. This causes my whole GUI to become very laggy and unresponsive after about a minute. First, I add the path to the Group like this:

root.getChildren().add(path);

Data is added like this:

while(true) {
    // Calculate x and y...

    path.getElements().add(new LineTo(x, y));
    path.getElements().add(new MoveTo(x, y));

    // Sleep 33 milliseconds...
}

If I do not add the path to the group, but still add data afterwards, the GUI remains responsive, so the performance issue seems to be when drawing the path's shape. Why? What can I do to improve the performance? Is this known to happen or am I doing something wrong? Thanks!

1条回答
淡お忘
2楼-- · 2019-07-01 15:27

There is a known issue (creating paths is very slow) in JavaFX 2.1 related to Path performance and another, similar issue unresolved in JavaFX 2.2 (improve path rendering performance). There may be other issues if you check the JavaFX issue tracker system. For JavaFX8, which is currently under development, the performance of many elements has been vastly improved.

One alternate approach you may try is to use a Canvas rather than a Path. Depends on your use case though, for some use cases, this will be a suitable substitution, for others it won't.

If you can create a short reproducible test case and file a JavaFX issue for it, the JavaFX team will investigate any performance issues you are seeing and potentially address them if they are caused by the underlying system implementation.

You should also check your implementation for the following things:

  1. Do not call sleep on the JavaFX thread.
  2. Don't do anything processor intensive on the JavaFX thread.
  3. Don't perform blocking I/O on the JavaFX thread.
  4. Don't place tens of thousands of nodes in the SceneGraph or a Path.
  5. When reading or writing objects in the SceneGraph from another thread, use Platform.runLater.
  6. Don't call Platform.runLater too often or you will end up overloading the event handling system.

Not saying your code has any of the above issues, just things to check.

查看更多
登录 后发表回答