Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (566)
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  
  Making your own input box  (Read 564 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2013-11-28 11:12:20 »

So, I would really like to make console or something for my game, but I'm kinda lazy.

The problem is that I don't know a good way to make a box where you can enter characters, for example like JTextField. The problem is taking input. What is the best way to take input? I could just hash all the characters on the keyboard and stuff, but that is one ugly way of doing it. Are there any other ways of taking input beside hashing all the keyboard?
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #1 - Posted 2013-11-28 13:12:09 »

Once you break down the concept of a text input box, you'll find it's not that much work to implement a basic one. Essentially you'll need to create a class that implements KeyListener and contains a Rectangle, a Point, and a String. Your key listener should check to see what key is being pressed and act appropriately. In the case of a backspace, you'll want to trim a character off of the end of your classes stored String value (assuming the String isn't already empty), in the case of an actual character, you'll want to append the value to the end of your String. If you need to limit which keys are processed by the event handler, use the events KeyCode value to see if the key is one you want to handle (here's an ASCII code reference chart). When it comes time to render your text control, draw your rectangle at the location stored in your Point variable, then draw the string over top of it using whatever methods your graphics library provides. If you want to avoid the text appearing outside of the control, use the Rectangles data to set a clipping bounds.

Of course the above is just a rough outline. At some point you'll need to figure out how you want to handle things like text alignment, line wrapping, caret implementation, and other items to make it polished. If you're going to have more than one or two controls you'll probably want to look into implementing a control stack/manager at some point as well, but you should be able to get away without one for the time being.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline trollwarrior1
« Reply #2 - Posted 2013-11-28 13:35:05 »

If I did not make myself clear:

what I asked was a nice way to "act accordingly to which key was pressed"
What I meant by that is there any other already MADE way to do that. I know how to make it, but it is really ugly and I don't like it. I don't like to leave ugly things in my code Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #3 - Posted 2013-11-28 14:29:00 »

what I asked was a nice way to "act accordingly to which key was pressed"

Sorry, I misunderstood. Try having a look at InputMaps and ActionMaps for some ideas. These mechanisms are what's used for handling key inputs to text components in Swing. If I recall correctly, they get their default values from the currently active L&F. That should at least give you a place to start.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2013-11-28 15:01:57 »

I'm going to ask the stupid question here, but are you using Java2D or OpenGL? I know a way to do it in OpenGL, but not Java2D. Although, you'll probably need a StringBuilder for each as its not immutable.

Offline trollwarrior1
« Reply #5 - Posted 2013-11-28 15:03:05 »

I was thinking maybe there was already a library for lwjgl made for that purpose of something, but I will just make my own Tongue I know how to do it, but was I'm lazy Cheesy
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2013-11-28 15:07:46 »

I think that TWL for LWJGL probably has something you could use, but its so much easier to just do it yourself. Check this out:
theCodingUniverse Text

Offline trollwarrior1
« Reply #7 - Posted 2013-12-01 20:07:32 »

This reply might seem strange, but it is made for people who might be looking for this. (Like tags or something)

How to make lwjgl text input box? To take input from the keyboard with LWJGL, you can try to do this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
while(Keyboard.next()) {
         
         if((int) Keyboard.getEventCharacter() != 0) {
                           
            System.out.println(Keyboard.getEventCharacter());
         
         }
         
      }

This awesome method will return the char of the keys that were pressed! It is just superb. I'm glad I rewrote my keyboard handler and discovered this method.
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.

Pippogeek (35 views)
2014-09-24 16:13:29

Pippogeek (28 views)
2014-09-24 16:12:22

Pippogeek (17 views)
2014-09-24 16:12:06

Grunnt (40 views)
2014-09-23 14:38:19

radar3301 (23 views)
2014-09-21 23:33:17

BurntPizza (58 views)
2014-09-21 02:42:18

BurntPizza (29 views)
2014-09-21 01:30:30

moogie (34 views)
2014-09-21 00:26:15

UprightPath (47 views)
2014-09-20 20:14:06

BurntPizza (51 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!