Loading...
Logo
Processing Forum
I'm trying to make a reader/writer that records what is drawn in one program and plays it back in the next. No problem, except I can't figure out how to pause the output to text file when the mouse is not being pressed. This causes really ugly jumps in the drawing on playback. What piece of the puzzle am I missing here? 

Copy code
  1.  void mouseDragged(){

  2.   output.println(mouseX + "$" + mouseY); // Write the coordinate to the file
  3.   line(mouseX-lRectX,mouseY-rectRy,pmouseX-lRectX,pmouseY-rectRy);
  4.   
  5.  }
  6.  
  7. void mouseReleased(){

  8.  output.println(pmouseX + "$" + pmouseY);
  9.  //this didn't work
  10.  
  11.   }

Replies(9)

You can stop the draw() continuous callback by issuing -> noLoop();
And re-activate it again w/ loop()!

Take a look at this post to see an example of it:
I don't understand your problem: when you release the mouse button, mouseDragged is no longer called, no?
If your problem is that your disconnected lines are connected, you should write something different, indicating a mouse release, that you must interpret when playing back the record.

Yes, that's the problem- and the question. How to write that break  and have the reader then interpret it. Right now, the reader reads this:

Copy code
  1.     String[] pieces = split(line,"$");
  2.     int x = int(pieces[0]);
  3.     int y = int(pieces[1]);
  4.  line(x,y,px,py);
  5.     px=x;
  6.     py=y;

I can't quite get my head around how to make it interpret the break so that it does not connect the lines. 
Copy code
  1.  void mouseDragged(){
  2.   output.println(mouseX + "$" + mouseY); // Write the coordinate to the file
  3.   line(mouseX-lRectX,mouseY-rectRy,pmouseX-lRectX,pmouseY-rectRy);
  4. }
  5.  
  6. void mouseReleased(){
  7.  output.println(pmouseX + "#" + pmouseY);
  8. }
  9.  
  10. // Usage
  11. if (line.contains("#")) {
  12.   String[] pieces = split(line,"#");
  13.   int x = int(pieces[0]);
  14.   int y = int(pieces[1]);
  15.   // Memorize but don't draw the line
  16.     px=x;
  17.     py=y;
  18. } else {
  19.   String[] pieces = split(line,"$");
  20.   int x = int(pieces[0]);
  21.   int y = int(pieces[1]);
  22.   line(x,y,px,py);
  23.     px=x;
  24.     py=y;
  25. }
Actually, you can put the px=x lines after the condition, since they are common to both branches. I leave them as is for better understanding...
I appreciate the help, and it made sense, but I'm getting the same problem still. 
I suggest to show your full code, or, if too long, to make a simplified version of it, illustrating the issue, so we can run it and see what goes wrong.
I feel like I'm overlooking something obvious here. Help greatly appreciated! The reader is still connecting lines from the last x and y- even when the mouse if not being dragged. That's what I don't want. I think you will see if you run it. Here are the simplified files: 


testWriter.pde:
Copy code
  1. PrintWriter output;

  2. void setup(){
  3.   size(600,400);
  4.   background(255);
  5.   output=createWriter("../testData/test.txt");
  6.  }

  7. void draw(){
  8.   smooth();
  9.  }
  10.  
  11.  void mouseDragged(){
  12.     output.println(mouseX + "$" + mouseY); // Write the coordinate to the file
  13.     line(mouseX,mouseY,pmouseX, pmouseY);
  14.   }
  15.  
  16. void mouseReleased(){
  17.   output.println(mouseX + "#" + mouseY);
  18.   }
  19.  
  20. void keyPressed() {
  21.   println("output");
  22.   output.flush(); // Writes the remaining data to the file
  23.   output.close(); // Finishes the file
  24.   exit(); // Stops the program
  25. }
  • testReader:

  • Copy code
    1. BufferedReader reader;
    2. String line;
    3. int px,py;

    4. void setup(){
    5.   size(600,400);
    6.   background(255);
    7.   reader = createReader("../testData/test.txt");  
    8. }

    9. void draw(){
    10.   try {
    11.     line = reader.readLine();
    12.   } catch (IOException e) {
    13.     e.printStackTrace();
    14.     line = null;
    15.   }
    16.   if (line == null) {
    17.     noLoop();  
    18.   } else {
    19.    if (line.contains("#")){
    20.       String[] pieces = split(line,"#");
    21.       int x = int(pieces[0]);
    22.       int y = int(pieces[1]);
    23.       println("Splitting at #");
    24.       px=x;
    25.       py=y;
    26.      //tried deleting, making other way around
    27.     }
    28.      else{
    29.       ///problem here. Still draws from the last location
    30.     String[] pieces = split(line,"$");
    31.     int x = int(pieces[0]);
    32.     int y = int(pieces[1]);
    33.      line(x,y,px,py);
    34.      px=x;
    35.      py=y;
    36.     }
    37.   }
    38. }

  • OK, I see the problem... Replace mouseReleased() by mousePressed() in the writer.
    AWESOME! THANKS! So simple.