drag corner to resize box

Hi everyone,

I'm trying unsuccessfully to build a prototype of resizeable box. As a first step I'd like to be able to drag the bottom right corner to have a bigger box. The dragging part of the code is working but I am not able to leave the bigger box on the screen when I release the mouse button. Here's the code:

void setup(){
  size(800,500);
  strokeWeight(4);
  stroke(0);
  smooth();
}

void draw(){
  background(0);

  if(mousePressed && mouseX > 200 && mouseY > 200){
    rect(100,100,mouseX-100,mouseY-100);
  } else {
    rect(100,100,100,100);
  }
}

Thanks in advance for any help.

Luca

Answers

  • Answer ✓

    upper left corner: drag the box around

    Lower right corner: resize

    ;-)

    ClassRect lines [] = new ClassRect [1];  
    
    boolean take1, take2; 
    
    void setup()
    {
      size(640, 560);
      background(111);
      lines [0] = new ClassRect  (  111, 122, 222, 333  );
    }
    
    void draw()
    { 
      background(111);
    
      stroke(0);
    
      text ("upper left corner: drag the box around \n"
        +"Lower right corner: resize ", 30, 30);
    
      lines [0].draw();
    
      if (take1) {
        lines[0].x1=mouseX;
        lines[0].y1=mouseY;
      }
    
      if (take2) {
        lines[0].rectWidth=mouseX-lines[0].x1;
        lines[0].rectHeight=mouseY-lines[0].y1;
      }
    } // func 
    
    void mousePressed() {
      // upper left corner 
      if (dist (lines[0].x1, 
      lines[0].y1, mouseX, mouseY) < 12) {
        take1=true;
      }
      // Lower right corner 
      else if (dist (lines[0].x1+lines[0].rectWidth, 
      lines[0].y1+lines[0].rectHeight, mouseX, mouseY) < 12) {
        take2=true;
      }
    }
    
    void mouseReleased() {
      take1=false;
      take2=false;
    }
    
    // =======================================
    
    class ClassRect {
    
      float x1, y1, // POS 
      rectWidth, rectHeight;       // SIZE 
    
      ClassRect (float x1_, float y1_, 
      float rectWidth_, float rectHeight_) 
      {
        // constr 
        x1=x1_;
        y1=y1_;
    
        rectWidth=rectWidth_;
        rectHeight=rectHeight_;
      } // constr
    
      void draw() {
        rect(x1, y1, rectWidth, rectHeight);
      }// method
    } // 
    
    //
    
  • this is incredible, thank you Chrisir! fast and perfect :)>- . Now I go to study the code...

Sign In or Register to comment.