We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Writing Text to Applet Window
Page Index Toggle Pages: 1
Writing Text to Applet Window (Read 1579 times)
Writing Text to Applet Window
Jan 3rd, 2007, 12:43am
 
Can anyone point me to some code, or the best function to use, for writing arbitrary data to an applet window?

I'm trying create a diagnostic display for a 3D accelerometer. The code is working fine with println() writing data to the console window no problem. Now I want to write the same data to the applet window.

The requirement is pretty simple. Display X=<Value>, Y=, Z= etc.and update the window in as new sample data comes in. The sample update period is about 50ms. I've tried the text() function using x,y position and text box size. However, text() seems to overwrite on top of the last output text rather than clearing the text box and then displaying which ends up with a mess on screen. I'm also not sure about the refresh speed and whether it is fast enough to keep up with the data stream.

Ideally I'd love to use a widget library like controlP5 and have three dial/knob widgets representing the data. However, it looks like none of the control libraries lets you dynamically set the control value during run time. i.e. there is only a getvalue() function and apparently no setvalue() function. Am I missing something here?

Any help for a Processing beginner would be most appreciated.
Re: Writing Text to Applet Window
Reply #1 - Jan 3rd, 2007, 2:01am
 
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...).
Re: Writing Text to Applet Window
Reply #2 - Jan 3rd, 2007, 6:14pm
 
Many thanks. This is very helpful.
Re: Writing Text to Applet Window
Reply #3 - Jan 3rd, 2007, 7:55pm
 
I had to do something similar in Flash, but the advantage with Flash is that TextFields are selectable. Whenever I had a thingy up with a friend on a different OS testing it I could ask them to copy the console output.

How is it that the console in the IDE is selectable? It sounds like a great piece of functionality that's missing from Processing.

(Then again you could always use Javascript to write to a div outside the applet.)
Re: Writing Text to Applet Window
Reply #4 - Jan 4th, 2007, 5:43am
 
mnn

Javascript writing would be very useful.

Also, a self-signed applet that can copy directly to clipboard can work.

Finally, if it's an executable you can also write to a .txt file and just have that emailed.

I guess you can also make a webservice that recieves the error then sends it as an email automatically to you, but we're getting out of hand here Smiley
Re: Writing Text to Applet Window
Reply #5 - Jan 4th, 2007, 12:57pm
 
Maybe the program could offer you discounts on Viagra in this mail as well.
Re: Writing Text to Applet Window
Reply #6 - Jan 4th, 2007, 3:10pm
 
Things wirtten by println are availiable form applets if you (on windows, not sure about Mac/linux) right-click on the java icon in the system tray, and select "show console"
Re: Writing Text to Applet Window
Reply #7 - Jan 4th, 2007, 3:33pm
 
My XP-PC is held together by digital jam and prayers (it even went blue-screen the first time I tried to write this) and has a lot of proprietry software on it that I've lost the CDs for.

And it rarely gives me the coffee cup on the system tray. Only when it feels like it.

I was more concerned about selectable text in applets really. That would be pretty useful.

Edit:

Since I'm writing something which does something similar in Perl, here's the JavaScript thing. Pretty easy really:
Code:

<html>
<head>
<script language="javascript">
function writeInDiv(div, string){
var d=document.getElementById(div);
d.innerHTML = string;
}
</script>
</head>
<body>
<a href="javascript:writeInDiv('myDiv', 'whatchootahkinaboutwillis');">Clicky</a>
<div id="myDiv">
</div>
</body>
</html>
Re: Writing Text to Applet Window
Reply #8 - Jan 5th, 2007, 6:56am
 
So how would you get a java app to output to javascript?

I assume you can use some interface library. Probably the easiest way is just to do it with php?

Like, have your program call myapp.php?output= + striiiiing

~M
Re: Writing Text to Applet Window
Reply #9 - Jan 5th, 2007, 6:45pm
 
I was about to say it's **** easy.

But it is a might particular. Make sure you have the draw(){} method in there or link() won't be called.

http://www.robotacid.com/PBeta/js_hah/index.html
Re: Writing Text to Applet Window
Reply #10 - Jan 5th, 2007, 7:00pm
 
and be aware that some browsers don't like the netscape.javascript lib at all ... like opera 9.1 on mac.

F
Page Index Toggle Pages: 1