text() does not create a "text box" as you would expect in Flash or Director. text() simply draws the text as pixels onto your screen for display. Thus, when you make multiple calls to text() to the same location in the same frame, they will draw over each other.
However, you can write your own custom console via this technique:
Code:
PFont uifont; //holds font for console ui
int maxLines = 30; //number of lines console will have at most
String console[]; //array of strings for console
void setup(){
size(300,500);
uifont = loadFont("Arial-BoldMT-12.vlw");
console = new String[maxLines];
//do a test for the console
printConsole("begin console");
}
void draw(){
background(0);
//draw the console every frame
drawConsole(10,15);
}
//Example code using printConsole
void mousePressed(){
if(mouseButton == LEFT)
printConsole("mousePressed. button = left");
if(mouseButton == RIGHT)
printConsole("mousePressed. button = right");
if(mouseButton == CENTER)
printConsole("mousePressed. button = center");
}
void keyPressed(){
printConsole("keypressed. button = " + key + " and ascii = " + keyCode);
}
//Draws the on-screen console
void drawConsole(float x, float y){
//Set drawing specifications
fill(255,180);
noStroke();
textFont(uifont,12);
textAlign(LEFT);
//move it to x and y positioning
pushMatrix();
translate(x,y);
//the number of pixels per row
float rowHeight = 15;
//go through console array and draw the text
for(int i=0;i<maxLines;i++){
if(console[i]!=null){
//do some smart detection of \n linebreaks etc
text(console[i],0,0);
//move to the next line
translate(0,rowHeight);
}
}
popMatrix();
}
//Prints to on-screen console and to processing console.
void printConsole(String string){
//allow us to print to both our own console
//and the println() console
println(string);
//shift everything down one, remove the last,
//and insert string to the first slot
//a fast way to do this is by making a new array
String refreshedConsole[] = new String[maxLines];
//copy the printer string to the first index
refreshedConsole[0] = string;
//copying everything into the new array except the last one
arraycopy(console,0,refreshedConsole,1,maxLines-1);
//set the pointer for console to our newly refreshed console
console = refreshedConsole;
}
Indeed we've created a MSDOS/terminal-like thing here, except going in reverse. Here, we can use printConsole() whenever we want to print something to our custom on-screen console. It might be useful for someone to make this into a library (I might do it later if I get really bored...).