Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 9-10: Interactive stripes

// An array of stripes
Stripe[] stripes = new Stripe[10];

void setup() {
  size(200,200);
  
  // Initialize all Stripe objects
  for (int i = 0; i < stripes.length; i ++ ) {
    stripes[i] = new Stripe();
  }
}

void draw() {
  
  background(100);
  // Move and display all Stripe objects
  for (int i = 0; i < stripes.length; i ++ ) {
    // Check if mouse is over the Stripe
    stripes[i].rollover(mouseX,mouseY); // Passing the mouse coordinates into an object.
    stripes[i].move();
    stripes[i].display();
  }
}
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 9-10: Interactive stripes

class Stripe {
  
  float x;     // horizontal location of stripe
  float speed; // speed of stripe
  float w;     // width of stripe
  
  // A boolean variable keeps track of the object's state.
  boolean mouse; // state of stripe (mouse is over or not?)

  Stripe() {
    // All stripes start at 0
    x = 0; 
    // All stripes have a random positive speed
    speed = random(1); 
    w = random(10,30);
    mouse = false;
  }

  // Draw stripe
  void display() {
    
    // Boolean variable determines Stripe color.
    if (mouse) { 
      fill(255);
    } else {
      fill(255,100);
    }
    
    noStroke();
    rect(x,0,w,height);
  }

  // Move stripe
  void move() {
    x += speed;
    if (x > width + 20) x = -20;
  }

// Check to see if point (mx,my) is inside the Stripe.
  void rollover(int mx, int my) { 
    // Left edge is x, Right edge is x + w
    if (mx > x && mx < x + w) {
      mouse = true;
    } else {
      mouse = false;
    }
  }
}
  • Verlila

    stupid

  • Killy

    très stupide

  • Lilly

    je sais

  • maroof

    I am having some problem using . I noticed from your source code that you used a .jar file. I was wondering what is wrong with the first approach. thanks

  • dxlara

    Hello, is there an optional syntax for the if statement, or what is the difference?

    if (x > width + 20) x = -20;

    vs

    if (x > width + 20) {
    x = -20;
    }

    Thank you! (I am enjoying your book and videos a lot!)

  • dxlara

    I found the answer here: http://forum.processing.org/one/topic/if-statements-and-curly-braces.html

    I can use the optional formatting if there is only one statement for the if…

  • Pete
  • ipinlnd

    we use brackets when there are more than one line in the statement. like:
    if(X > width +20)
    {
    x = -20;
    println(x);
    }