Layering stroke() and fill() correctly

edited August 2014 in Using Processing

I have been using contour maps and was having trouble correctly layering different calls of stroke() and fill().

For instance, I draw a bunch of strokes that surround an island and then I want to fill in the contour with a green land. However, this fill() is always drawn underneath the other strokes, to my dismay, and I have not figured out how to make sure that stays on top even though it comes after the stroke.

Is there any way to achieve this?

Tagged:

Answers

  • edited August 2014
    void setup()  {
      size(640, 480, P3D);
      background(100);
    }
    
    void draw()  {
      background(100);
    
      fill(0, 200, 0);
      stroke(0);
      strokeWeight(3);
      rect(width/2, height/2, 50, 50);
      noFill();
      noStroke();
    
      fill(200, 0, 0);
      stroke(255);
      strokeWeight(3);
      rect(mouseX, mouseY, 100, 70);
      noFill();
      noStroke();
    
    }
    
  • Making this a P2D sketch eliminates the problem. But I need a P3D setup because I use Syphon in the final application. I am wondering how to possibly use a z-posiiton for the points...

  • So using a translate in combination with a push/pop matrix call I was able to sort the shapes and not get anything to shine through.

    void setup()  {
      size(640, 480, P3D);
      smooth();
      background(100);
    }
    
    void draw()  {
      background(100);
    
      fill(0, 200, 0);
      stroke(0);
      strokeWeight(3);
      rect(width/2, height/2, 50, 50);
      noFill();
      noStroke();
    
      pushMatrix();
      translate(0, 0, 1);
    
      fill(255);
      stroke(255);
      strokeWeight(3);
      rect(mouseX, mouseY, 100, 70);
      noFill();
      noStroke();
    
      popMatrix();
    
    }
    
Sign In or Register to comment.