jfx1

JavaFX Tutorial – Make a Window

Make a Program – JavaFX Tutorial: Make a Window

 

For instructions on setting up Eclipse (our IDE), click here.

Start by making a new Java project in Eclipse by using File -> New -> Java Project.  After that, create a new class named Main and open it.  Create the entry point to our program as such:

Next we need to declare two variables, a BorderPane and a Scene:

In order to access these class types, we’ll need to make our Main class extend Application, which is a JavaFX interface.  We should now have a class like this:

Next we can import the Application, BorderPane and Scene resources by hovering over them and selecting the import option, or by hitting Ctrl+Shift+O.  If you are unable to import these classes, it’s possible that your version of Java requires a custom access modifier in order to use JavaFX.  Please check the video embedded above for instructions on how to enable that.

Main will now ask you to implement the start() method required by the Application interface.  Go ahead and do that and then initialize the scene and layout variables within the method.  The Scene() arguments are a layout instance, window width and window height.

Add the following line to your main() method in order to initialize the JavaFX calls.

Finally, at the bottom of the start() method, you will need to set the scene of the passed Stage with .setScene(scene) and then make a call to window.show() at the bottom of the method.

 

If you found this tutorial helpful, consider showing your support here.

Current Patreon supporters at the Source Code tier and up can see the entire source code right below this line: (Login with your Patreon credentials here)

Power Towers v0.69

Changes

  • Added functionality within the StateManager class to track how many frames per second (FPS) our game is running at.

To-Do

  • Implement dynamic on-screen text to track FPS without having to check the console
  • Implement a way to show/hide diagnostic data while in-game

Known Bugs

  • There is no way to unselect a tower other than paying for it and placing it on a valid tile

Changes — Extended

FPS Counter:

The following static variables were declared and initialized at the top of our StateManager class:

We then used these variables within our update() method (which is called each update by the Boot class) to track how many frames pass each second and print the result to the console.