Getting Started with JavaFX
By: Phillip Bailey
Tags: Easy, Fun, Javafx, Java, Programming

Maybe you are new to programming and want to start making stunning projects! Perhaps, you have been coding for years and want to make fun side projects. Well, with JavaFX you can enhance your practical knowledge of Object Oriented programming while having a blast!

Before we dive into a brief example, we need to make sure we have all we need to get started. Below is a list of prerequisites for our short project.

Java. An IDE (Eclipse, NetBeans, etc.) JavaFX jar files. A little knowledge of classes and objects in modern programming languages.

Once you have those, you are all set! JavaFX is a tool that allows us to build GUI(Graphical User Interface) applications with Java. It is very simple to pick up and can be quite fun to use. Now, let’s start our project.

Open your IDE. (I am using Eclipse, but the process should be similar on most Java IDEs.) Choose a workplace location on your computer. Create a new Java project with the name SolarSystem or anything that you like. In your project, select the src package. In that package create a Java class called “JavaFXDemo”. Next, go to the project tab at the top of your screen and select properties. Choose the build path option and paste the jar’s from your JavaFX download into the classpath. Finally, add the JRE needed for running JavaFX applications. Go to the Run tab -> run configurations -> arguments. Then add the following line to the VM arguments entry box: “ — module-path (path to javafx 11 lib folder)— add-modules=javafx.controls” (View the link below from StackOverflow for more help with this issue).

https://stackoverflow.com/questions/51478675/error-javafx-runtime-components-are-missing-and-are-required-to-run-this-appli

Now, we have a Java class that will serve as the code that builds our mini solar system. Let’s import the needed classes with the following statements. (To make this easier to follow, the all of the instructions will be in bold for the rest of this article and the code will be in regular font).

import javafx.animation.PathTransition;

import javafx.animation.Timeline;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.layout.Pane;

import javafx.scene.layout.StackPane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.stage.Stage;

Great, now we need to make sure that our JavaFXDemo class extends Application and that we override its start method.

@Override

public void start(Stage arg0) throws Exception {

// TODO Auto-generated method stub

//Create a pane.

StackPane pane = new StackPane();

Then we will add a StackPane object that will hold our model solar system. After that, we can add three Circle objects that will represent the sun, earth, and earth’s orbit.

//Create two Circle objects for the sun and earth.

Circle sun = new Circle();

Circle earth = new Circle();

//Create one more Circle earth’s path of travel.

Circle earthOrbit = new Circle();

We can now customize these circle’s with methods such as setRadius() and setFill(). We can also make a PathTransition object that we can use to place our earth circle onto the circle for its orbit. We can customize the PathTransition with methods like setOrientation() and setRate().

//Set the radius of the Sun, earth, and earth’s orbit.

sun.setRadius(50.0);

sun.setFill(Color.YELLOW);

earthOrbit.setRadius(100.0);

earthOrbit.setFill(Color.BLACK);

earth.setRadius(10.0);

earth.setFill(Color.BLUE);

//Make a PathTransition object for the earth and establish some attributes.

PathTransition pathTransition = new PathTransition();

pathTransition.setPath(earthOrbit);

pathTransition.setNode(earth);

pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);

pathTransition.setRate(0.2);

pathTransition.setAutoReverse(false);

pathTransition.setCycleCount(Timeline.INDEFINITE);

Now we just need to use the play method to start our PathTransition object and add all of our Circles onto our pane. Finally, we’ll add our pane object to a Scene object and that Scene object to our Stage parameter. Once we call the stage argument’s show method it will become visible.

pathTransition.play();

//Finally, add the nodes onto the pane.

pane.getChildren().add(earthOrbit);

pane.getChildren().add(earth);

pane.getChildren().add(sun);

//Make a Scene object and use the Pane object as its argument.

Scene scene = new Scene(pane,100,100);

arg0.setScene(scene);

arg0.show();

}

To finish our application, we must create a main method and use Application.launch() to start our app.

public static void main(String[] args) {

Application.launch(args);

}

}

That’s all! You now have all the knowledge you need to create a model of our solar system in JavaFX. If you want to make your project more appealing, try experimenting with more of the Circle objects’ methods. Also, feel free to look at the GitHub repository at the end of this article for more ideas, or view OpenJDK’s website. Have fun coding!

GitHub Solar System Repository : https://github.com/pbaileydev/SolarSystem