error code help plz (-ve strokeweight)

Hi there,

I'm really new to processing and honestly not very good at coding but am trying to learn. I'm trying to make a simple paint type of thing and keep getting an error because my stroke weight is going below zero. My question would be how to stop that from happening.

float px;
float py;
float strokeW = 1;

//draws once
void setup() {
  size(400, 400);
  background(255);
}

//draws everyframe
void draw() {
  px = pmouseX;
  py = pmouseY;


  //draws my line(paint brush)
  if (mousePressed) {
    stroke(10);
    fill(0);
    line(mouseX, mouseY, px, py);
  }

  //this increases the size of line
  if (keyPressed) {
    if (key == ']' || key == '}') {
      //created a variable for strokeWeight and added
      //strokeWeight = itself plus 1 for increaseing the size
      strokeW++;
    }

  //this decreases the size of my line
    if (keyPressed) {
      if (key == '[' || key == '{') {
        //opposite process for decrease
        //but i do have a problem because i can decrease the size
        //of the line until it is gone and the program freezes
        strokeW--;
      }
    }
  }
}

and i tried adding this to stop it but no luck yet

 if (strokeW < 1) {
          strokeWeight(1);
        }

???

Thanks

Answers

  • Answer ✓

    I made a few changes to your sketch, the most important one is checking that strokeW is greater than 1.0 before you make it smaller:

    float strokeW = 1;
    
    void setup() {
      size(400, 400);
      background(255);
    
      // This only needs to happen once, moved to setup()
      fill(0);
      stroke(0);
    }
    
    void draw() {
      if (mousePressed) {
        // strokeWeight based on strokeW
        strokeWeight(strokeW);
    
        // You don't need px and py, just use pmouseX and pmouseY
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
    
      if (keyPressed) {
        if (key == ']' || key == '}') {
          strokeW++;
        }
        // You can place this inside "if (keyPressed)" as well
        // instead of having two of the same condition
        else if (key == '[' || key == '{') {
          // You have to limit strokeW from going below 1
          if (strokeW > 1.0)
            strokeW--;
        }
      }
    }
    

    I think that the rate of growing / shrinking is very fast. Your strokeW is a float, you could make it grow / shrink at a rate slower than 1.0 if you wanted to. For example, add / subtract 0.25 instead of doing strokeW++ and strokeW--

  • edited September 2015

    thanks GoToLoop and asimes for your help!!!!! really appreciate it!!

  • // forum.processing.org/two/discussion/12702/error-code-help-plz
    // 2015-Sep-27
    
    static final float MIN = 1.0, MAX = 10.0, STEP = 1.0;
    float sw = MIN;
    
    void setup() {
      size(400, 400, JAVA2D);
      smooth(4);
    
      noLoop();
      frameRate(30);
    
      background(-1);
      stroke(0);
    }
    
    void draw() {
      strokeWeight(sw);
      line(mouseX, mouseY, pmouseX, pmouseY);
    
      frame.setTitle("StrokeWeight = " + sw);
    }
    
    void keyPressed() {
      final int k = keyCode;
    
      if      (k == ']' | k == '}')  sw = min(sw + STEP, MAX);
      else if (k == '[' | k == '{')  sw = max(sw - STEP, MIN);
      else if (k == ' ')             background((color) random(#000000));
    
      redraw = true;
    }
    
    void mouseMoved() {
      redraw = true;
    }
    
  • Thank you so much!

Sign In or Register to comment.