|
18 | 18 | import java.awt.Dimension;
|
19 | 19 | import java.util.*;
|
20 | 20 | import javax.swing.*;
|
| 21 | +import javax.swing.event.ChangeEvent; |
| 22 | +import javax.swing.event.ChangeListener; |
| 23 | + |
21 | 24 | import java.awt.event.*;
|
22 | 25 |
|
23 |
| -public class AlgVisualizer implements ActionListener { |
| 26 | +public class AlgVisualizer implements ActionListener, ChangeListener { |
24 | 27 |
|
25 | 28 | private final int CONTENT_WIDTH = 900;
|
26 | 29 | private final int CONTENT_HEIGHT = 960;
|
27 | 30 | private final int ARR_DISPLAY_HEIGHT = 900;
|
| 31 | + private final int FPS_MIN = 1; |
| 32 | + private final int FPS_INIT = 10; |
| 33 | + private final int FPS_MAX = 100; |
28 | 34 | private final String[] SIZE_OPTIONS = { "10", "50", "100", "300", "450", "900" }; // array size options
|
29 | 35 | private int n;
|
30 | 36 | private int numSwaps;
|
31 | 37 | private int delay;
|
32 | 38 | private Integer indexComparisons;
|
33 | 39 | private long startTime; // start time of a sort
|
34 | 40 | private long endTime; // end time of a sort
|
| 41 | + private long visualizationTime; |
| 42 | + private long sortingTime; |
35 | 43 | private boolean doBubbleSort;
|
36 | 44 | private boolean doInsertionSort;
|
37 | 45 | private boolean doSelectionSort;
|
@@ -88,7 +96,9 @@ public void initializeVars() {
|
88 | 96 | indexComparisons = 0;
|
89 | 97 | startTime = 0;
|
90 | 98 | endTime = 0;
|
91 |
| - setDelay(2); |
| 99 | + visualizationTime = 0; |
| 100 | + sortingTime = 0; |
| 101 | + setDelay(1000 / FPS_INIT); |
92 | 102 |
|
93 | 103 | // Initialize objects that will display and sort the array
|
94 | 104 |
|
@@ -136,9 +146,10 @@ public void initializeVars() {
|
136 | 146 | sizeChanger = new JComboBox<String>(SIZE_OPTIONS); // Pass the String containing all of the size options
|
137 | 147 | sizeChanger.addActionListener(this);
|
138 | 148 | sizeChanger.setBackground(Color.WHITE);
|
139 |
| - |
140 |
| - FPSslider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15); |
141 | 149 |
|
| 150 | + FPSslider = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT); |
| 151 | + FPSslider.addChangeListener(this); |
| 152 | + FPSslider.setBackground(Color.DARK_GRAY); |
142 | 153 | // Initialize the performance label and center it
|
143 | 154 |
|
144 | 155 | performanceLabel = new JLabel();
|
@@ -227,6 +238,16 @@ public void actionPerformed(ActionEvent event) {
|
227 | 238 | }
|
228 | 239 | }
|
229 | 240 |
|
| 241 | + @Override |
| 242 | + public void stateChanged(ChangeEvent e) { |
| 243 | + JSlider source = (JSlider) e.getSource(); |
| 244 | + if (!source.getValueIsAdjusting()) { |
| 245 | + int fps = (int) source.getValue(); |
| 246 | + delay = 1000 / fps; // ms |
| 247 | + setDelay(delay); |
| 248 | + } |
| 249 | + } |
| 250 | + |
230 | 251 | /*
|
231 | 252 | * Reset method is called whenever the user presses the reset button, or when a
|
232 | 253 | * new size of array is chosen from the size changer. This method stops sorting,
|
@@ -257,6 +278,8 @@ public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displa
|
257 | 278 | public void resetTime() {
|
258 | 279 | startTime = 0;
|
259 | 280 | endTime = 0;
|
| 281 | + visualizationTime = 0; |
| 282 | + sortingTime = 0; |
260 | 283 | }
|
261 | 284 |
|
262 | 285 | public Integer[] shuffleArr(Integer[] arr) {
|
@@ -288,11 +311,12 @@ public Integer[] fillArr(Integer[] arr) {
|
288 | 311 | public void updatePerformance() {
|
289 | 312 | numSwaps = arrDisplay.getSwappedIndexes().size();
|
290 | 313 |
|
291 |
| - long visualizationTime = 0; |
292 |
| - long sortingTime = 0; |
293 |
| - if (!getSort().equals("Not Sorting")) { |
| 314 | + if (!getSort().equals("Not Sorting") && arrDisplay.getNumChunks() == 1) { |
294 | 315 | visualizationTime = System.currentTimeMillis() - startTime;
|
295 | 316 | sortingTime = visualizationTime - (delay * (arrDisplay.getNumChunks() - 1));
|
| 317 | + } else if (arrDisplay.getNumChunks() > 1) { |
| 318 | + visualizationTime = System.currentTimeMillis() - startTime; |
| 319 | + sortingTime = sortingTime + (System.currentTimeMillis() - sortingTime); |
296 | 320 | }
|
297 | 321 |
|
298 | 322 | String performance = String.format(
|
|
0 commit comments