Is there a way to change the color of PShape without entering begin/end?

我是研究僧i 提交于 2019-12-11 13:05:51

问题


I've been writing a program which redraws lots of (several hundred) the same PShape in different colors. However, I haven't found a way to redraw the PShape in different colors without actually recreating the PShape, i.e. going through the begin/end shape redoing all the vertices and simply changing the fill and then assigning it to a new PShape variable. I've tried things like tint(), fill(), setFill() and they all seem to require being called in begin/end shape.

Is there a way to redraw a PShape in different colors without redefining the shape completely and assigning it to a new variable?

Any help greatly appreciated.

(Here is a sort of collation of the different things I tried)

   PShape p;

    void setup()
{
  size(600,600,P2D);
  p = createShape();
  p.beginShape();
  p.vertex(0, 0);
  p.vertex(20, 0);
  p.vertex(20, 20);
  p.vertex(0, 20);
  p.endShape(CLOSE);
}

void draw()
{
  p.tint(200,100,30);
  p.fill(200,100,30);
  p.setFill(0,0);
  shape(p,100,100);
}

any help greatly appreciated


回答1:


Yes, you can use PShape's disableStyle() to disable it's rendering style and use Processing's (your sketches'):

PShape p;

void setup()
{
  size(600, 600, P2D);
  p = createShape();
  p.beginShape();
  p.vertex(0, 0);
  p.vertex(20, 0);
  p.vertex(20, 20);
  p.vertex(0, 20);
  p.endShape(CLOSE);
  //disable the PShape's default styles and use Processing's 
  p.disableStyle();
}

void draw()
{
  background(255);
  for(int i = 0 ; i < 30 ; i++){
      fill(i/30.0*255, 100, 30);
      shape(p, i * 20,300);
  }
}

For such a simple shape you can of course use rect(), but I assume that's a place holder for something more complex. Other things to explore are beginShape() and maybe createGraphics()



来源:https://stackoverflow.com/questions/18904080/is-there-a-way-to-change-the-color-of-pshape-without-entering-begin-end

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!