We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Drawing tool using rubber band rectangle
Page Index Toggle Pages: 1
Drawing tool using rubber band rectangle? (Read 1153 times)
Drawing tool using rubber band rectangle?
Sep 24th, 2009, 8:09am
 
Hi, I am trying to write a drawing tool using rubber band rectangle
so far the code below can draw rubber band rectangle, but rectangle does not stay when mouseReleased. Is there any way to modify the code so that the user can draw rubber band rectangles as his wish (like other design software)? thanks a lot for help.

int canvas_width=800;
int canvas_height=600;
int [] rect_x1= new int[1]; // catch the start dragging point x
int [] rect_y1= new int[1]; // catch the start dragging point y
int [] rect_x2 = new int[canvas_width]; // record  moving mouseX
int [] rect_y2 = new int[canvas_height]; // record moving mouseY
int [] rect_x3= new int[1]; // record mouseX releasing point
int [] rect_y3= new int[1]; // record mouseY releasing point.



void setup() {

size(canvas_width,canvas_height);

}

void draw() {
background(255);
smooth();
draw_rect();

}

void draw_rect() {
 if (mousePressed== true){
float sizex= rect_x2[0]- rect_x1[0];
float sizey= rect_y2[0]- rect_y1[0];
strokeWeight(.4);

rect(rect_x1[0], rect_y1[0], sizex,sizey);
fill(100,30);
 } else{
    float sizex1= rect_x3[0]- rect_x1[0];
    float sizey1= rect_y3[0]- rect_y1[0];
    rect(rect_x1[0], rect_y1[0], sizex1, sizey1);
    fill(100,30);
    strokeWeight(1.5);
 }

}

void mousePressed() {
rect_x1[0]= mouseX;
rect_y1[0]= mouseY;
}

void mouseDragged() {
rect_x2[0]= mouseX;
rect_y2[0]= mouseY;

}

void mouseReleased() {
  rect_x3[0]= mouseX;
  rect_y3[0]= mouseY;
}



Re: Drawing tool using rubber band rectangle?
Reply #1 - Sep 24th, 2009, 8:38am
 
I simplified a bit your sketch (no need for arrays of 1 entry, unless you plan to expand them later) and well, as a result it seems to work as you wish... Note: avoid putting sketch size in variables.
Code:
int rect_x1; // catch the start dragging point x
int rect_y1; // catch the start dragging point y
int rect_x2; // record  moving mouseX
int rect_y2; // record moving mouseY
int rect_x3; // record mouseX releasing point
int rect_y3; // record mouseY releasing point.

void setup() {
 size(800, 600);
 smooth();
}

void draw() {
 background(255);
 draw_rect();
}

void draw_rect() {
 if (mousePressed) {
   float sizex = rect_x2 - rect_x1;
   float sizey = rect_y2 - rect_y1;
   strokeWeight(.4);
   rect(rect_x1, rect_y1, sizex,sizey);
   fill(100,30);
 } else{
   float sizex1 = rect_x3 - rect_x1;
   float sizey1 = rect_y3 - rect_y1;
   rect(rect_x1, rect_y1, sizex1, sizey1);
   fill(100, 30);
   strokeWeight(1.5);
 }
}

void mousePressed() {
 rect_x1 = mouseX;
 rect_y1 = mouseY;
}

void mouseDragged() {
 rect_x2 = mouseX;
 rect_y2 = mouseY;
}

void mouseReleased() {
 rect_x3 = mouseX;
 rect_y3 = mouseY;
}


Looking more attentively at the code, I see it can be simplified even more:
Code:
// Same code above, but get rid of x3/y3

void draw_rect() {
float sizex = rect_x2 - rect_x1;
float sizey = rect_y2 - rect_y1;
if (mousePressed) {
strokeWeight(.4);
} else{
strokeWeight(1.5);
}
fill(100, 30);
rect(rect_x1, rect_y1, sizex,sizey);
}

void mousePressed() {
rect_x1 = mouseX;
rect_y1 = mouseY;
mouseDragged(); // Reset vars
}

void mouseDragged() {
rect_x2 = mouseX;
rect_y2 = mouseY;
}
Re: Drawing tool using rubber band rectangle?
Reply #2 - Sep 24th, 2009, 8:54am
 
Thank you so much for help, so far the code can only draw one rectangle at one time. When drawing second rectangle, first rectangle will  be erased. is there anyway to keep previous rectangle? thanks.
Re: Drawing tool using rubber band rectangle?
Reply #3 - Sep 24th, 2009, 9:12am
 
In that case, they are no more "rubber band rectangles"... You aim at some kind of drawing tool?
You can indeed declare arrays of coordinates and maintain an index on the next one, hoping user doesn't draw too many rectangles (or using Processing auto-expanding arrays, speed isn't critical here).
Or you can look at using classes of shapes, that you ease a bit the storing and drawing of shapes and would allow something more generic.
Page Index Toggle Pages: 1