hello, i am a super beginner at processing and java, but i have a reasonable background at C language, so i know some of the concepts of programing, but i decided learn something new, and i am trying to learn processing.
so, my problem today is this:
-i want to draw 2 (or more) rectangles on the window, and move it based on:
-if the mouse is pressed;
AND
-if the mouse X and Y its inside of the rectangle
so based on this i created the following program, but it has some bugs.
1- the 2 rectangle it can only be moved if i did not realesed the mouse out of the initial ractangle limits (its hard to explain)
2- the 2 rectangle , after being initialized, it will have the the X and Y always equals
3- if i cliked down of the limits of the 1 rectangle it will move, wich it is not suposed.
4-and others…
its weird that they are the same type of object but diferent bugs, its confusing me.
main
Retangulo quadr = new Retangulo();
Retangulo quadr2 = new Retangulo();
void setup(){
size(1800,1000);
quadr.incia_rect(0,0,255, 50, 50);
quadr2.incia_rect(0, 255, 0, 300, 330);
}
void draw(){
background(255);
quadr.move_rect();
quadr2.move_rect();
//i was trying to debug the program with this, viewing the current position of each variable, but it seems that everything its ok :((
println(mouseX, mouseY, quadr.xpos, quadr.ypos, quadr2.xpos, quadr2.xpos, quadr.f, quadr2.f);
}
//with this conditions i assume that the rectangle will be 100*50
//it has one condition for each object, (i dont know if this is the best way(or even a way xD) to do this, because i am still a noob :) )
void mousePressed() {
if(mouseX >= quadr.xpos & mouseX <= quadr.xpos+100 & mouseY >= quadr.ypos & mouseX <= quadr.ypos+50){
quadr.f=true;
}
if(mouseX >= quadr2.xpos & mouseX <= quadr2.xpos+100 & mouseY >= quadr2.ypos & mouseX <= quadr2.ypos+50){
quadr2.f=true;
}
}
void mouseReleased(){
quadr.f=false;
quadr2.f=false;
}
retangulo
class Retangulo{
float xpos; //corrent x position of the rectangle
float ypos; //corrent y position of the rectangle
boolean f=false; //this variable it is to true when the mouse is pressed and the current positions of mouse are in beetween of the rectangle limits
int clr; //variables used to color the rectangle
int clg;
int clb;
//funtion the initiates the rectangle in begining of the program
//xinc and yinc are intial the positions of the rectangle
void incia_rect(int r, int g, int b, float xinc, float yinc){
fill(r,g,b);
stroke(255, 0, 0);
rect(xinc,yinc,100,50);
xpos=xinc;
ypos=yinc;
clr=r;
clg=g;
clb=b;
}
//this fuction its to move the rectangle while the condition its true
void move_rect(){
if(f==true){
fill(0,0,255);
stroke(255, 0, 0);
rect(mouseX,mouseY,100,50);
xpos=mouseX;
ypos=mouseY;
}else{
fill(0,0,255);
stroke(255, 0, 0);
rect(xpos,ypos,100,50);
}
}
}