This article is part of my JavaFX Series. Get the preceding articles here.
After setting up the basic world in which the simulation takes place, I decided to tweak the user interface a little bit. As you can see below, there are context menus at the resource spawners that allow for manipulation of the radius in which new resources are being spawned. The new objects now grow into existence by using a FadeTransition and a ScaleTransition.
Using JavaFX
After using the framework for a couple of hours, I’m positively suprised. JavaFX has a well designed API that allows for intuitive programming. Often I find myself thinking “He, I want to do X. There should be a method at this object here - great, there it is!”. Also ,the use of varargs minimizes boiler plate code. For example, to add some more objects to the root group of objects, I could either do this:
… or I simply use the varargs-method:
Of course, diving into a new framework without having read a single tutorial has its downsides. I wanted to add a listener to the resource spawners to listen for a mouse over event. I found the known method addEventHandler and a new method addEventFilter According to my namesake Steven Yang, the difference between them is that filters are executed before handlers. A small, but important detail.
Context Menus
The two resource spawners now have a context menu that allows for setting the radius in which new resources are being spawned. You can see one context menu at the picture above. Adding them is very similar to adding listeners for Swing components:
The pane itself is a subclass of TitledPane and provides two ScaleTransitions for the appearance and the disappearance of the pane. The first is called when opening the pane, see code above, the second is called from within the pane itself:
Transitions
Not only the context menu has a nice animation, also the resources now fade into the world and grow. This is done by a FadeTransition and a ScaleTransition:
Once again, I find JavaFX code quite intuitive.
Get the Code
You can get the code in my repository on github. The codebase for this article is tagged as Codebase_HoverPanes.
Next Steps
Having had first contact with forms, I wonder if larger panes with more form components handle as well as the small ones did. Because most of the enterprise applications are heavily form-based, handling them in a good way is essential for becoming widly accepted. On the other hand, transitions are also easy to handle which makes me think if I should go on introducing the bases / cities / settlements of the culture that harvests the resources. They could send little harvesters to get the resources. Or should they beam them into their base? What do you think?