Unexpected Color Change

edited February 2017 in Questions about Code

// Using Processing-3.3 // The stroke color defaults to black if no stroke command is issued. // In the code I was expecting the rectangle to have a black stroke color. // The ellipse has the expected blue stroke color. // The line has the expected green stroke color.

// Why does the rectangle have a green stroke color?

void setup() { size(480,420); background(220); }

void draw() { rect(100,200,100,100); // Expect this to have a black stroke color stroke(255,0,0); strokeWeight(3); stroke(0,0,255); ellipse(240,80, 115,115); stroke(0,255,0); line(5,5,400,310); }

Answers

  • Move background() to top of draw(), maybe? :-/

  • Putting "background(220);" as first line of the "draw" loop made no change. Thanks for the suggestion.

  • GoToLoop, THANK YOU! Also, where is the information for proper posting? PapaK

  • Answer ✓

    You mean, where that post is located? It's the 2nd top announcement:
    https://forum.Processing.org/two/discussions

  • Actually I am looking for the "etiquette" rules and suggestions.

  • // Using Push and Pop around part of the code // displays the stroke colors as I am expecting, // the rectangle now has a black border. // Now I notice the the strokeCap(PROJECT) appears to be applied // to the line before the it. // Is this a bug or am I not understanding how the code should work? // I am not expecting something to change before I actually change it.

    void setup() { size(480,420); //background(220); }

    void draw() { background(220); rect(100,200,100,100); stroke(255,0,0); strokeWeight(5); stroke(0,0,255); ellipse(240,80, 115,115); stroke(0,255,0); line(30,150,430,150); pushMatrix(); strokeWeight(9); strokeCap(PROJECT); line(30,170,430,170); popMatrix(); stroke(0,0,0); strokeWeight(1); line(430,140,430,189); }

  • // OK, I think I understand the coding format for the forum. Thanks. 
    
    // Using Push and Pop around part of the code 
    // displays the stroke colors as I am expecting, 
    // The rectangle now has a black border. 
    // Now I notice the the strokeCap(PROJECT) appears to be applied 
    // to the line before it. 
    // Is this a bug or am I not understanding how the code should work? 
    // I am not expecting something to change before I actually change it.
    
    void setup()
    {
      size(480,420);
      //background(220);
    }
    
    void draw()
    {
      background(220);
      rect(100,200,100,100);
      stroke(255,0,0);
      strokeWeight(5);
      stroke(0,0,255);
      ellipse(240,80, 115,115);
      stroke(0,255,0);
      line(30,150,430,150);
      pushMatrix();
      strokeWeight(9);
      strokeCap(PROJECT);
      line(30,170,430,170);
      popMatrix();
      stroke(0,0,0);
      strokeWeight(1);
      line(430,140,430,189);
    }
    
  • // Using pushStyle and popStyle displays all objects as expected.
    // Without them, the first "line" appears to be Projected, even though
    // it is before the strokeCap(PROJECT) line.
    // Any idea why this is occuring?
    
    void setup()
    {
      size(480,420);
      background(220);
    }
    
    void draw()
    {
      rect(100,200,100,100);
      //pushStyle();
      stroke(255,0,0);
      strokeWeight(5);
      stroke(0,0,255);
      ellipse(240,80, 115,115);
      //popStyle();
      stroke(0,255,0);
      line(30,150,430,150);
      strokeWeight(9);
      strokeCap(PROJECT);
      line(30,170,430,170);
      stroke(0,0,0);
      strokeWeight(1);
      line(430,140,430,189);
    }
    
  • Answer ✓
    • Remember that draw() is called back at about 60 FPS.
    • And all "style" commands are permanent until they're changed again.
    • After the 1st draw() iteration, strokeCap() is set to PROJECT.
    • Given your drawing is static (not an animation), it's a good idea to call noLoop() within setup():
      https://Processing.org/reference/noLoop_.html
  • GoToLoop, Thank you. That makes perfect sense! PapaK

Sign In or Register to comment.