Skip to content

Commit d4af2e5

Browse files
committed
commit Exercise_30_19
1 parent 8abb13d commit d4af2e5

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package exercise_30_19;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import javafx.application.Application;
6+
import javafx.application.Platform;
7+
import javafx.geometry.Insets;
8+
import javafx.geometry.Pos;
9+
import javafx.scene.Scene;
10+
import javafx.scene.control.Label;
11+
import javafx.scene.layout.BorderPane;
12+
import javafx.scene.layout.HBox;
13+
import javafx.scene.paint.Color;
14+
import javafx.scene.shape.Rectangle;
15+
import javafx.stage.Stage;
16+
17+
public class Exercise_30_19 extends Application {
18+
19+
@Override
20+
public void start(Stage primaryStage) {
21+
ArrayList<Integer> list1 = new ArrayList<>();
22+
for(int i = 0; i < 50; i++) {
23+
list1.add(i + 1);
24+
}
25+
Collections.shuffle(list1);
26+
27+
HBox hBox = new HBox(10);
28+
hBox.setPadding(new Insets(5));
29+
30+
HistogramPane pane1 = new SelectionSortPane(list1, new Label("Selection Sort"));
31+
// HistogramPane pane2 = new HistogramPane(list1, new Label("Insertion Sort"));
32+
// HistogramPane pane3 = new HistogramPane(list1, new Label("Bubble Sort"));
33+
hBox.getChildren().addAll(pane1);
34+
35+
Scene scene = new Scene(hBox);
36+
scene.getStylesheets().add("file:///c:/programming/java/Intro_to_Java_Programming_10th_exercises/JavaProgrammingExercises/Chapter_30/exercise_30_19/style2.css");
37+
primaryStage.setTitle("Exercise_30_19");
38+
primaryStage.setScene(scene);
39+
primaryStage.show();
40+
}
41+
42+
private abstract class HistogramPane extends BorderPane {
43+
protected ArrayList<Integer> list;
44+
protected HBox hBoxForChart = new HBox();
45+
protected Label lblSortType;
46+
protected Rectangle[] rectangles;
47+
protected final int WIDTH = 5;
48+
protected final int HEIGHT = 5;
49+
50+
public HistogramPane(ArrayList<Integer> list, Label lblSortType) {
51+
this.list = list;
52+
this.lblSortType = lblSortType;
53+
rectangles = new Rectangle[list.size()];
54+
setCenter(hBoxForChart);
55+
setTop(lblSortType);
56+
BorderPane.setAlignment(lblSortType, Pos.CENTER);
57+
repaintHistogram();
58+
}
59+
60+
public void repaintHistogram() {
61+
hBoxForChart.getChildren().clear();
62+
hBoxForChart.setAlignment(Pos.BASELINE_CENTER);
63+
int j = 0;
64+
for(Integer i : list) {
65+
rectangles[j] = new Rectangle(WIDTH, i * HEIGHT);
66+
rectangles[j].setStroke(Color.BLACK);
67+
rectangles[j].setFill(Color.TRANSPARENT);
68+
hBoxForChart.getChildren().add(rectangles[j]);
69+
j++;
70+
}
71+
}
72+
}
73+
74+
private class SelectionSortPane extends HistogramPane {
75+
private Thread thread = new Thread(() -> {
76+
try {
77+
while(true) {
78+
Platform.runLater(() -> selectionSort());
79+
Thread.sleep(1000);
80+
}
81+
}
82+
catch(InterruptedException ex) {
83+
ex.printStackTrace();
84+
}
85+
});
86+
87+
public SelectionSortPane(ArrayList<Integer> list, Label lblSortType) {
88+
super(list, lblSortType);
89+
}
90+
91+
public void selectionSort() {
92+
for(int i = 0; i < list.size() - 1; i++) {
93+
int currentMin = list.get(i);
94+
int currentMinIndex = i;
95+
for(int j = i + 1; j < list.size(); j++) {
96+
if(currentMin > list.get(j)) {
97+
currentMin = list.get(j);
98+
currentMinIndex = j;
99+
}
100+
}
101+
if(currentMinIndex != i) {
102+
list.set(currentMinIndex, list.get(i));
103+
list.set(i, currentMin);
104+
}
105+
repaintHistogram();
106+
}
107+
}
108+
}
109+
110+
public static void main(String[] args) {
111+
launch(args);
112+
}
113+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#hboxfornodes {
2+
-fx-border-color: black;
3+
-fx-padding: 2;
4+
-fx-alignment: bottom-left;
5+
}
6+
7+
#pane {
8+
-fx-border-color: black;
9+
}
10+
11+
.button {
12+
-fx-font-family: "Ariel";
13+
-fx-font-weight: bold;
14+
-fx-background-radius: 5, 5;
15+
}
16+
17+
.label {
18+
-fx-font-family: "Ariel";
19+
-fx-font-weight: bold;
20+
}
21+
22+
#labels {
23+
-fx-font-family: "Courier New";
24+
-fx-font-weight: bold;
25+
-fx-font-size: 30px;
26+
}
27+
28+
#labelwithred {
29+
-fx-text-fill: red;
30+
-fx-font-family: "Courier New";
31+
-fx-font-weight: bold;
32+
-fx-font-size: 30px;
33+
}
34+
35+
#line {
36+
-fx-stroke: deepskyblue;
37+
-fx-stroke-width: 3;
38+
}
39+
40+
#redline {
41+
-fx-stroke: red;
42+
-fx-stroke-width: 3;
43+
}
44+
45+
#textfield {
46+
-fx-alignment: BASELINE_LEFT
47+
}

0 commit comments

Comments
 (0)