Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Input Box and output box - applet  (Read 1411 times)
0 Members and 1 Guest are viewing this topic.
Offline Cirdan

Senior Newbie




Java games rock!


« Posted 2005-03-25 20:50:09 »

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?
Offline allan_s

Senior Newbie




pwned


« Reply #1 - Posted 2005-03-26 12:13:44 »

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.

Offline Cirdan

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-03-26 15:34:43 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline allan_s

Senior Newbie




pwned


« Reply #3 - Posted 2005-03-27 01:21:46 »

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?

Offline Cirdan

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-03-28 05:07:46 »

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

Offline allan_s

Senior Newbie




pwned


« Reply #5 - Posted 2005-03-30 01:02:41 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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.

Offline digitprop

Junior Devvie





« Reply #6 - Posted 2005-03-30 07:53:55 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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.

M. Fischer . www.digitprop.com
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (21 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (60 views)
2014-12-15 09:26:44

Mr.CodeIt (48 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (117 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!