How to swap the positions of 2 rectangles

edited January 2018 in How To...

So I'm trying to do a thing where I want to swap the positions of 2 rectangles when I click on them, but I can't figure out how to do that.

Answers

  • edited January 2018

    when one has been clicked set a marker isSwapped to true in mousePressed():

    void mousePressed(){
        isSwapped=true;
    }
    

    before setup() say

    boolean isSwapped=false; 
    

    depending on isSwapped draw the rects in draw() as they are initially or in a new way

    if(isSwapped){
        rects...
    }else {
        rects...
    }
    

    Chrisir

  • edited January 2018

    But how do I get the coordinates of the rectangles I want to swap to redraw them in each other's previous positions? I have several rectangles that I want the user to be able to swap with each other.

  • it might help to show your entire code so that we know whet you mean

  • to swap two you mark first one rectangle and then the next??

  • Please don't start multiple threads for the same question. I'm deleting the other one, because you have answers here. But here's the code from over there

    class Person{
      int xinit;
      int yinit;
      String nom;
      int xpos;
      int ypos;
      int xbut;
      int ybut;
      int count;
      color borderColor;
    
      Person (int iyinit,String inom,int iybut){
        xinit=700;
        yinit=iyinit;
        nom=inom;
        ypos=iyinit;
        ybut=iybut;
        count=0;
        borderColor=#000000;
    }
    
      void mouseClicked(){
        if (mouseX>=xinit && mouseX<=800){
        count+=1;
          if (count<=2){
            borderColor=#ffff00;
          }
          if (count==2){
            ypos=
            count=0;
           borderColor=#000000;
        }
        }
      }
    
      void draw(){
        stroke(borderColor);  
        rect(xinit,ypos,100,50);
    
      }
    }
    
  • sorry, I thought it might be better to start a more precise thread and it says that asking help with code is for the other category. my bad, 1st time here

  • You can edit and change the category...

  • edited January 2018

    so, to clarify, I wanted to make a class for rectangles with names inside of them and I want the user to be able to click on the rectangles to swap them. I also tried to make it so that it "highlights" the chosen rectangles. The code is pretty incomplete for now. Also, I'm a bit new programming

  • Ah, didn't know that koogs, sorry!

Sign In or Register to comment.