Later on, if you want to get into doing user input for Strings, here's a simple class to get you started. I wrote this for a project a while back.
I used a class I created called Console. No of course it's not a real console... that's just what I called it. :)
Basically, if it's in "active" mode, it will be able to take alphabetical and numerical input as well as be compatible with backspace.
The keyAnalyzer() function simply takes a character and spits out either "NUMBER", "LETTER", or "OTHER" depending on what catagory the key is in. This is in case you don't want to allow punctuation. In the example below, I did not include "OTHER" in the keyPressed() function, so punctuation will not work in the Console, nor will spaces.
You have the freedom to limit whatever characters you want of course.
When you're ready to type in the console, just say c.activate() for it to listen for keys, and c.deactivate() for it to stop listening for keys. In this example, I put c.activate() in the setup so that it starts active. I will note that the textBox isn't really something drawn. It only has a location. If you run this program below, you'll simply be able to start typing immediately, and you'll see text appearing at the x and y points (10,100).
I hope this was interesting so someone. :)
-THz
- Console c = new Console(10,100,12);
- void setup()
- {
- size(500,500);
- c.activate();
- }
- void draw()
- {
- background(0);
- c.display();
- }
- void keyPressed()
- {
- if (keyAnalyzer(key).compareTo("LETTER") == 0 || keyAnalyzer(key).compareTo("NUMBER") == 0)
- {
- c.addChar(key);
- }
- if (keyCode == BACKSPACE)
- {
- c.deleteChar();
- }
- }
- String keyAnalyzer(char c)
- {
- if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
- {
- return "NUMBER";
- }
- else if (c == 'A' || c == 'a' || c == 'B' || c == 'b' || c == 'C' || c == 'c' || c == 'D' || c == 'd' || c == 'E' || c == 'e' ||
- c == 'F' || c == 'f' || c == 'G' || c == 'g' || c == 'H' || c == 'h' || c == 'I' || c == 'i' || c == 'J' || c == 'j' ||
- c == 'K' || c == 'k' || c == 'L' || c == 'l' || c == 'M' || c == 'm' || c == 'N' || c == 'n' || c == 'O' || c == 'o' ||
- c == 'P' || c == 'p' || c == 'Q' || c == 'q' || c == 'R' || c == 'r' || c == 'S' || c == 's' || c == 'T' || c == 't' ||
- c == 'U' || c == 'u' || c == 'V' || c == 'v' || c == 'W' || c == 'w' || c == 'X' || c == 'x' || c == 'Y' || c == 'y' ||
- c == 'Z' || c == 'z')
- {
- return "LETTER";
- }
- else
- {
- return "OTHER";
- }
- }
- class Console
- {
- float x;
- float y;
- String chars;
- int numChars;
- boolean active;
- int font;
-
- Console(float x, float y, int font)
- {
- this.x = x;
- this.y = y;
- active = false;
- this.font = font;
- chars = "";
- numChars = 0;
- }
-
- void display()
- {
- line(x,y,x,y+font);
- textSize(font);
- text(chars,x,y);
- }
-
- void addChar(char c)
- {
- chars += c;
- numChars++;
- }
-
- String readString()
- {
- return chars;
- }
-
- boolean isActive()
- {
- return active;
- }
-
- void activate()
- {
- active = true;
- }
-
- void deactivate()
- {
- active = false;
- }
-
- void reset()
- {
- chars = "";
- }
-
- void deleteChar()
- {
- if (numChars > 0)
{
- chars = chars.substring(0,chars.length()-1);
- numChars -= 1;
- }
- }
- }