JavaFX Series - Hover-Panes


Posted by Steven

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.


Get the executable jar here.

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:

  1. Node n1 = getN1();
  2. Node n2 = getN2();
  3. Node n3 = getN3();
  4.  
  5. rootGroup().getChildren().add(n1);
  6. rootGroup().getChildren().add(n2);
  7. rootGroup().getChildren().add(n3);

... or I simply use the varargs-method:

  1. Node n1 = getN1();
  2. Node n2 = getN2();
  3. Node n3 = getN3();
  4.  
  5. rootGroup().getChildren().addAll(n1, n2, n3);

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:

  1. this.addEventFilter(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
  2. @Override
  3. public void handle(MouseEvent event) {
  4. // this is just the red circle marking the radius for this spawner
  5. JavaFxApplication.getInstance().getRootGroup().getChildren().add(radiusCircle);
  6.  
  7. // mouseOverPane is an instance variable
  8. if (mouseOverPane == null)
  9. mouseOverPane = new MouseOverPane(thisResourceSpawnerFX);
  10.  
  11. // Possible that the pane is already added because the
  12. // mouse hovered over this spawner and the pane didn't
  13. // get removed after the mouse left.
  14. if(!JavaFxApplication.getInstance().getRootGroup().getChildren().contains(mouseOverPane)) {
  15. JavaFxApplication.getInstance().getRootGroup().getChildren().add(mouseOverPane);
  16. }
  17.  
  18. // after instanciating the pane, there shall be a nice animation opening it
  19. mouseOverPane.getAnimationAppear().playFromStart();
  20. }
  21. });

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:

  1. addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
  2.  
  3. @Override
  4. public void handle(MouseEvent event) {
  5. // first let the animation finish
  6. animationDisappear.setOnFinished(new EventHandler<ActionEvent>() {
  7.  
  8. @Override
  9. public void handle(ActionEvent event) {
  10. // remove the pane from the root group of nodes
  11. JavaFxApplication.getInstance().getRootGroup().getChildren().remove(createdPane);
  12. }
  13. });
  14.  
  15. JavaFxApplication.getInstance().getRootGroup().getChildren().remove(resourceSpawnerFX.getRadiusCircle());
  16.  
  17. animationDisappear.play();
  18. }
  19. });

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:

  1. public class ResourceFX extends Polygon {
  2.  
  3. private Resource representedResource;
  4.  
  5. public ResourceFX(final Resource representedRessource) {
  6.  
  7. // these numnbers represent the polygon that shapes the resource
  8. super(new double[] { 0, 0, 10, 10, 20, 0, 20, 10, 30, 20, 20, 20, 20,
  9. 30, 10, 30, 0, 20, 10, 20 });
  10.  
  11. this.representedResource = representedRessource;
  12.  
  13. this.setFill(Color.CORAL);
  14. setLayoutX(representedRessource.getPosition().getX());
  15. setLayoutY(representedRessource.getPosition().getY());
  16.  
  17. FadeTransition ft = new FadeTransition(Duration.millis(3000), this);
  18. ft.setFromValue(0.1);
  19. ft.setToValue(1.0);
  20. ft.play();
  21.  
  22. ScaleTransition st = new ScaleTransition(Duration.millis(3000), this);
  23. st.setFromX(0);
  24. st.setToX(1);
  25. st.setFromY(0);
  26. st.setToY(1);
  27. st.play();
  28. }
  29. }

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?

Category: 
Share: