问题描述
提示:这里最难的是JavaFX
将选择排序做成动画即可
难点分析:
提示:这里因为没学过JavaFX所以这个程序是根据黑皮书程序清单15-3改编而来
首先是数组,这里用ArrayList因为工具类有shuffle操作,方便高效
其次,是组件的安排,这里运用到了垂直布局和水平布局,还有组件,这里将布局放到面板上,将组件放到布局中就好了。
最后是事件的驱动,其实这里清单15-3已经写得差不多了,稍微改一下就好
代码:
提示:这个代码,说实话,只是实现了基本功能,很多地方都还有改,其实要做完了反而觉得其实网格面板可能更好用,还有其他思路的(例如画统计图的方式),这个就是生拉硬套,跑个乐还行,要写得好还得学JavaFX
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;
public class Lab23_15 extends Application {
private ArrayList<Integer> arrayList = new ArrayList<>(20);
private int count = 0;
@Override
public void start(Stage primaryStage) throws Exception {
for (int i = 1; i <= 20; i++) {
arrayList.add(i);
}
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
hBox.setPrefSize(10, 10);
Button btStep = new Button("Step");
Button btReset = new Button("Reset");
hBox.getChildren().add(btStep);
hBox.getChildren().add(btReset);
//实现Step:
HBox hBox1 = new HBox();
hBox1.setSpacing(10);
hBox1.setAlignment(Pos.BASELINE_CENTER);
hBox1.setPrefSize(10, 10);
Collections.shuffle(arrayList);
for (int i = 0; i < 20; i++) {
//这个组是为了创建组件
Group group = new Group();
RectanglePane temp = new RectanglePane(arrayList.get(i));
Text text = new Text(10, -5, arrayList.get(i) + "");
group.getChildren().addAll(temp, text);
hBox1.getChildren().add(group);
}
//排序完了输出的信息和实时信息
VBox vBox = new VBox();
vBox.setAlignment(Pos.BOTTOM_CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setTop(vBox);
borderPane.setCenter(hBox1);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
btStep.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (count < 20) {
selectSort(arrayList, count);
count++;
hBox1.getChildren().clear();
vBox.getChildren().clear();
for (int i = 0; i < 20; i++) {
Group group = new Group();
RectanglePane temp = new RectanglePane(arrayList.get(i));
if (arrayList.get(i) == count)
temp.getRectangle().setFill(Color.RED);
Text text = new Text(10, -5, arrayList.get(i) + "");
group.getChildren().addAll(temp, text);
hBox1.getChildren().add(group);
}
vBox.getChildren().add(new Text(arrayList.toString()+""));
} else {
vBox.getChildren().clear();
vBox.getChildren().add(new Label("Already Sort"));
}
}
});
//Reset,这个简单
btReset.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Collections.shuffle(arrayList);
count = 0;
hBox1.getChildren().clear();
for (int i = 0; i < 20; i++) {
Group group = new Group();
RectanglePane temp = new RectanglePane(arrayList.get(i));
Text text = new Text(10, -5, arrayList.get(i) + "");
group.getChildren().addAll(temp, text);
hBox1.getChildren().add(group);
}
vBox.getChildren().clear();
}
});
Scene scene = new Scene(borderPane, 1000, 280);
primaryStage.setTitle("Lab23_15");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static void selectSort(ArrayList<Integer> arr, int count) {
int i = count;
int minIndex = i;
for (; i <= count; i++) {
int min = arr.get(i);
for (int j = i + 1; j < arr.size(); j++) {
if (min > arr.get(j)) {
min = arr.get(j);
minIndex = j;
}
}
// 这个判断是一种优化:
if (minIndex != i) {
arr.set(minIndex, arr.get(i));
arr.set(i, min);
}
}
}
}
//这个类是自定义面板,仿照清单写的
class RectanglePane extends StackPane {
private Rectangle rectangle = new Rectangle(30, 100);
public RectanglePane() {
getChildren().add(rectangle);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.WHITE);
}
public RectanglePane(int high) {
rectangle.setWidth(30);
rectangle.setHeight(100 + high * 5);
getChildren().add(rectangle);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.WHITE);
}
public Rectangle getRectangle() {
return rectangle;
}
}