Why won't stroke color change

I have my code set up so that I can draw on my sketch, but I want to use if statements where if clicking within certain perimeters the stroke size and color will change of the brush. I thought that what I had would create that, but it doesn't seem to affect anything. Any help would be appreciated.

Here is the code:

            int Yellow;
            int Red;
            int Green;
            int Blue;
            int White;
            int Black;


            void setup()

            {
              size(600, 600);
              background(255);

              Yellow = color(255, 255, 0);
              Red = color(255, 0, 0);
              Green = color(0, 255, 0);
              Blue = color(0, 0, 255);
              White = color(255);
              Black = color(0);
            }

            //interface
            void draw()
            {
              stroke(1);
              smooth();


              fill(Yellow);
              rect(20, 20, 20, 20);
              fill(Red);
              rect(50, 20, 20, 20);
              fill(Green);
              rect(80, 20, 20, 20);
              fill(Blue);
              rect(110, 20, 20, 20);

              fill(255);
              rect(140, 20, 20, 20);    //the eraser

              //stroke size and erase
              fill(255);
              rect(20, 50, 20, 20);

              stroke(2);
              line(20, 60, 40, 60);

              fill(255);
              rect(20, 80, 20, 20);

              stroke(4);
              line(20, 90, 40, 90);

              fill(255);
              rect(20, 110, 20, 20);

              line(20, 110, 40, 130);
              line(40, 110, 20, 130);
            }

            void mouseDragged()
            {
              line(pmouseX, pmouseY, mouseX, mouseY);
              stroke(1);
              fill(255, 0, 0);
            }


            //here set boundries
            {
              if (mousePressed) 
              {
                if (mouseY > 20 && mouseY < 40)
                {
                  if (mouseX >20 && mouseX < 40)
                  {
                    stroke(Yellow);
                  }
                  if (mouseX>50 && mouseX < 70)
                  {
                    stroke(Green);
                  }
                  if (mouseX>80 && mouseX<100)
                  {
                    stroke(Blue);
                  }

                  if (mouseX>110 && mouseX<130)
                  {
                    stroke(Black);
                  }
                  if (mouseX>140 && mouseX<160)
                  {
                    stroke(White);
                  }
                }
              }
            }
Tagged:

Answers

  • edited February 2014
  • The code after //here set boundries is outside any function, so it won't be run. Perhaps you have missing a void mousePressed() before this block (and in this case, there is no need for if (mousePressed)

  • edited February 2014

    try this ..

    color[] c = { 
      -1, #FF0000, #10FF00, #0017FF, #FFF300
    }; 
    color pick = color(-1);
    int w = 20,st;
    void setup() {
      size(600, 600);
      background(-1);
    }
    
    void draw()
    {
      smooth();
      for (int i=0;i<c.length;i++) {
        fill(c[i]);
        stroke(0);
        strokeWeight(1);
        rect(i*20+20, 20, w, w);
        if ( i*20+20 < mouseX && mouseX< i*20+20 + w && 20 < mouseY && mouseY< 20 + w && mousePressed) {
          pick = c[i];
          mousePressed = false;
          if(i==0) st = 10;
          else st = 1;
        }
      }
    }
    
    void mouseDragged()
    {
      stroke(pick);
      strokeWeight(st);
      line(pmouseX, pmouseY, mouseX, mouseY);
    }
    
Sign In or Register to comment.