Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带动画的条形图中的JavaFX可视化算法_Java_Algorithm_Javafx - Fatal编程技术网

带动画的条形图中的JavaFX可视化算法

带动画的条形图中的JavaFX可视化算法,java,algorithm,javafx,Java,Algorithm,Javafx,所以我真的被困在这个问题上了,我需要将代码底部的算法输出到JavaFX中,以显示算法是如何工作的,即气泡排序将较小的条移动到这对条的前面等等。我遇到的问题是,我不知道如何让我的算法在JavaFX中正确显示。交换的一半和随机工作,但我没有工作的代码或上面的部分,所以我仍然在黑暗中,我仍然是新的java和JavaFX对我来说是一种奇怪的时刻 import javafx.application.Application; import javafx.application.Platform; impor

所以我真的被困在这个问题上了,我需要将代码底部的算法输出到JavaFX中,以显示算法是如何工作的,即气泡排序将较小的条移动到这对条的前面等等。我遇到的问题是,我不知道如何让我的算法在JavaFX中正确显示。交换的一半和随机工作,但我没有工作的代码或上面的部分,所以我仍然在黑暗中,我仍然是新的java和JavaFX对我来说是一种奇怪的时刻

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import static javafx.scene.chart.XYChart.*;
import javafx.geometry.Insets;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class hamrickP2 extends Application {
public static void main(String[] args) {
    hamrickP2.launch(args);
}

private static final int
    BAR_COUNT = 14,
    MAX_BAR_HEIGHT = 50;

private static final String
    COLOR_ACTIVE = "-fx-bar-fill: #f64",
    COLOR_INITIAL = "-fx-bar-fill: #888",
    COLOR_FINALIZED = "-fx-bar-fill: #3cf";

private static final int
    DELAY_MILLIS = 700;

@Override
public void start(Stage stage) {
    stage.setTitle("Sorting Animations");
    stage.setWidth(800);
    stage.setHeight(600);

    final BorderPane pane = new BorderPane();
    pane.setPadding(new Insets(10));

    final BarChart<String,Number> chart = new BarChart<>(new      CategoryAxis(), new NumberAxis(0, MAX_BAR_HEIGHT, 0));
    chart.setLegendVisible(false);
    chart.getYAxis().setTickLabelsVisible(false);
    chart.getYAxis().setOpacity(0);
    chart.getXAxis().setTickLabelsVisible(false);
    chart.getXAxis().setOpacity(0);
    chart.setHorizontalGridLinesVisible(false);
    chart.setVerticalGridLinesVisible(false);

    bars = new ArrayList<Data<String,Number>>();
    final Series<String,Number> data = new Series<>();
    chart.getData().add(data);
    for (int i = 0; i < BAR_COUNT; i++) {
        bars.add(new Data<>(Integer.toString(i+1), 1));
        data.getData().add(bars.get(i));
        paint(i, COLOR_INITIAL);
    }
    pane.setCenter(chart);

    inputs = new FlowPane();
    inputs.setHgap(5);
    inputs.setVgap(5);
    createButton("Randomize", () -> randomizeAll());
    createButton("Swap Halves", () -> swapHalves());
    createButton("Reverse ", () -> reverse()); 
    createButton("Selection Sort", () -> selectionSort());


    pane.setBottom(inputs);

    stage.setScene(new Scene(pane));
    stage.show();

    Platform.runLater(() -> randomizeAll());
}

    private ArrayList<Data<String,Number>> bars;
    private FlowPane inputs;

    private void createButton(String label, Runnable method) {
    final Button test = new Button(label);
    test.setOnAction(event -> new Thread(() -> {
        Platform.runLater(() -> inputs.setDisable(true));
        method.run();
        Platform.runLater(() -> inputs.setDisable(false));
    }).start());
    inputs.getChildren().add(test);
}



// CHART ACCESSORS AND MUTATORS

private void assign(int index, int value) {
    bars.get(index).setYValue(value);
}

private int retrieve(int index) {
    return (int) bars.get(index).getYValue();
}

// ANIMATION CONTROLS

private void paint(int index, final String style) {
    Platform.runLater(() -> {
        bars.get(index).getNode().setStyle(style);
    });
}

private void paintAll(final String style) {
    Platform.runLater(() -> {
        for (int i = 0; i < BAR_COUNT; i++) paint(i, style);
    });
}

private void delay() {
    try {
        Thread.sleep(DELAY_MILLIS);
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// ALGORITHMS FOR BUTTONS

private void randomizeAll() {
    Random random = new Random();
    for (int i = 0; i < BAR_COUNT; i++) {
        assign(i, random.nextInt(MAX_BAR_HEIGHT) + 1);
    }
}

private void swapHalves() {
    final int half = bars.size()/2;
    final int offset = bars.size() % 2;
    for (int i = 0; i < half; i++) {
        final int j = i + half + offset;

        paint(i, COLOR_ACTIVE);
        paint(j, COLOR_ACTIVE);

        int temp = retrieve(i);
        assign(i, retrieve(j));
        assign(j, temp);

        delay();

        paint(i, COLOR_FINALIZED);
        paint(j, COLOR_FINALIZED);
    }
    paintAll(COLOR_INITIAL);
}// Start of algorithms 

/**
 * Reverse algorithm 
 * @param array
 * @return
 */
public void reverse(){
    int array [] = new int [BAR_COUNT];
    for(int pos = 0; pos < array.length; pos++)
    {

        int generic = array[pos];
        array[pos] = array[array.length - 1 - pos];
        array[array.length -1 - pos] = generic;

        int temp = array[array.length -1 - pos];

        assign(array[pos] , retrieve(array[array.length - 1 - pos]));
        assign(temp , generic);

        delay();


        paint(pos, COLOR_FINALIZED);
        paint( temp, COLOR_FINALIZED);


    }
    paintAll(COLOR_INITIAL);

}



/**
 * Selection Sort algorithm
 * @param words
 */
public  void selectionSort(){
    int arr [] = new int [BAR_COUNT];

    for (int i = 0; i < arr.length - 1; i++)
    {
        paint(i, COLOR_ACTIVE);

        int index = i;
        for (int j = i + 1; j < arr.length; j++)
            if (arr[j] < arr[index]) 
                index = j;

        int smallerNumber = arr[index];  
        arr[index] = arr[i];
        arr[i] = smallerNumber;

        int temp = retrieve(i);
        assign(i, retrieve(i));
        assign(index, temp);

        delay();

        paint(i, COLOR_FINALIZED);
        paint(index, COLOR_FINALIZED);

    }
    paintAll(COLOR_INITIAL);
}
        /**
         * Bubble Sort algorithm
         * @param words
         */
        public  void bubbleSort(){
            int array [] = new int [BAR_COUNT];
            int temp;
            for(int i = 0; i <array.length; i++){

                for(int j = 1; j <array.length -i; j++){
                    if(array[j-1] > array[j]){
                        temp = array[j -1];
                        array[j-1] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }

                    /**
             * Insertion Sort algorithm
             * @param array
             */
    public  void insertionSort (){

        int array [] = new int [BAR_COUNT];
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j;
            for(j = i -1; j>= 0 && temp < array[j]; j--){
                array[j +1 ] = array[j];
            array[j + 1 ] =temp;
        }
            paintAll(COLOR_INITIAL);    
    }
}
}
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.stage.stage;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.FlowPane;
导入javafx.scene.chart.BarChart;
导入javafx.scene.chart.CategoryAxis;
导入javafx.scene.chart.NumberAxis;
导入静态javafx.scene.chart.XYChart.*;
导入javafx.geometry.Insets;
导入java.lang.reflect.Array;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Random;
公共类hamrickP2扩展了应用程序{
公共静态void main(字符串[]args){
hamrickP2.发射(args);
}
私有静态最终整数
杆数=14,
最大钢筋高度=50;
私有静态最终字符串
COLOR_ACTIVE=“-fx条填充:#f64”,
COLOR_INITIAL=“-fx条填充:#888”,
COLOR_FINALIZED=“-fx条填充:#3cf”;
私有静态最终整数
延迟_毫秒=700;
@凌驾
公众假期开始(阶段){
stage.setTitle(“排序动画”);
舞台布景宽度(800);
舞台设置高度(600);
最终边框窗格=新边框窗格();
窗格。设置填充(新插图(10));
最终条形图=新条形图(新类别轴(),新编号轴(0,最大条形图高度,0));
图表。setLegendVisible(假);
chart.getYAxis().setTickLabelsVisible(false);
chart.getYAxis().setOpacity(0);
chart.getXAxis().setTickLabelsVisible(false);
chart.getXAxis().setOpacity(0);
chart.setHorizontalGridLinesVisible(假);
chart.setVerticalGridLinesVisible(假);
bars=新的ArrayList();
最终系列数据=新系列();
chart.getData().add(数据);
对于(int i=0;irandomizeAll());
createButton(“交换一半”,()->交换部分());
createButton(“Reverse”,()->Reverse());
createButton(“选择排序”,()->selectionSort());
窗格.setBottom(输入);
舞台场景(新场景(窗格));
stage.show();
Platform.runLater(()->randomizell());
}
私人酒吧;
私有流窗格输入;
私有void createButton(字符串标签,可运行方法){
最终按钮测试=新按钮(标签);
test.setOnAction(事件->新线程(()->{
Platform.runLater(()->inputs.setDisable(true));
方法run();
Platform.runLater(()->inputs.setDisable(false));
}).start());
inputs.getChildren().add(测试);
}
//图表存取器和变异器
私有void赋值(int索引,int值){
bar.get(索引).setYValue(值);
}
私有整数检索(整数索引){
return(int)bar.get(index.getYValue();
}
//动画控件
专用void绘制(int索引,最终字符串样式){
Platform.runLater(()->{
bar.get(index.getNode().setStyle(style);
});
}
私有void paintAll(最终字符串样式){
Platform.runLater(()->{
对于(int i=0;iimport javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

import java.util.List;
import java.util.Random;

import static javafx.scene.chart.XYChart.Data;
import static javafx.scene.chart.XYChart.Series;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    private static final int
        BAR_COUNT = 14,
        MAX_BAR_HEIGHT = 50;

    private static final String
        COLOR_ACTIVE = "-fx-bar-fill: #f64",
        COLOR_INITIAL = "-fx-bar-fill: #888",
        COLOR_FINALIZED = "-fx-bar-fill: #3cf";

    private static final int
        DELAY_MILLIS = 700;


    private ObservableList<Data<String, Number>> bars;
    private BarChart<String, Number> chart;
    private FlowPane inputs;


    @Override
    public void start(Stage stage) {
        stage.setTitle("Sorting Animations");
        stage.setWidth(800);
        stage.setHeight(600);

        final BorderPane pane = new BorderPane();
        pane.setPadding(new Insets(10));

        makeChart(pane);
        makeButtons(pane);

        stage.setScene(new Scene(pane));
        stage.show();

        randomizeAll();
    }

    private void makeChart(BorderPane pane) {
        chart = new BarChart<>(new CategoryAxis(), new NumberAxis(0, MAX_BAR_HEIGHT, 0));
        chart.setLegendVisible(false);
        chart.getYAxis().setTickLabelsVisible(false);
        chart.getYAxis().setOpacity(0);
        chart.getXAxis().setTickLabelsVisible(false);
        chart.getXAxis().setOpacity(0);
        chart.setHorizontalGridLinesVisible(false);
        chart.setVerticalGridLinesVisible(false);

        bars = FXCollections.observableArrayList();
        chart.getData().add(new Series<>(bars));

        for (int i = 0; i < BAR_COUNT; i++) {
            Data<String, Number> dataObject = new Data<>(Integer.toString(i + 1), 1);
            bars.add(dataObject); // node will be present after this
            addPainting(dataObject.getNode(), COLOR_INITIAL); // do this after bars.add
        }
        pane.setCenter(chart);
    }


    private void makeButtons(BorderPane pane) {
        inputs = new FlowPane();
        inputs.setHgap(5);
        inputs.setVgap(5);
        createButton("Randomize", () -> randomizeAll());
        createButton("Bubble Sort 1", () -> bubbleSort1());
        createButton("Bubble Sort 2", () -> bubbleSort2());
        pane.setBottom(inputs);
    }

    private void addPainting(Node newNode, String colorInitial) {
        if (newNode != null) {
            newNode.setStyle(colorInitial);
        }
    }

    private void createButton(String label, Runnable method) {
        final Button test = new Button(label);
        test.setOnAction(event -> method.run());
        inputs.getChildren().add(test);
    }


    private void randomizeAll() {
        Random random = new Random();
        for (Data<String, Number> bar : bars) {
            bar.setYValue(random.nextInt(MAX_BAR_HEIGHT) + 1);
        }
    }


    /**
     * Bubble Sort algorithm
     */
    private void bubbleSort1(){
        List<Data<String, Number>> list = bars;

        double temp;        
        for(int i = 0; i < list.size(); i++){
            for(int j = 1; j < list.size() - i; j++){
                if (getValue(list, j - 1) > getValue(list, j)){
                    temp = getValue(list, j - 1);
                    list.get(j - 1).setYValue(list.get(j).getYValue());
                    list.get(j).setYValue(temp);
                }
            }
        }
    }

    private double getValue(List<Data<String, Number>> list, int index) {
        return list.get(index).getYValue().doubleValue();
    }


    /**
     * Bubble Sort algorithm
     */
    private void bubbleSort2(){
        double[] array = bars.stream().mapToDouble(data -> data.getYValue().doubleValue()).toArray();

        double temp;
        for(int i = 0; i <array.length; i++){
            for(int j = 1; j <array.length -i; j++){
                if(array[j-1] > array[j]){
                    temp = array[j -1];
                    array[j-1] = array[j];
                    array[j] = temp;
                }
            }
        }
        for (int i = 0; i < array.length; i++) {
            bars.get(i).setYValue(array[i]);
        }
    }

}
private void assign(int index, int value) {
    bars.get(index).setYValue(value);
}

private int retrieve(int index) {
    return (int) bars.get(index).getYValue();
}