Alakanu
Alakanu

Reputation: 325

Java: GUI to controller thread communication

I'm building a game using Java for a university project. The game had to use a textual interface at first, and only later we had to add a GUI.

I have a thread running the game logic and updating the game model, and a separate thread, the one with the main function, having the JFrame instantiated on, where I then switch panels according to the game state (the controller tells the gui which frame to display with a setPage method).

Getting the user input, however, is what is driving me crazy now. With the textual interface, all I had to do to get the user input was to call a method, something like getPlayerNum, and after the user put in the number and pressed enter I would have the input back to the controller. All the game logic is implemented in a game loop, asking for input when it needs it and notifying the output to the view.

How can I emulate a similar behavior now that I'm forced to use a event-based gui? How can I block the method call until the user clicks a button, and then have the method return the correct value?

Upvotes: 0

Views: 205

Answers (1)

trashgod
trashgod

Reputation: 205835

Kudos for developing an independent game model. As a prelude to designing your GUI, start with this very simple game. Add a TextView that listens to the model and reports on game progress. This will help you see how the GUI works, and it may help you decide what additional features you may need in your model.

Upvotes: 1

Related Questions