posX = mouseX-posX+mouseX;
posY = mouseY-posY+mouseY;
are my attempt, to position the element at the position, where the user actually clicked the square rather then just drawing the square at the current mouse position such as,
posX = mouseX;
posY = mouseX;
I would be very thankful for any help,
- int posX = 20; // X Position of square
- int posY = 20; // Y Position of square
- int widthX = 20; // X Position of square
- int heightY = 20;
- boolean dragg = false;
- void setup(){
- size(100,100);
- background(0);
- stroke(255);
- }
- void draw(){
- if(dragg == true){
- background(255);
- stroke(0);
- }
- rect(posX,posY,20,20);
- }
- void mousePressed(){
- if(mouseX >= posX && mouseX <= posX + widthX && mouseY >= posY && mouseY <= posY + heightY){
- print(mouseX);
- print(" : ");
- println(mouseY);
- dragg = true;
- background(100,100,100);
- }
- else{
- dragg = false;
- background(0);
- print(dragg);
- }
- }
- void mouseDragged(){
- if(dragg == true){
- posX = mouseX-posX+mouseX;
- posY = mouseY-posY+mouseY;
- }
- }
1