I feel that the drawing performance of JavaFX 2 is worse than those of Swing. I think one of the reasons is how you draw things, e.g. when drawing a Line, one has to create a Line object for each line and add it to some container while in Swing one just gets the Graphics2D once and then does drawLine without creating a new object each time. So, is there any more performant way to draw using JavaFX 2, e.g. if you have to draw about 3000 Arcs? What's the best "drawing surface"? Currently I am using a Group. Thanks for any hint!
Your current approach of using a Group, is likely fine, and you will probably find that the performance of drawing 3000 arcs within your Group is perfectly acceptable for your application users.
A current alternative to using a Group would be to use layout Panes which would add some convenient functionality, but add an extra layer of overhead, which is unnecessary when you have thousands of easily manually laid out objects.
See the JavaFX mailing list archive for a discussion of an upcoming direct draw interface (called the Canvas Node) for JavaFX 2.2 and it's implications.
Although it may seem less performant to stick objects in a container, the container implementation can, if it wishes, make use of accelerated retained mode rendering hardware built into modern gpus. Also note that the JavaFX architecture internally keeps track of dirty areas and caches the results of expensive rendering operations to increase performance. Therefore, the use of a container does not, in and of itself, imply slower performance than a direct draw interface which might rely on an immediate mode implementation.
Choosing a drawing surface will depend on the application. The best surface for most JavaFX apps will be a collection of scenegraph nodes rather than a canvas node. Using many nodes in a scenegraph rather than a single Canvas node will (usually) be easier to develop for and the performance will be perfectly acceptable.
Eventually, it is likely that numerous blog articles will be written to compare performance of Canvas vs many scenegraph objects and perhaps against other frameworks such as html canvas, java2d, swing, etc. So in time, it will be easier to answer such a question.
Related
Osvaldo's JavaFX performance analysis, though old, also includes a in depth discussion of some of the issues raised in your question.
There is a nice section on Performance Tips and Tricks in the openjfx wiki.
There are some related StackOverflow JavaFX performance questions:
In JavaFX 2.2 an object similar to HTML5 Canvas
is planned to be added which will ideally fit you need of drawing a lot of primitevs.
See docs attached to next issue for details: http://javafx-jira.kenai.com/browse/RT-17407
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With