-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualizerFrame.java
204 lines (170 loc) · 6.78 KB
/
VisualizerFrame.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
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
package Visualizer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class VisualizerFrame extends JFrame {
private final int MAX_SPEED = 1000;
private final int MIN_SPEED = 1;
private final int MAX_SIZE = 500;
private final int MIN_SIZE = 5;
private final int DEFAULT_SPEED = 20;
private final int DEFAULT_SIZE = 20;
private final String[] Sorts = {"Bubble", "Selection", "Insertion", "Gnome", "Merge", "Radix LSD", "Radix MSD", "Shell", "Quandrix", "Bubble(fast)", "Selection(fast)", "Insertion(fast)", "Gnome(fast)"};
private int sizeModifier;
private JPanel wrapper;
private JPanel arrayWrapper;
private JPanel buttonWrapper;
private JPanel[] squarePanels;
private JButton start;
private JComboBox<String> selection;
private JSlider speed;
private JSlider size;
private JLabel speedVal;
private JLabel sizeVal;
private GridBagConstraints c;
private JCheckBox stepped;
public VisualizerFrame(){
super("Sorting Visualizer");
start = new JButton("Start");
buttonWrapper = new JPanel();
arrayWrapper = new JPanel();
wrapper = new JPanel();
selection = new JComboBox<String>();
speed = new JSlider(MIN_SPEED, MAX_SPEED, DEFAULT_SPEED);
size = new JSlider(MIN_SIZE, MAX_SIZE, DEFAULT_SIZE);
speedVal = new JLabel("Speed: 20 ms");
sizeVal = new JLabel("Size: 20 values");
stepped = new JCheckBox("Stepped Values");
c = new GridBagConstraints();
for(String s : Sorts) selection.addItem(s);
arrayWrapper.setLayout(new GridBagLayout());
wrapper.setLayout(new BorderLayout());
c.insets = new Insets(0,1,0,1);
c.anchor = GridBagConstraints.SOUTH;
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SortingVisualizer.startSort((String) selection.getSelectedItem());
}
});
stepped.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SortingVisualizer.stepped = stepped.isSelected();
}
});
speed.setMinorTickSpacing(10);
speed.setMajorTickSpacing(100);
speed.setPaintTicks(true);
speed.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
speedVal.setText(("Speed: " + Integer.toString(speed.getValue()) + "ms"));
validate();
SortingVisualizer.sleep = speed.getValue();
}
});
size.setMinorTickSpacing(10);
size.setMajorTickSpacing(100);
size.setPaintTicks(true);
size.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
sizeVal.setText(("Size: " + Integer.toString(size.getValue()) + " values"));
validate();
SortingVisualizer.sortDataCount = size.getValue();
}
});
buttonWrapper.add(stepped);
buttonWrapper.add(speedVal);
buttonWrapper.add(speed);
buttonWrapper.add(sizeVal);
buttonWrapper.add(size);
buttonWrapper.add(start);
buttonWrapper.add(selection);
wrapper.add(buttonWrapper, BorderLayout.SOUTH);
wrapper.add(arrayWrapper);
add(wrapper);
setExtendedState(JFrame.MAXIMIZED_BOTH );
addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
// Reset the sizeModifier
// 90% of the windows height, divided by the size of the sorted array.
sizeModifier = (int) ((getHeight()*0.9)/(squarePanels.length));
}
@Override
public void componentMoved(ComponentEvent e) {
// Do nothing
}
@Override
public void componentShown(ComponentEvent e) {
// Do nothing
}
@Override
public void componentHidden(ComponentEvent e) {
// Do nothing
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
// preDrawArray reinitializes the array of panels that represent the values. They are set based on the size of the window.
public void preDrawArray(Integer[] squares){
squarePanels = new JPanel[SortingVisualizer.sortDataCount];
arrayWrapper.removeAll();
// 90% of the windows height, divided by the size of the sorted array.
sizeModifier = (int) ((getHeight()*0.9)/(squarePanels.length));
for(int i = 0; i<SortingVisualizer.sortDataCount; i++){
squarePanels[i] = new JPanel();
squarePanels[i].setPreferredSize(new Dimension(SortingVisualizer.blockWidth, squares[i]*sizeModifier));
squarePanels[i].setBackground(Color.blue);
arrayWrapper.add(squarePanels[i], c);
}
repaint();
validate();
}
public void reDrawArray(Integer[] x){
reDrawArray(x, -1);
}
public void reDrawArray(Integer[] x, int y){
reDrawArray(x, y, -1);
}
public void reDrawArray(Integer[] x, int y, int z){
reDrawArray(x, y, z, -1);
}
// reDrawArray does similar to preDrawArray except it does not reinitialize the panel array.
public void reDrawArray(Integer[] squares, int working, int comparing, int reading){
arrayWrapper.removeAll();
for(int i = 0; i<squarePanels.length; i++){
squarePanels[i] = new JPanel();
squarePanels[i].setPreferredSize(new Dimension(SortingVisualizer.blockWidth, squares[i]*sizeModifier));
if (i == working){
squarePanels[i].setBackground(Color.green);
}else if(i == comparing){
squarePanels[i].setBackground(Color.red);
}else if(i == reading){
squarePanels[i].setBackground(Color.yellow);
}else{
squarePanels[i].setBackground(Color.blue);
}
arrayWrapper.add(squarePanels[i], c);
}
repaint();
validate();
}
}