forked from vbohush/SortingAlgorithmAnimations
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
214 lines (174 loc) · 6.75 KB
/
Main.java
File metadata and controls
214 lines (174 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Visualization and Comparison of Sorting Algorithms
public class Main extends JPanel {
private static final long serialVersionUID = 1L;
// Sadece 2 panel karşılaştırıyoruz
private SortPanel[] sortPanels = new SortPanel[2];
private static int size = 100;
private int sleepTime = 10; // Radix hızlı olduğu için biraz yavaşlattım (2 -> 10)
private String animationName = "";
private JComboBox<String> algo1Box;
private JComboBox<String> algo2Box;
private JComboBox<String> dataBox;
private JButton startButton;
private SortPanelsHolder sortPanelHolder;
public Main() {
setLayout(new BorderLayout());
// ---------- CONTROL PANEL ----------
JPanel controlPanel = new JPanel();
// DÜZELTME 1: Virgüller eklendi
String[] algorithms = {
"Heap Sort",
"Shell Sort",
"Radix Sort"
};
algo1Box = new JComboBox<>(algorithms);
algo2Box = new JComboBox<>(algorithms);
dataBox = new JComboBox<>(new String[]{
"Random",
"Reversed",
"Almost Sorted",
"Few Unique"
});
startButton = new JButton("Start");
controlPanel.add(new JLabel("Algorithm 1:"));
controlPanel.add(algo1Box);
controlPanel.add(new JLabel("Algorithm 2:"));
controlPanel.add(algo2Box);
controlPanel.add(new JLabel("Data:"));
controlPanel.add(dataBox);
controlPanel.add(startButton);
add(controlPanel, BorderLayout.NORTH);
// ---------- SORT PANELS HOLDER ----------
sortPanelHolder = new SortPanelsHolder();
sortPanelHolder.setLayout(new GridLayout(0, 2, 0, 0));
sortPanelHolder.setBackground(Color.decode("#3B9797"));
add(sortPanelHolder, BorderLayout.CENTER);
// ---------- BUTTON ACTION ----------
startButton.addActionListener(e -> startSelectedAlgorithms());
setPreferredSize(new Dimension(1000, 600));
}
// ---------- CREATE SORT PANEL ----------
private SortPanel createSortPanel(String name, int width, int height) {
switch (name) {
case "Heap Sort":
return new HeapSortPanel(" Heap Sort ", sleepTime, width, height);
case "Shell Sort":
return new ShellSortPanel(" Shell Sort ", sleepTime, width, height);
case "Radix Sort":
return new RadixSortPanel(" Radix Sort ", sleepTime, width, height);
default:
throw new IllegalArgumentException("Unknown algorithm: " + name);
}
}
// ---------- START ANIMATION ----------
private void startSelectedAlgorithms() {
sortPanelHolder.removeAll();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width / 2;
int height = screenSize.height / 2;
// DÜZELTME 2: 3. paneli sildim, çünkü dizi boyutu 2.
// Dropdown'dan seçilenleri alıyoruz:
sortPanels[0] = createSortPanel(
(String) algo1Box.getSelectedItem(), width, height);
sortPanels[1] = createSortPanel(
(String) algo2Box.getSelectedItem(), width, height);
for (SortPanel panel : sortPanels) {
panel.setVisible(false);
sortPanelHolder.add(panel);
}
sortPanelHolder.revalidate();
sortPanelHolder.repaint();
String dataType = (String) dataBox.getSelectedItem();
int[] list = generateData(dataType);
new Thread(() -> beginAnimation(dataType, list)).start();
}
private int[] generateData(String type) {
int[] list = new int[size];
switch (type) {
case "Random":
for (int i = 0; i < size; i++) list[i] = i + 1;
shuffle(list);
break;
case "Reversed":
for (int i = 0; i < size; i++) list[i] = size - i;
break;
case "Almost Sorted":
for (int i = 0; i < size / 2; i++) list[i] = i + 1;
for (int i = size / 2; i < size; i++) list[i] = i + 2;
list[size - 1] = size / 2 + 1;
break;
case "Few Unique":
for (int i = 0; i < size; i++) {
list[i] = (1 + i / (size / 4)) * (size / 4);
}
shuffle(list);
break;
}
return list;
}
private void shuffle(int[] list) {
for (int i = 0; i < list.length; i++) {
int j = (int) (Math.random() * list.length);
int tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
// ---------- HOLDER ----------
class SortPanelsHolder extends JPanel {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
Font font = new Font(Font.MONOSPACED, Font.BOLD, 150);
FontMetrics fm = getFontMetrics(font);
g.setFont(font);
int x = (getWidth() - fm.stringWidth(animationName)) / 2;
int y = getHeight() / 2;
g.drawString(animationName, x, y);
}
}
// ---------- ANIMATION ----------
public void beginAnimation(String animationName, int[] list) {
try {
this.animationName = animationName;
repaint();
Thread.sleep(2000);
this.animationName = "";
repaint();
for (SortPanel panel : sortPanels) {
panel.setList(list.clone());
panel.setVisible(true);
}
ExecutorService executor = Executors.newFixedThreadPool(sortPanels.length);
for (SortPanel panel : sortPanels) {
executor.execute(panel);
}
executor.shutdown();
while (!executor.isTerminated()) {
Thread.sleep(100);
}
for (SortPanel panel : sortPanels) {
panel.setVisible(false);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// ---------- MAIN ----------
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Sorting Algorithm Animations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Main());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}