我一直致力于Java FX应用程序。它旨在成为A-Level AQA计算机科学课程的教学工具。我目前正在研究课程中的排序算法的可视化。但是,我对动画有问题。我使用顺序转换。然后,顺序转换执行并行转换,执行2个转换转换,以交换数组中的2个元素。从理论上讲,这应该是完美的。但是,当我执行转换时,元素的定位会受到奇怪的影响。我已经包含了之前和之后显示的代码和图片。请帮帮我。
图片: https://imgur.com/34mbZhe https://imgur.com/gHlal7z
代码:
排序类:
public boolean BubbleSortWrapper() {
if (Array.size() > 1) {
Animations = new SequentialTransition();
BubbleSort();
Animations.play();
return true;
}
return false;
}
private void BubbleSort() {
//O(n^2) - BubbleSort using CompletedFlag implimentation
boolean CompletedFlag = true;
for (int i = 0; i < Array.size() && CompletedFlag; i++) {
CompletedFlag = false;
for (int j = i + 1; j < Array.size(); j++) {
if (Array.get(i).element.compareTo(Array.get(j).element) > 0) {
Element<Type> Temporary = Array.get(i);
Animations.getChildren().add(Animation.Swap(GetStackPaneFromElement(Array.get(i).element), GetStackPaneFromElement(Array.get(j).element), GetXPosition(i), GetXPosition(j)));
Array.set(i,Array.get(j));
Array.set(j, Temporary);
Array.get(i).index = i;
Array.get(j).index = j;
CompletedFlag = true;
}
}
}
}
private double GetXPosition(int Index){
return (StartX + (Index * (ElementWidth + ElementGap)));
}
动画类:
public static ParallelTransition Swap(StackPane ElementPane1, StackPane ElementPane2, double X1, double X2) {
return new ParallelTransition(
Move(ElementPane1, X2),
Move(ElementPane2, X1)
);
}
public static TranslateTransition Move(StackPane ElementPane, double X) {
TranslateTransition Transition = new TranslateTransition(Duration.millis(900) , ElementPane);
Transition.setToX(X);
return Transition;
}