JavaFx:从循环动画,所有动画一次性发生

时间:2017-02-18 08:09:48

标签: loops sorting animation javafx bubble-sort

我正在开发一个排序应用程序,它显示静态数组的冒泡排序动画。

int arr[] ={6,5,3,2,4,1};

我面临的问题是, 当我从冒泡排序函数调用animateCircle函数时,所有动画(应该发生的多个转换一次发生)。我尝试过使用SequenceTransition,ParallelTransition,PauseTransition。但是我仍然无法在交换动画发生时让程序暂停。

animateCircle函数

private void animateCircle(Circle fromC, Circle toC, Double fromD, Double toD){

    TranslateTransition translateTransition = new TranslateTransition();
    translateTransition.setNode(fromC);
    translateTransition.setDuration(Duration.millis(1000));
    translateTransition.setToX(toD);

    TranslateTransition translateTransition2 = new TranslateTransition();
    translateTransition2.setNode(toC);
    translateTransition2.setDuration(Duration.millis(1000));
    translateTransition2.setToX(fromD);

    //translateTransition.play();

    //pauseTransition.setDuration(Duration.seconds(5));

    parallelTransition = new ParallelTransition(translateTransition, translateTransition2);

    //seq = new SequentialTransition(parallelTransition, pauseTransition);

// pauseDelay++;
//        translateTransition.setOnFinished(new EventHandler<ActionEvent>() {
//
//            @Override
//            public void handle(ActionEvent event) {
//              //translateTransition2.play();
//              System.out.println("Done");
//            }
//        });

//        return true;


    parallelTransition.setOnFinished((ActionEvent e) -> {

        pauseTransition.play();
        System.out.println("IN !!");

    });

    parallelTransition.play();

}

我尝试的其他方式被评论。

bubbleSort功能

void bubbleSort(int[] arr,Circle[] circleArray, double[] circleX, Label[] labelArray, double[] labelX) {  
    int n = arr.length;  
    int temp = 0;
    Circle tempC;
    Label tempL;
    double tempD;
     for(int i=0; i < n; i++){  
             for(int j=1; j < (n-i); j++){  
                      if(arr[j-1] > arr[j]){ 
                             System.out.println(j-1 + " " + j);
                             temp = arr[j-1];  
                             arr[j-1] = arr[j];  
                             arr[j] = temp;  


                             //System.out.println(circleArray[j-1] +" "+ circleArray[j] + " "+ circleX[j-1] +" "+ circleX[j]);
                             animateCircle(circleArray[j-1], circleArray[j], circleX[j-1], circleX[j]);
                             //animateLabel(labelArray[j-1], labelArray[j], labelX[j-1], labelX[j]);                                 

                             tempC = circleArray[j-1];  
                             circleArray[j-1] = circleArray[j];  
                             circleArray[j] = tempC;

                             tempL = labelArray[j-1];  
                             labelArray[j-1] = labelArray[j];  
                             labelArray[j] = tempL;

                             tempD = circleX[j-1];  
                             circleX[j-1] = circleX[j];  
                             circleX[j] = tempD;

                             tempD = labelX[j-1];  
                             labelX[j-1] = labelX[j];  
                             labelX[j] = tempD;                                                                                              

                             System.out.println("After " + arr[j-1] + " " + arr[j]);
                     }  

             }  
     }  

} 

Before Sort

After Swap

0 个答案:

没有答案