Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 3-4: Drawing a continuous line
void setup() {
  size(200, 200);
  background(255);
  smooth();
}

void draw() {
  stroke(0);
  // Draw a line from previous mouse location to current mouse location.
  line(pmouseX, pmouseY, mouseX, mouseY);
}
  • Rupert

    What would be the easiest way to get the line to start from a default point that was different from (0,0)?

  • http://www.learningprocessing.com Daniel Shiffman

    Processing always initializes the mouse location at 0,0 for the first frame. You could check and make sure the values are not 0,0 before you draw:

      if (!(pmouseX == 0 && pmouseY == 0)) {
        line(pmouseX, pmouseY, mouseX, mouseY);
      }
    

    And even use your own variable to set up another starting point for when the values are 0,0.

  • oliver kalbermatter

    How can I draw a continuous line with the background in the draw section?

  • shiffman

    you’ll have to store all the points over time, you can use an array or ArrayList.

  • oliver kalbermatter

    thanks :) do I have to make a class where I draw the curent mousepos with an ArrayList?

  • oliver kalbermatter

    //I am stuck :( what am I doing wrong?

    // Declare and construct two objects (h1, h2) from the class HLine

    HLine h1 = new HLine(20, 2.0);

    HLine h2 = new HLine(50, 2.5);

    void setup()

    {

    size(200, 200);

    frameRate(30);

    drawing = new ArrayList(); // Create an empty ArrayList

    }

    void draw() {

    background(204);

    drawing.add(new Line(mouseX, mouseY, pmouseX, pmouseY)); // Start by adding one element

    //h1.update(mouseX,mouseY,pmouseX,pmouseY);

    //array list wird gefüllt bis unendlich

    for (int i = drawing.size()-1; i >= 0; i–) {

    Line line = drawing.get(i);

    line.display();

    if (line.finished()) {

    // Items can be deleted with remove().

    line.remove(i);

    }

    }

    }

    class Line {

    float ym, xm, ypm, xpm;

    HLine (float x, float y, float px, float py) {

    ym = y;

    xm = x;

    ypm = py;

    xpm = px;

    }

    void display() {

    line(ym, xm, ypm, xpm);

    }

    }