【问题标题】:Trying to change color of paintbrush for my drawing app尝试为我的绘图应用更改画笔颜色
【发布时间】:2016-09-25 21:23:00
【问题描述】:

我正在尝试使用 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;
      }
    }
}

【问题讨论】:

  • 您能否澄清一下“它不起作用” 的含义?你有错误吗?程序没有达到你的预期吗? ...
  • 我发现了我的问题。事实证明,我应该在 void draw() 中进行填充和描边,而不是 void setup() :)
  • 我的问题是线条的颜色没有改变颜色。然后我意识到我的问题出在哪里有填充语句

标签: colors processing


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    相关资源
    最近更新 更多