topical media & game development

talk show tell print

graphic-processing-learning-17-example-17-3-example-17-3.pde / pde



  // 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 - 3;
    
    // If x is less than the negative width, then it is off the screen
    // textWidth() is used to calculate the width of the current String.
    float w = textWidth(headlines[index]); 
    if (x < -w) {
      x = width;
      // index is incremented when the current String has left the screen in order to display a new String.
      index = (index + 1) % headlines.length; 
    }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.