Skip to content
Advertisement

Performance of JavaFx Gui vs Swing

I wrote two simple programs, both draw the same Sierpinski Triangle: enter image description here
One program was implemented using swing, and one using javafx. There is a very significant performance difference, swing implementation being consistently much faster:

enter image description here


(In this test case : Swing over 1 sec. Javafx over 12 seconds)
Is it to be expected or is there something very wrong with my javafx implementation ?

Swing implementation

JavaScript

JavaFx implementation

JavaScript

Advertisement

Answer

The Swing example flattens the image to 6002 = 360,000 pixels. In contrast, the JavaFX example strokes almost 2.4 million overlapping polygons when finally rendered. Note that your JavaFX example measures both the time to compose the fractal and the time to render it in the scene graph.

If you want to preserve the strokes comprising the fractal, compose the result in a Canvas, as shown here.

If a flat Image is sufficient, compose the result in a BufferedImage, convert it to a JavaFX Image, and display it in an ImageView, as shown below. The JavaFX result is over a second faster than the Swing example on my hardware.

Because SwingFXUtils.toFXImage makes a copy, a background Task<Image> could continue to update a single BufferedImage while publishing interim Image results via updateValue(). Alternatively, this MandelbrotSet features a Task that updates a WritableImage via its PixelWriter.

image

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement