JavaFX Particle-o-Rama Thoughts

Update: I have put up a new version of Particle-o-rama (still using my refactored code), but instead of applying effects to each individual partical, I apply the same effect to the group responsponsible for all particles. This, on my computer, is far more performant than Josh’s version, but looks the same (or very similar). </Update>

If you’re reading this blog, you’re probably well aware of Josh Marinacci’s Particle-o-rama project that he put out recently. It’s a simple particle simulator designed to put JavaFX through it’s paces (by throwing lots of independent nodes into the scenegraph). When it came out I was not very impressed by the speed of the animation, so I decided to look into it today. I should note that the video on the page linked to above shows it to be very performant on Josh’s Mac, so I’m not entirely sure why the same application doesn’t work well on my (equally powerful) Vista machine.

I ended up doing a decent refactoring of the code (which is what I tend to do to understand what is going on), but it turns out that the key issue is the use of JavaFX effects to blur the particles. By simply disabling three lines of code, the performance jumps considerably (on my machine a rough guide is by about 400%). This improvement also includes my other refactorings that would have improved the performance a little, but not anywhere near as much as the result of turning off effects.

You can compare my version to Josh’s version.

So, in summary, effects in JavaFX are still in some circumstances slow. If you are experiencing slow-downs in your code, try disabling any effects you have enabled and see if that helps.

Even with the improved speed of my version, it maxes out and starts to become laggy after about 400 nodes are visible. This is obviously not good enough, but it sounds like Sun are putting a lot of effort into improving performance in the next few releases of JavaFX. It will be good to keep testing performance in relation to both my version and Josh’s version of this particle simulator.

For those of you interested in how I cleaned up / refactored Josh’s code, you can see my version here. I kept the same general gist of Josh’s version, but binned a lot of unnecessary code, merged loops where possible, and extracted comparisons to be performed outside of loops where possible. Josh’s original code can be seen here.

Java desktop links of the week, July 19

Well, no surprises this week with the news that the Oracle acquisition of Sun has been approved by shareholders. With the next step being Government consideration, we can only wonder how much longer Sun will be around. All I hope is that Oracle does it’s best to continue developing relevant Java technologies, and keeps Java pretty much on the path it is on now.

This week has been oddly quiet in the Java desktop world. Regardless, here are the most important Java desktop links of the week.

P.S. For anybody out there interested in hearing more Java desktop news, remember to follow me on twitter.





That’s us for another week. As always, feel free to email me any links you think are relevant. Have a great week!

Java desktop links of the week, July 13

Here is another weeks fresh Java desktop links, served steaming hot straight to you – just the way I know you like it.





  • Andres Almiray keeps his blog on all things Groovy churning out the hits, this week with two on GfxBuilder. The first post is about custom nodes, and the second on transforms and plugins.

That’s it for another week. Keep up the good work everyone, but for those of you up in the northern hemisphere, get outside and enjoy the sunshine 🙂

Java desktop links of the week, July 6

Well, I’m glad I got around to learning JavaFX as the quantity of news is hugely sliding towards JavaFX these days. This week is no different. Despite this, if you have any Java desktop related news, please feel free to email me. Lots of people do, and it makes my life a little easier 🙂 You can find my email to the right on every page. Here we go again – enjoy.



  • Ken Orr lets everyone know that a co-worker of his, Jared MacDonald, spoke at JavaOne on building bullet proof user interfaces using test-driven development. The slides (in pdf form) are available for download.
  • Mariusz Trzaska emailed me to let me know of a project he has been working on called gcl-dsl, which stands for ‘GUI Creating Language Domain Specific Language’. As suggested, it is designed to make it easier to build user interfaces in Swing.
  • Alexander Potochkin (from the Swing Team at Sun) emailed me to let me know of CollectionSpy, which is a commercial application designed to profile Java applications which make use of collections. It is also written in Swing. An observation I’d like to make is that developers who write profiling software are very good at making good looking applications. Look at software such as JProfiler and YourKit.



That’s another week down. Have a great week everyone!

JavaFX: Building custom controls

One of the big criticisms leveled at JavaFX presently is its lack of controls (or components if you come from the Swing world). This was addressed to some degree in JavaFX 1.2, but there are still many controls missing, and some of the controls in the latest release have relatively basic API’s at this point. Certainly this will improve in future releases, but I thought I’d try to simply explain the process you need to go through to build your own controls. Who knows, you might be able to sell them if you get them good enough.

In JavaFX, there are three classes that you should become familiar with: Control, Skin and Behavior. Simply put, the Control class should be used to maintain state. It can have it’s look (but not feel) modified by assigning a different Skin instance to the skin variable. To separate the look of the control from how a user interacts with it, you should put all user interaction code into a Behavior instance, and assign it to the behavior variable within Skin.

I’ll jump right into a demo now. The demo is of a button (this term is used very, very loosely as I wanted to keep the code simple), and is split between three classes, as well as a test script to use the new button control. I have neglected to include any package and import statements, just to keep the code concise.

Firstly, the control itself simply has a text variable to store the buttons text. It also creates and sets an instance of the new skin, which handles the painting.

Secondly, we have the skin class shown below. It is necessary to implement the contains and intersects methods, but you can normally just reuse the code I have below for that. Note that I can access the control variable directly, but I cast it to be an instance of JGButton so I can access the text variable. Note also that I override the behavior variable to use my own implementation. Finally, you should note that I create a Group of nodes to represent my button, and I assign this to the node variable. I attach a few mouse listener functions to this node, allowing me to palm off the functionality to the behavior class. This allows me to change the skin without worrying about the behavior code needing to be needlessly duplicated to all skins.

Thirdly, we have the simple behavior class, which is where I have put in functions related to the buttons behavior. Oddly, the Behavior class offered by JavaFX only currently supports keyboard input, so it is necessary to write mouse input events from scratch. I’m not sure why this is, but perhaps Richard might leave a comment related to this. The only thing to note in this class is that I can access the skin and control through the skin and skin.control variables respectively.

Finally, we have a very simple test script, with nothing really worth noting. When the application starts, we have a red ‘button’ with the text “Button Text” printed atop it. When the mouse clicks on it, “Button pressed” is printed to the console. When the mouse moves over it, the text of the button changes to “Button entered”, and when it moves away from the button, the text changes to “Button exited”. Yes, it’s a very useful button.

I hope this quick and simple tutorial on creating custom controls in JavaFX has been useful. I tried to keep everything as simple as possible, but if you have any more detailed questions, please leave a comment or email me. Cheers!