Hi!
I am a newbie in slick.
I have a problem in putting textfield/text area in slick.
It’s like I have to put a name of a character in the game once clicking the button.
Thanks! :persecutioncomplex:
Hi!
I am a newbie in slick.
I have a problem in putting textfield/text area in slick.
It’s like I have to put a name of a character in the game once clicking the button.
Thanks! :persecutioncomplex:
What exactly do you think is your problem with slick that prevents your from showing the character name on the screen?
Do you have a code example? Do you have any error messages? What is your current concept to implement the desired feature? Or don’t you know where to start?
Permafrostrocks
It’s not about showing the character name on screen, but rather giving the character a name, by inputting text/string in a text area/ text field (probably a textbox where i can type in the name).
and i don’t where to start (textfields).
But i have already started some of the GUI.
What parts of your GUI have you started? If you’re not too far in, you can always try one of the existing OpenGL based GUI libraries. Many of them work with Slick2D without issue. If you’re rolling your controls from scratch, then you basically need to have a label that can receive key input when it’s focused. How you accomplish that largely depends on the way you’ve constructed your control hierarchy.
Actually, what i have started is just putting an image which serves as the bg of the application.
then i put buttons that will bring you to a new screen (with image as the bg).
those are things that i had started. And i don’t know where to start
If you’re rolling your own controls, a good starting point would be to create a base class to hold the common variables that all of your controls will have such as dimensions, visibility, rendering offsets, etc. add an empty render method as well that takes a Graphics object as a parameter. You’ll override this method in each of your control types. Once you’ve got your base class created, write a label control class that extends it. You’ll need to add a String variable to the class to hold the text to display. Override the base class’ render method and use the dimension and offset data from the base class as well as the String data from your label class to render the control. After everything is set up, you’ll need to create and instantiate an instance of your label and store it somewhere that’s accessible to Slick’s render method. Whenever it’s time to render your scene, call your label’s render method and it should be displayed on your screen.
Now the trick. I’ll stick to using the mouse to focus a control to keep the concept simple. Whenever Slick processes a mouse clicked event, compare the x and y offsets of the event to your controls bounds. If the click was within the bounds of your label, you’ll need to set a flag to indicate that subsequent keyboard events (specifically the key typed event) should be sent to your label. Assuming you’ve written a proper getter/setter method for the label control, you just get the current value of the labels text and append the typed key. In theory, that’s all you should need to do to create a text input field in Slick.
And now the dirty trick. The above method is “good” for doing a single control, but assuming you’ll want multiple controls, you’ll need to come up with a more elegant way to keep track of your controls as well as a better way to route your input to the proper place. Ideally you don’t want to have to check every control when an event occurs to see if it needs to process it. You’ll also want to let controls handle their own events instead of the main class doing everything.
It can seem like a lot of work, but it’s not too bad if you take it step by step, and figuring out how to implement a control from the ground up is actually quite a lot of fun. I’ve been working on my Slick based GUI system for about 2 months off and on. It’s my first serious attempt at implementing my own GUI controls and I’ve managed to get quite a lot of functionality implemented with surprisingly few moments of banging my head against the proverbial wall. If I can manage it, it can’t be that hard to figure out. :point: LOL
There’s some good discussion in this thread about implementing a button control in Java2D. While it’s not specifically about creating a text input control, a lot of the discussion/concepts are applicable to all types of controls.
Thank you CodeHead.
I’ll do it step by step and hopefully i can manage it.