Mastodon

JavaFX Series - Exit Application and Animated Harvesting

This week, I got around to adding some animation into the game and fixing a (again) thread-related bug.

Exiting the Application

After playing around with the application, I noticed a problem: After a couple of executions from within eclipse, my computer got pretty slow. It turned out that after closing the application, one Java thread always stayed alive. The reason for this is my architecture. In my starting class, I called the JavaFX application as follows:

public class Start {

    public static void main(String[] args) {
    
          // If there were other user interfaces, here would be the decision which
          // of them to start. Right now, there is only JavaFX.
          JavaFxApplication.main(new String[0]);
    }
}

The JavaFxApplication was as follows:

public class JavaFxApplication extends Application implements UserInterface {

    // ...
    
    public static void main(String[] args) {
        launch(args);
    }
    
    // ...
}

As you can see, the JavaFX application started in a new thread. When the user closes the (JavaFX) window, this thread stopped. But because the first thread that called the FX-thread never did something else as to start its counterpart, it was never exited. This way, it went on and on. To make it stop after the user closed the JavaFX window, I changed it as follows:

public class Start {
    
    public static void main(String[] args) throws InterruptedException {
    
          // If there were other user interfaces, here would be the decision which
          // of them to start. Right now, there is only JavaFX.
          Thread t = new Thread() {
             @Override
             public void run() {
                System.out.println("Starting JavaFX ...");
                JavaFxApplication.main(new String[0]);
                System.out.println("JavaFX ended");
             }
          };
     
          t.start();
          t.join();
          System.out.println("terminating or closing java program");
          System.exit(1);
    }
}

Now, the first (purely Java) thread starts the JavaFX thread and joins it. This means that only after the JavaFX thread finishes, the originaly thread continues. This gives me the chance to stop it in a proper way.

Animations!

I know that animations are a huge topic in JavaFX and there are many tutorials out there which show pretty impressing stuff. However, I programmed my own little animation for the harvesting colonies. Oh right: There are colonies in the game now! I’m not sure what they will do, but they sure will need some resources to do anything. Hence, I made them harvest existing resources:

SimFX Harvesting Beam

The ultimate resource-gathering red laser beam is made as follows:

Colony c = (Colony) harvester;
Resource r = (Resource) harvest;

final Line line = new Line(c.getPosition().getX(), c
    .getPosition().getY(), r.getPosition().getX(),
    r.getPosition().getY());
line.setFill(null);

line.setStroke(Color.RED);

line.setStrokeWidth(2);

rootGroup.getChildren().add(line);

FadeTransition ft = new FadeTransition(Duration
    .millis(1300), line);
ft.setFromValue(1.0);
ft.setToValue(0);
ft.play();

// remove the line after the animation
ft.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        rootGroup.getChildren().remove(line);
    }
});

There are a few articles out there that describe how to remove a node from the group after its animation went through. However, the most articles I’ve seen didn’t work for JavaFX 2.2.

Get the Code

As always, I pushed the code in my repository on github. The codebase for this article is tagged as Codebase_ExitApplicationAndAnimation.

Next Steps

Because only having one colony is not very awesome, colonies will be able to expand soon. Most likely, they will be able to spawn a new colony if they have enough energy.