codingIsFun
codingIsFun

Reputation: 91

Seperate JavaFX GUI and Logic

I'm creating a game and thought of using JavaFx. What I want to do is from the main-class call a render-method repeatedly in the GUI-class (which contains all the javafx stuff start, scenes etc) and at the same time handle the game logic (collisiondetection etc) in the main-class. The problem is I'm getting a bunch of errors and it seems like they want the GUI-class to be the center of the application. So what I wonder is is my approach wrong?

Upvotes: 2

Views: 2161

Answers (1)

Zircon
Zircon

Reputation: 4707

JavaFX's main thread is the "application thread", which performs GUI functionality, and there is no circumventing this.

It is best to make your "GUI-class" contain the start and main methods as JavaFX requires, and then after creating your GUI objects, start a new thread that starts the entry point for your application logic. Even if, for some reason, you don't want to show the GUI when the app starts, you can call a method that performs primaryStage.show() for you after you've done other business logic.

You could still have your application logic call the GUI's render method every so often, and you could even make that yet another thread, or just use a timer. I don't think this is the paradigm, though. In any case you should use Platform.runLater() to make changes to your GUI, or it will end up hanging.

Upvotes: 1

Related Questions