// Learning Processing // Daniel Shiffman // http://www.learningprocessing.com // Example 17-3: Scrolling headlines // An array of news headlines String[] headlines = { "Processing downloads break downloading record." , "New study shows computer programming lowers cholesterol." , }; PFont f; // Global font variable float x; // Horizontal location int index = 0; void setup() { size(400,200); f = createFont( "Arial" ,16,true); // Initialize headline offscreen x = width; } void draw() { background(255); fill(0); // Display headline at x location textFont(f,16); textAlign (LEFT); // A specific String from the array is displayed according to the value of the "index" variable. text(headlines[index],x,180); // Decrement x x = x - 2; //adjust - 2 to change speed // If x is less than the negative width, then it is off the screen //ie. if text is 10 wide needs to be at x = -10 to be off screen // textWidth() is used to calculate the width of the current String. float w = textWidth(headlines[index]); if (x < -w) { x = width; //go back to other side // index is incremented when the current String has left the screen in order to display a new String. index++; if (index>= headlines.length) index=0; } }