Pshape setFill() not working for me

Hi, I am trying to create an annulus sector and be able to control the interior fill color. I created the shape as Pshape using arcs and lines, and then try to fill the color in the draw() routine. The annulus sector displays, but the setFill() did not change the color.

Is there a way to make this work ?

Thanks.

(Formatted code below)

Answers

  • PShape annulus_sector, arc1, arc2, line1, line2;
    
    float rad_inner = 150;
    float rad_outer = 200;
    float angle_start = PI;
    float angle_end = 13*PI/8;
    float x1, y1, x2, y2;
    
    void setup() {
      size(400, 400);
    
      // Create the shape group
    
      annulus_sector = createShape(GROUP);
    
      // Make shapes
    
      noFill();
    
      arc1 = createShape(ARC, 0, 0, rad_inner, rad_inner, angle_start, angle_end, OPEN);
    
      x1 = rad_inner/2 * cos(angle_start);
      y1 = rad_inner/2 * sin(angle_start);
      x2 = rad_outer/2 * cos(angle_start);
      y2 = rad_outer/2 * sin(angle_start);
      line1 = createShape(LINE, x1, y1, x2, y2);
    
      arc2 = createShape(ARC, 0, 0, rad_outer, rad_outer, angle_start, angle_end, OPEN);
    
      x1 = rad_inner/2 * cos(angle_end);
      y1 = rad_inner/2 * sin(angle_end);
      x2 = rad_outer/2 * cos(angle_end);
      y2 = rad_outer/2 * sin(angle_end);
      line2 = createShape(LINE, x1, y1, x2, y2);
    
      // Add the "child" shapes to the parent group
    
      annulus_sector.addChild(arc1);
      annulus_sector.addChild(line1);
      annulus_sector.addChild(arc2);
      annulus_sector.addChild(line2);
    }
    
    void draw() {
      background(80);
      translate(width/2, height/2);
    
      annulus_sector.setFill(color(100,125,175));    //DOES NOT WORK !!
    
      shape(annulus_sector); // Draw the group 
    }
    
  • Answer ✓

    did you try using setfill on arc1 after line 20 ?

  • @Chrisir ... I did not try that ... I can give that a try but am not optimistic that they will do the trick, because I think that will fill in the area within the arc but not within the annulus sector.

    In the meantime, I was reading more of the documentation. I think the problem above is that I created the Pshape with arc() and not within vertex() function.

    So, I re-did the code using vertex() to construct the Pshape, and it works as intended now !!

    See below code for updates.

    I do not know if this is an efficient way to create annulus sectors; I am open to suggestion if anyone has a better way.

    Thanks.

    PShape annulus_sector;
    
    float rad_inner = 150;
    float rad_outer = 200;
    float angle_start = PI;
    float angle_end = 3*PI/2;
    float x1, y1, x2, y2;
    
    void setup() {
      size(400, 400);
    
      // Create the shape
    
      annulus_sector = createShape();
      annulus_sector.beginShape();
    
      // Make shapes
    
      x1 = rad_inner * cos(angle_start);
      y1 = rad_inner * sin(angle_start);
      x2 = rad_outer * cos(angle_start);
      y2 = rad_outer * sin(angle_start);
    
      annulus_sector.vertex(x2, y2);
      annulus_sector.vertex(x1, y1);
    
      println(x2, y2, x1, y1);
    
      for (float theta = angle_start; theta <= angle_end; theta += 0.1)
      {
         annulus_sector.vertex(rad_inner * cos(theta), rad_inner * sin(theta));
         println(rad_inner * cos(theta), rad_inner * sin(theta));
      }
    
      annulus_sector.vertex(rad_inner * cos(angle_end), rad_inner * sin(angle_end));
    
      x1 = rad_inner * cos(angle_end);
      y1 = rad_inner * sin(angle_end);
      x2 = rad_outer * cos(angle_end);
      y2 = rad_outer * sin(angle_end);
    
      annulus_sector.vertex(x1, y1);
      annulus_sector.vertex(x2, y2);
    
      println(x1, y1, x2, y2);
    
      for (float theta = angle_end; theta >= angle_start; theta -= 0.1)
      {
         annulus_sector.vertex(rad_outer * cos(theta), rad_outer * sin(theta));
         println(rad_outer * cos(theta), rad_outer * sin(theta));
      }
      annulus_sector.vertex(rad_outer * cos(angle_start), rad_outer * sin(angle_start));
    
      annulus_sector.endShape(CLOSE);
    
    }
    
    void draw() {
      background(80);
      translate(width/2, height/2);
    
      annulus_sector.setFill(color(100,125,175));
    
      shape(annulus_sector); // Draw the group 
    }
    
  • you can set colors in setup() as I first suggested

    but noFill(); is NOT allowed before doing so (dunno why)

    PShape annulus_sector, arc1, arc2, line1, line2;
    
    float rad_inner = 150;
    float rad_outer = 200;
    float angle_start = PI;
    float angle_end = 13*PI/8;
    float x1, y1, x2, y2;
    
    void setup() {
      size(400, 400);
    
      // Create the shape group
    
      annulus_sector = createShape(GROUP);
    
      // Make shapes
    
      // noFill();
    
      arc1 = createShape(ARC, 0, 0, rad_inner, rad_inner, angle_start, angle_end, OPEN);
      arc1.setFill(color(100, 125, 175));
      arc1.setStroke(color(100, 125, 175));
    
      x1 = rad_inner/2 * cos(angle_start);
      y1 = rad_inner/2 * sin(angle_start);
      x2 = rad_outer/2 * cos(angle_start);
      y2 = rad_outer/2 * sin(angle_start);
    
      line1 = createShape(LINE, x1, y1, x2, y2);
      line1.setStroke(color(100, 125, 175));
    
      arc2 = createShape(ARC, 0, 0, rad_outer, rad_outer, angle_start, angle_end, OPEN);
      arc2.setFill(color(100, 125, 175));
      arc2.setStroke(color(100, 125, 175));
    
      x1 = rad_inner/2 * cos(angle_end);
      y1 = rad_inner/2 * sin(angle_end);
      x2 = rad_outer/2 * cos(angle_end);
      y2 = rad_outer/2 * sin(angle_end);
      line2 = createShape(LINE, x1, y1, x2, y2);
      line2.setStroke(color(100, 125, 175));
    
      // Add the "child" shapes to the parent group
    
      annulus_sector.addChild(arc1);
      annulus_sector.addChild(line1);
      annulus_sector.addChild(arc2);
      annulus_sector.addChild(line2);
    }
    
    void draw() {
      background(80);
      translate(width/2, height/2);
    
      //annulus_sector.setFill(color(100, 125, 175));    //DOES NOT WORK !!
    
      shape(annulus_sector); // Draw the group
    }
    
Sign In or Register to comment.