Moving and cutting

Hey Guys,
I’m trying to do some demo project. What I’d like to do is draw some lines and a rectangle. Those part of the lines will be invisible which are out of the rectangle. I give the end-coordinates of the lines by clicking. I’m using noLoop() because I want to draw the line just before the second end-coordinate is given.
1

  void setup() {
    size(800, 800);
    noLoop();
  }

I want to move the rectangle by pressing a key. Because of the noLoop() function the KeyPressed() function is not working. If I delete the noLoop() the processing keeps drawing lines as I’m moving the mouse.

void mouseClicked() {
  redraw();
} 
void keyPressed(){
if(key==CODED){
  if(keyCode==UP){
    y=y-100;
  }
  if(keyCode==DOWN){
    y=y+100;
  }
  if(keyCode==LEFT){
    x=x-70;
  }
  if(keyCode==RIGHT){
    x=x+70;
  }
  }
}

Is there any solution on this?

1 Like

A sketch w/o draw() simply doesn’t go beyond setup(). :fearful:
Even an empty callback draw() is enough to turn a sketch interactive: :smile_cat:

void draw() {
}

Exactly as GoToLoop says. And you can always say something like

if (!redrawNeeded) { return; }

at the top of your draw() so that draw() only does some drawing when you want it to.

Actually this part of code looks like this:

  void setup() {
    size(800, 800);
    background(100,10,10);
    noLoop();    
  }  
void draw(){
      if ( (frameCount & 1) == 0 ) {
      point(mouseX, mouseY);
      return;
      } 
      strokeWeight(1);
      DrawARect(x3, y3, x4, y4); //it's a custom function which draws a rectangle
      cuting_F(pmouseX, pmouseY, mouseX, mouseY); // it cuts out the part of the lines which are outside the rect.
//and a bunch of code goes here...
}

:smile:

1 Like

You’re lacking redraw() in callback keyPressed() then. :grimacing:

2 Likes

You hit the nail on the head, thanks! :wink:

How can I remove the “previous” rects?

I’d to store the lines even if they are not visible now, and unhide the parts ofthem if the rectangle is moved into the right spot. Do you have any idea how to do it?

Maybe you should consider drawing in separate PGraphics objects, so you can clear() 1 w/o affecting the other: :bulb:
Processing.org/reference/createGraphics_.html

It looks like I can not use PGraphics in case of custom methods.