Dynamically Change Strokeweight for lines already drawn

edited November 2013 in How To...

Hello, I am trying to make a simple line drawing application, where a line is drawn from one number (from an OSC program) to another, while a toggle button is pushed. I am wondering if there is a way to change the strokeWeight for lines I have already drawn. So, if a line is drawn (which I realize is a series of points to points) with strokeWeight(1), I could then go back and change the thickness (strokeWeight()) of that specific line.

The ultimate goal is to get the thickness of lines to change to music frequencies (like a graphic eq). I have the eq numbers, now I just need to be able to change the line thickness. Thanks

Answers

  • edited November 2013 Answer ✓

    hello,

    you would need to store the lines in an arraylist and draw them from there.

    To draw a new line, it is stored in the arraylist. To update the screen use background(0); and then draw the complete arraylist to the screen, either with a global strokeWeight for all or each strokeWeight individually.

    To do so, you could make a class Line and have an arraylist of type Line. (all pseudocode, more or less) [edited]

    class Line {
    float lineFromX, lineFromY;
    float lineToX, lineToY;
    int lineThickness;
    color lineColor; 
    
    // ....
    // constructor
    // .....
    
    void display() {
    strokeWeight(lineThickness);
    stroke(lineColor);
    line ( lineFromX,lineFromY,
    lineToX,lineToY ); 
    strokeWeight(1); // back to normal
    } // method 
    
    } // class 
    

    in draw ()

    void draw() {
    background(0);
    for (int i=0; i < lines.size(); i++ ) { 
    Line currentLine = lines.get(i);
    currentLine.display(); 
    } // for 
    } // func
    

    Greetings, Chrisir

  • edited November 2013 Answer ✓

    this is the idea:

    real code...

    ArrayList<Line> lines;
    
    void setup() {
      size(800, 800);
      lines = new ArrayList();
      lines.add(new Line());
      lines.add(new Line( 13, 122, 
      33, 444, 
      26, 
      color(0, 0, 255)  ));
      frameRate(12);
    } // func 
    
    void draw() {
      background(0);
      for (int i=0; i < lines.size(); i++ ) {
        Line currentLine = lines.get(i);
        currentLine.lineThickness=frameCount % 10;
        currentLine.display();
      } // for
    } // func
    
    // =============================================
    
    class Line {
      float lineFromX=100, lineFromY=100;
      float lineToX=300, lineToY=200;
      int   lineThickness=15;
      color lineColor=color(123, 12, 0);
    
      // constructor I:
      Line (float lineFromX, float lineFromY, 
      float lineToX, float lineToY, 
      int lineThickness, 
      color lineColor) {
        this.lineFromX = lineFromX;
        this.lineFromY = lineFromY;
        this.lineToX = lineToX;
        this.lineToY = lineToY;
        this.lineThickness = lineThickness;
        this.lineColor = lineColor;
      } // constructor I
    
      // constructor II:
      Line () {
        // empty
      } // constructor II
    
      // 
      void display() {
        strokeWeight(lineThickness);
        stroke(lineColor);
        line ( lineFromX, lineFromY, 
        lineToX, lineToY );
        strokeWeight(1); // back to normal
      } // method
    } // class
    
  • edited November 2013

    Thank you so much. I knew an array was the answer but this is the guidance I needed to get started. I will let you know what I come up with, thanks again!

Sign In or Register to comment.