Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (602)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 867 times)
0 Members and 1 Guest are viewing this topic.
Online 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: 51

From rags 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.
Online 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: 51

From rags 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 Opiop
« 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.

Online 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 Opiop
« 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

Online 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:
while( {
         if((int) Keyboard.getEventCharacter() != 0) {

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.

Jesse (15 views)
2015-07-29 04:35:27

Riven (37 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (42 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (31 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!