text, and mouse

More with Processing:
Text and Animation
Console Printing
 Prints to the small output window on the Processing
 print(“Hey!”);
 print(x);
 To go to the next line:
 println(“Hey – go down a line!”);
Probably, you want to type to the applet window.
We need to create a .vlw font
Go to Tools -> Create Font
Find the one you like
In your sketch folder you will see a folder named data,
and in it is your font file.
Text writing commands
Whatever your font is
Pfont font;
font = loadFont("ACaslonPro-BoldItalic-48.vlw");
My message will be red!
text(“Hello!”, 100, 100);
Draw it at (100,100)
Text size
 A font used at the same size it’s created looks best.
But you can change the size of your text:
 textSize(12);
 If we need to know how wide our message is, we can
 float width = textWidth(“My message”);
Follow the Mouse
 use mouseX and mouseY to draw instead and watch
the text follow the mouse!
 text("Hello!", mouseX, mouseY);
 We can animate our message by changing the x,y
values of where we write the text.
 Keep the current values of x,y outside the methods so
that I can access them at any point.
// Make all the variables I need
float x = 100;
float y = 100;
PFont font;
// This happens once
void setup(){
font = loadFont("ACaslonPro-BoldItalic-48.vlw");
// This happens over and over
void draw(){
Background drawing
 What happens if I move the background line to the
setup method? Try it!
Keep it in bounds
Use an if statement:
if(x > 400){
x = 0;
Means: if the x variable is bigger than the width of the
screen, reset it to 0. Do this for y also.
Your turn
 Can you change the code so that it wraps around
when the right-most edge of the text is off screen
rather than the left-most edge?
 Hint: Use textWidth!
 Let’s make the word bounce around rather than
wrap. To do this we will use the idea of a velocity –
how many pixels the word moves in each direction.
 Add variables near x and y for dx and dy.
 Change the code that moves the words to
 x = x + dx;
 y = y + dy;
 What should happen to the velocity when a boundary
is hit? Can you write the if statements?
More if statements
 And : &&
if (x<0 && y < 0){
 Or: ||
if ( x < 0 || y < mouseY){
 What do you think it does?
if (y>400 || mousePressed){
dy = dy * -1;
And more…
x = x + dx;
x =x – dx;
Can you…
 Make the word move faster with each bounce?
 Write on the screen how many times the bounce
 Make the text change color when it bounces?
 Make it follow the mouse when the button is pressed,
but bounce otherwise?

similar documents