Loading...
Logo
Processing Forum
I want to display the frame rate of the sketch as the title of the display window. How would I do this?
Thanks

Replies(7)

You can set the frame's title with frame.setTitle("foo"). To accomplish this, you need to call the following every frame (during draw()):

Copy code
  1. frame.setTitle(frameRate); //Set the frame title to the frame rate
The method setTitle(String) in the type Frame is not applicable for the arguments (float)
So I guess setTitle can't accept floats as arguments, is there an alternate method or a way to continuously convert the frame rate to an integer?
frame.setTitle() works in setup() for me, in 2.0.

md_za6, you need to convert the integer to a string:
Copy code
  1. void setup()
  2. {
  3.   size(500, 200);
  4.   frame.setTitle("Sketch");
  5. }
  6. int c;
  7. void draw()
  8. {
  9.   frame.setTitle(str(frameRate));
  10.   line(c, 0, c, height);
  11.   c++;
  12. }
It is a bit "hysterical" this way, perhaps showing the frame rate one frame out of ten is enough:
if (frameCount % 10 == 0) frame.setTitle(str(frameRate));
Example:
Copy code
  1. void setup()
  2. {
  3.   size(500, 200);
  4.   frame.setTitle("Sketch");
  5.   frameRate(500);
  6. }
  7.  
  8. int c;
  9. void draw()
  10. {
  11.   if (frameCount % 50 == 0) frame.setTitle(str(frameRate));
  12.   line(c, 0, c, height);
  13.   c++;
  14.   if (c >= width)
  15.   {
  16.     stroke(random(256));
  17.     c = 0;
  18.   }
  19. }

It's funny I've never encountered this error w/ setTitle().
Now I realize it's b/c I always use a descriptive String before the variable.
And an auto concatenation makes all of them String!  

frame.setTitle("FPS: " + frameRate);

Also, "" + variable or variable + "", replaces str(variable).  
" Also, "" + variable or variable + "", replaces str(variable)."
Yes but concatenation of strings creates behind the scene a StringBuffer:
Copy code
  1. StringBuffer b = new StringBuffer("");
  2. b.append(variable);
  3. return b.toString();
while str(variable) involves only String.valueOf(variable);
I thought Processing's own convert functions did much more than Java's!
Returns the string representation of primitive datatypes and arrays.

String.valueOf() doesn't deal w/ Arrays but char[]!

Even though str() ends up invoking valueOf() after all; I still think that "" + variable would be faster, 
b/c besides being a built-in Java feature, it skips calling a function which is 
yet to decide what's the best course to take!

Nevertheless, I think str() is aesthetically better. Plus, it's very handful on converting whole Arrays!  
You have one str() version per type of argument... The simpler types just call String.valueOf(), while the arrays are managed with a loop.

" besides being a built-in Java feature, it skips calling a function"
The function call is probably optimized away by the Jit compiler, anyway. And being "built-in" doesn't mean it is fast: the concatenation creates object to collect later, as show, etc. That's why it is recommended to use a StringBuilder instead of using + in a loop.

" str() is aesthetically better"
Hey, at least we agree on this point! "" + v feels old school... And str() carries the intent, while "" + v is more obscure.
Note also that String.valueOf() can cache the result (IIRC), so it can be really fast (although perhaps it is only true for integers).