Skip to content

Instantly share code, notes, and snippets.

@trashgod
Last active July 3, 2023 15:21
Show Gist options
  • Save trashgod/dfa2f2e228465449ac473bb96ad8ae72 to your computer and use it in GitHub Desktop.
Save trashgod/dfa2f2e228465449ac473bb96ad8ae72 to your computer and use it in GitHub Desktop.
Bubble sort animated bar chart.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* @see https://stackoverflow.com/a/53579013/230513
*/
public class BubbleSortAnimation extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" fx:controller="FXMLDocumentController"
xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="button" layoutX="16" layoutY="16" onAction="#handleButtonAction" text="Run" />
<BarChart fx:id="barChart" animated="false" cache="false" layoutX="16" layoutY="64" legendVisible="false" prefHeight="400" prefWidth="600">
<xAxis>
<CategoryAxis fx:id="domain" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="range" side="LEFT" />
</yAxis>
</BarChart>
</children>
</AnchorPane>
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.util.Duration;
public class FXMLDocumentController implements Initializable {
private static final int N = 16;
private static final Random R = new Random();
private final BarChart.Series<String, Integer> series = new BarChart.Series<>();
private final Timeline timeline = new Timeline();
private final int[] pole = new int[N];
private int iteration;
@FXML
private BarChart<String, Integer> barChart;
@FXML
private void handleButtonAction(ActionEvent ae) {
for (int i = 0; i < pole.length; i++) {
pole[i] = R.nextInt(100);
series.getData().get(i).setYValue(pole[i]);
}
iteration = 0;
timeline.stop();
timeline.getKeyFrames().clear();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(1000 / N), (ev) -> {
System.out.println(iteration + ": " + Arrays.toString(pole));
for (int j = 0; j < pole.length - (iteration) - 1; j++) {
if (pole[j] < pole[j + 1]) {
final int tmp = pole[j];
pole[j] = pole[j + 1];
pole[j + 1] = tmp;
}
}
// update series data for this iteration
for (int i = 0; i < pole.length; i++) {
series.getData().get(i).setYValue(pole[i]);
}
iteration++;
}));
timeline.setCycleCount(pole.length);
timeline.play();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
for (int i = 0; i < N; i++) {
series.getData().add(new XYChart.Data<>(String.valueOf(i), 0));
}
ObservableList<BarChart.Series<String, Integer>> chartData = FXCollections.observableArrayList();
chartData.add(series);
barChart.setData(chartData);
handleButtonAction(null); // initialize
}
}
#!/bin/sh
JFX="--module-path /Users/Shared/javafx-sdk-17.0.7/lib --add-modules ALL-MODULE-PATH"
javac $JFX *.java && java $JFX BubbleSortAnimation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment