0

I am trying to animate a rectangle moving up and down on each button when the button is clicked like an indicator which button is active. But i don't like the idea of it just showing on each button as i had it done before i would like to animate it moving slowly from each button to another. So i tried doing it very simply bu changing the Y "coordinates" layout of the rectangle every 10mili seconds which would finally look like the rectangle is moving up or down depending on the for loop executed. But what happens is when a button is clicked there is longer wait time without the rectangle moving and then it appears on the place it is supposed to be at once. Looks like the Thread.sleep() is executed at once as much times as it should be and afterwards the setLayoutY() function is executed just once. Here is the code

 public void moveRectangle(double newY) {
    if (statisticsRectangle.getY() > newY)
        for (double i = statisticsRectangle.getY(); i >= newY; i--) {
            statisticsRectangle.setLayoutY(i);
            try {
                Thread.sleep(10);
            }
            catch (InterruptedException e){}
        }
    else
        for (double i = statisticsRectangle.getY(); i <= newY; i++) {
            statisticsRectangle.setLayoutY(i);
            try{
                Thread.sleep(10);
            }
            catch (InterruptedException e){}
        }
}

public void addMemberButton() {
    moveRectangle(60);
}
  • you can try `Thread.yield();` before sleep. – Vadim Oct 01 '18 at 10:20
  • 2
    Are you sleeping on the event dispatcher thread? I think you should enhance your question and give a [mcve]. – GhostCat Oct 01 '18 at 10:22
  • 1
    Most UI Frameworks will only draw the new state once your method has returned, and so what you see is the state before you enter the method, and then the one after all the sleeping is done. All the steps inbetween are invisible – zapl Oct 01 '18 at 10:25

0 Answers0