Input Box and output box - applet

If you have played Runescape, you know at the bottom how yuo can enter text in and It would send it to everyone in the game. And it shows the text that other people say below it. How do I accomplish this in my applet? I have tryed keyTyped, but you can erase using backspace ect. And I’ve Tried using JTextField, but then I can’t controle my character. Any Ideas?

Designate a specific key as the “say” key. Then, whenever that key is pressed, set a flag in your key listening class to true. In your keypressed method, check if that flag is set to true, and if it is, send all the keystrokes recieved to a string that will appear on the screen. When the user presses enter, turn your “say” flag back off. You can see this type of behavior in many professional games and it’s easy to implement.

Sorry if my question is un-understanable…

I ment, how do I do a box, and still have my gui images behind it. ALSO implement things like backspace, unless you can use JTextField. The only problem with using jtext field is that the controels get messed up, so maby I need to look into the JTextField class more. But, if I do use JTextField, how do I get it to be transparent, so my GUI image can show behind it?

You’ll probably need to make a custom component to get the transparency you want. What do you mean by the JTextField making your controls not work? Is it a focus issue, (the text field is taking all the key input and therefore the keys you’ve specified to control your character aren’t being listened to by the game)? Or do you mean that adding the text field to your game is making your gui not appear correctly?

Sorry for the not replying sooner.

I’ve tried making a custom component, through lsitening for Character keys instead of the other keys. And it worked. Except I don’t know how to code the backspace, to erase the last character. I tried to sue JTextFiedl but, what the problem is, is what you explained. the text field is taking al lthe key input. I looekd up the class/api, but couldn’t find anything on making it unfocused. Also, JTextfield is drawn under my GUI, which is drawn in update(). So when every I type, the gui flikers, and I can’t see what I’m writing.

Is there any other way to accomplish this? www.miragesource.com … I’m trying to immatate this. Its written in VB6, thats how they do it, they have controels with all sorts of fancy stuff, but I don’t have enoguh money fro vb6, and I’d rather have cross platform and more powerfull thing. If you can help me easier in lvie chat, my AIM is tr00p3er, and my MSN is ryancapote@hotmail.com

If I were in your situation, I would render the text box completely myself, instead of trying to use existing Java components to do the job. While JTextField is very nice for non-game applications, it really isn’t meant for what you’d like to do. By actively rendering your text box like whatever else is on your screen, you can have absolute control over where it appears and even give it some transparency if you want to get fancy. Like I said before, designate a “say” key that will send all your key strokes to your text box. To implement back space, all you need to do is get the key code of the KeyEvent that is passed into the keyPressed(KeyEvent) method and check if it is equal to KeyEvent.BACK_SPACE, and then delete the apropriate character from the String in your text box. Here’s what I’m thinking:



public class MessageBox implements KeyListener {

      private StringBuffer text;

      public MessageBox() {...}

      public draw(Graphics g) {...}

      public keyPressed(KeyEvent e) {

            int code = e.getKeyCode();

            if(code = KeyEvent.BACK_SPACE && text.length() > 0) {
                  text.deleteCharAt(text.length()-1);
            } else {
                  //add character to text corresponding to the key pressed
            }

      }

}


The example is simplistic but I think it illustrates what I mean.

As for transparency, you could go with a JTextField and use setOpaque(false). This should make it completely transparent.

If you want to have partial opacity (to reduce the contrast of the game content underneath), a custom component will be much easier than JTextField.

In theory you should be able to set a background color with a semitransparent alpha value (such as new Color(255,0,0,128) for a semitransparent red). However, I just tried to do that and it failed - the text field suddenly shows lots of repaint artifacts.

As for the key events, you could do the following: Whenever your game loop gets a KeyEvent and you want that to be processed by the JTextField, retrieve the ActionMap key for that event from the InputMap, then retrieve the Action from the ActionMap and execute the Action:


Object key=textfield.getInputMap().get(KeyStroke.getKeyStroke(e.getKeyChar()));
if(key!=null)
{
      Action a=textfield.getActionMap().get(key);
      if(a!=null)
      {
            ActionEvent ae=new ActionEvent(textfield, KeyEvent.KEY_TYPED, e.getKeyChar()+"");
            a.actionPerformed(ae);
      }
}

In this example e is the KeyEvent. However, as mentioned by the other posters, using JTextField is probably not worth the trouble, and in the long run a custom component is better suited.