-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
114 lines (99 loc) · 3.37 KB
/
Main.java
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
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 JApplet {
private static final long serialVersionUID = 1L;
private SortPanel[] sortPanels = new SortPanel[3];
private static int size = 30;
private int sleepTime = 100;
private String animationName = "";
public Main() {
setLayout(new GridLayout(1, 1, 0, 0));
SortPanelsHolder sortPanelHolder = new SortPanelsHolder();
sortPanelHolder.setLayout(new GridLayout(3, 1, 0, 0));
sortPanelHolder.setBackground(Color.CYAN);
sortPanelHolder.setForeground(Color.CYAN);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width / 1;
int height = screenSize.height / 3;
sortPanels[0] = new SelectionSortPanel(" Selection Sort ", sleepTime, width, height);
sortPanels[1] = new InsertionSortPanel(" Insertion Sort ", sleepTime, width, height);
sortPanels[2] = new BubbleSortPanel(" Bubble Sort ", sleepTime, width, height);
// sortPanels[3] = new MergeSortPanel(" Merge Sort ", sleepTime, width, height);
for (int i = 0; i < sortPanels.length; i++) {
sortPanels[i].setVisible(false);
sortPanelHolder.add(sortPanels[i]);
}
add(sortPanelHolder);
}
class SortPanelsHolder extends JPanel {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
Font animationNameFont = new Font("Serif", Font.BOLD, 150);
FontMetrics animationNameFontFontMetrix = getFontMetrics(animationNameFont);
g.setFont(animationNameFont);
int x = (getWidth() - animationNameFontFontMetrix.stringWidth(animationName)) / 2;
int y = (getHeight() - animationNameFontFontMetrix.getLeading()) / 2;
g.drawString(animationName, x, y);
}
}
public void beginAnimation(String animationName, int[] list) {
try {
this.animationName = animationName;
repaint();
Thread.sleep(2000);
this.animationName = "";
repaint();
for (int i = 0; i < sortPanels.length; i++) {
sortPanels[i].setList(list);
sortPanels[i].setVisible(true);
}
Thread.sleep(1000);
ExecutorService executor = Executors.newFixedThreadPool(sortPanels.length);
for (int i = 0; i < sortPanels.length; i++) {
executor.execute(sortPanels[i]);
}
executor.shutdown();
while(!executor.isTerminated()) {
Thread.sleep(100);
}
Thread.sleep(1000);
for (int i = 0; i < sortPanels.length; i++) {
sortPanels[i].setVisible(false);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void endAnimation(){
this.animationName = "Hakuna Matata";
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Sorting Algorithm Animations");
Main main = new Main();
frame.add(main);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
int[] list = new int[size];
for (int i = 0; i < list.length; i++) {
list[i] = i + 1;
}
for (int i = 0; i < list.length; i++) {
int index = (int) (Math.random() * list.length);
int temp = list[i];
list[i] = list[index];
list[index] = temp;
}
main.beginAnimation("CS382 Project", list);
main.endAnimation();
}
}