I wanted to let the player rename the character of my game. So I thought that making a text box and when he clicks the ok button, the text inside the text box will make it to a string and render it on the screen. How do I let the user type their own text?
You have reached the point where you need to use a GUI (for graphical user interface) in your game. Sometimes, in games, you will only need buttons or similar simple components. In this case, i would recommend doing it yourself, a button is something really simple to do.
Considering a button is a clickable image, using Slick2D, a button could be something like that:
And then add a kind of listeners system that would listen for mouse events (look at the Slick2D API to learn about catching mouse events) and proceed the registered buttons to see if one of them was clicked.
Then a button could be defined like that:
And in your GUI logic:
The above shows that you can easily implement simple GUI components without any external tools, a button or a check box for example. However, if you think you will need more complex components like textfields, combo box, panels, draggable windows etc. i highly recommend using an already existing GUI system and use it in your game.
As far as i know, with Slick2D, you have to major choices:
Both are made in Java using LWJGL and Slick2D implementations are available. For example, NiftyGUI provide a custom implementation of the
The choice of TWL or NiftyGUI is a subjective question, a lot of people on the Slick2D forums will recommend using TWL since it seems like the Slick2D support is a bit more mature than with Nifty. I personally use Nifty.
In conclusion, you will have set your requirements and then choose between doing it yourself or using an existing GUI system.
If you decide to create your own GUI system, Slick2D seems to provide some tools to help the process:
I haven't use slick2d but I know it's an engine based on lwjgl. It also seems to have a rudimentary GUI system. Searching through the API, I found the TextField class, which belongs to the gui package. You should search for tutorials about using the gui system in slick2d, though. I don't have enough experience to help you further.