0

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?

3
  • 1
    You need to completely re-think your program structure so that it is not linear but rather state and event driven. Don't "block" methods but rather vary your program's response to events depending on the model's state. Commented Jun 1, 2013 at 16:46
  • 1
    For a more detailed answer, consider asking a more concrete question, complete with a small bit of compilable and runnable example code. Commented Jun 1, 2013 at 17:47
  • 1
    Thanks for your answer. Building the textual interface was suggested by the teacher as "it will lead you on the right way to build the game cycle", what he "forgot" to mention is that we were meant to separate requests to and answers from the user. It took me a while to split everything up, but now it's up and -kinda- running. Commented Jun 6, 2013 at 9:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the sample you posted, it's a good example I wish I saw way before than just today..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.