Messed up "if" statements with draw()?

edited September 2016 in Questions about Code

Edit: Solved -- question can be deleted.

I believe I'm misunderstanding something about how the draw() function works. Below is a minimal working example of what I have in mind. The conditional 'if' statement seems to be reversing the fill colour. Rectangles should be in red, ellipses in blue.

Instead, the rectangle shows up in blue, while the circle shows up in red. If I only display(0) or display(1), then the correct colour shows up. Any thoughts?

Thanks for any help!

void setup() {
  size(400, 400);
}

void draw() {
  display(0);
  display(1);
}

void display(int i) {    
    if(i == 0) {      
      rect(20, 20, 40, 40);
      fill(200, 0, 0); // rectangles are red!
    }
    if(i == 1) {
      ellipse(100, 100, 20, 20);
      fill(0, 0, 200); // circles are blue!
    }
}
Tagged:

Answers

  • Answer ✓

    This question can be deleted, the obvious mistake is that fill must be called before the shape to be drawn. Reversing the order solves the issue.

Sign In or Register to comment.