试图为我的绘图应用程序更改画笔的颜色

时间:2016-09-25 21:23:00

标签: colors processing

我尝试使用Processing 3.2.1创建一个绘图程序,以便当我按任意数字1 - 0时,颜色分别根据分配给该键的颜色而变化。我面临的问题是,我似乎无法弄清楚它为什么不起作用。到目前为止,这是我的代码。

float value = 255;
color c1 = color(0,0,0);
float e = 5;
float thickness = 1;
float max = 6;

void setup(){
  size (720,480);
  frameRate (120);
  background (255);
  fill (c1);
  stroke (c1);
}

void draw() 
{ 
 if(mousePressed) 
{ 
  if(thickness < max) 
    { 
    strokeWeight(thickness); 
    line(mouseX, mouseY, pmouseX,pmouseY); 
    thickness = thickness+0.25; 
    }
   else 
   { 
     line(mouseX, mouseY, pmouseX,pmouseY);
     strokeWeight(max);
   }
} 
}
void mouseReleased() 
{ 
thickness = thickness/thickness; 
}


void keyPressed (){
    if (key == '1') {
      c1 = color(255,0,0); //red
    }
    if (key == '2') {
      c1 = color(0,255,0); //green
    }
    if (key == '3') {
      c1 = color(0,0,255); //blue
    }
    if (key == '4') {
      c1 = color(255,255,0); //yellow
    }
    if (key == '5') {
      c1 = color(255,0,255); //magenta
    }
    if (key == '0') {
      c1 = color(255);
    }
    if (key == '9') {
      c1 = color(0);
    }
    if (key == '=') {
      e = e + 3;
      if (e > 100){
        e = 100;
      }
    }
    if (key == '-') {
      e = e - 3;
      if (e < 1){
        e = 1;
      }
    }
}

1 个答案:

答案 0 :(得分:0)

现在,您只需在fill()功能中设置颜色(通过调用stroke()setup()功能)一次。即使您正在更改c1变量,也不会神奇地改变颜色。

要解决此问题,您需要在更改fill()中的颜色时调用stroke()keyPressed()函数,或者可以在draw()函数中调用它们

另请注意,如果您不想使用不同颜色的轮廓,则可以致电noStroke()