1515
1616import java .util .*;
1717import javax .swing .*;
18- import javax .swing .event .ChangeEvent ;
19- import javax .swing .event .ChangeListener ;
20-
2118import java .awt .event .*;
19+ import javax .swing .event .*;
2220
2321public class AlgVisualizer implements ActionListener , ChangeListener {
2422
25- private final int FPS_MIN = 2 ;
23+ private final int FPS_MIN = 2 ;
2624 private final int FPS_INIT = 10 ;
2725 private final int FPS_MAX = 100 ;
2826 private String [] sizeOptions = { "10" , "50" , "100" , "300" , "450" , "900" }; // array size options
@@ -31,24 +29,28 @@ public class AlgVisualizer implements ActionListener, ChangeListener {
3129 private int delay ;
3230 private long totalDelay ;
3331 private Integer indexComparisons ;
34- private long startTime ; // start time of a sort
32+ private long startTime ; // start time of a sort
3533 private long visualizationTime ;
3634 private long sortingTime ;
3735 private boolean doBubbleSort ;
3836 private boolean doInsertionSort ;
3937 private boolean doSelectionSort ;
4038 private boolean doMergeSort ;
4139 private boolean doQuickSort ;
42- private boolean stopSort ; // True if sorting is stopped
43- private Integer [] arr ; // array that is going to be sorted
40+ private boolean stopSort ; // True if sorting is stopped
41+ private Integer [] arr ; // array that is going to be sorted
4442 private ContentWindow frame ;
4543 private SwingWorker <Void , Integer []> arrSort ;
4644
4745
4846 /*
49- * When an action is performed on a component on the JFrame that has had this
47+ * actionPerformed(ActionEvent event)
48+ *
49+ * When an action is performed on a component on the JFrame that has had this
5050 * classes actionListener added to it, this method will decide what to do based
51- * on what component was clicked on. When a sorting button is clicked, its
51+ * on the event.
52+ *
53+ * When a sorting button is clicked, its
5254 * respective boolean do(..)Sort will be set to true, and arrSort().execute.
5355 * This will call the doInBackground() method in the arrSort object, where it
5456 * will use the do(..)Sort variable to discover which sorting algorithm to use,
@@ -62,7 +64,9 @@ public void actionPerformed(ActionEvent event) {
6264 doInsertionSort = false ;
6365 doMergeSort = false ;
6466 doQuickSort = false ;
67+
6568 // Find the source of the action
69+ // Is there a better way to do this?
6670 if (event .getSource () == frame .getBubbleButton ()) {
6771 doBubbleSort = true ;
6872 arrSort .execute ();
@@ -91,6 +95,15 @@ public void actionPerformed(ActionEvent event) {
9195 }
9296 }
9397
98+ /*
99+ * stateChanged(ChangeEvent e)
100+ *
101+ * This method is called when the FPS slider
102+ * is shifted to change the FPS of sorting.
103+ *
104+ * We change the amount of delay occuring in the
105+ * sorting to what the value of the slider was moved to.
106+ */
94107 @ Override
95108 public void stateChanged (ChangeEvent e ) {
96109 JSlider source = (JSlider ) e .getSource ();
@@ -99,10 +112,13 @@ public void stateChanged(ChangeEvent e) {
99112 setDelay (delay );
100113 }
101114
102- /*
115+ /*
116+ * reset()
117+ *
103118 * Reset method is called whenever the user presses the reset button, or when a
104- * new size of array is chosen from the size changer. This method stops sorting,
105- * re-shuffles the array, clears all swapped indexes, frames painted, tracked
119+ * new size of array is chosen from the size changer.
120+ *
121+ * This method stops sorting, re-shuffles the array, clears all swapped indexes, frames painted, tracked
106122 * time, and comparisons. It must also reset the swingWorker so that the user is
107123 * able to see another sort. Since sort.execute() can only be called once for
108124 * SwingWorker, we simply re-instantiate it so that we are able to call it
@@ -149,28 +165,38 @@ public Integer[] fillArr(Integer[] arr) {
149165 }
150166
151167 /*
152- * updatePerformance will be called every time the array is repainted. This
153- * makes it slower / not real time when there is a high delay.
168+ * updatePerformance()
169+ *
170+ * This method will be called every time that the frame is updated in order to
171+ * update our performance statistics.
154172 *
155- * We get the values for each performance statistic we want to track (number of
156- * swaps, number of comparisons, visualization time, sorting time), format them
173+ * Finds the values for each performance statistic being tracked (number of
174+ * swaps, number of comparisons, visualization time, sorting time), formats them
157175 * into a string that will then be assigned to the JLabel's text.
158176 *
159177 * frame.pack() makes sure that our new text will fit in the frame, and will
160178 * adjust if it does not.
161179 */
162180 public void updatePerformance () {
163181 numSwaps = frame .getArrDisplay ().getSwappedIndexes ().size ();
164- if (!getSort ().equals ("Not Sorting" ) && frame .getArrDisplay ().getNumChunks () == 0 ) {
165- visualizationTime = System .currentTimeMillis () - startTime ;
166- sortingTime = visualizationTime - totalDelay ;
167- } else if (frame .getArrDisplay ().getNumChunks () > 1 && !frame .getArrDisplay ().isComplete ()) {
168- visualizationTime = System .currentTimeMillis () - startTime ;
169- sortingTime = visualizationTime - totalDelay ;
170- }
182+
183+ // updates the sorting times based on whether or not we are currently visualizing a sort
184+ if (!getSort ().equals ("Not Sorting" ) && frame .getArrDisplay ().getNumChunks () == 0 ) {
185+
186+ visualizationTime = System .currentTimeMillis () - startTime ;
187+ sortingTime = visualizationTime - totalDelay ;
188+
189+ } else if (frame .getArrDisplay ().getNumChunks () > 1 && !frame .getArrDisplay ().isComplete ()) {
190+
191+ visualizationTime = System .currentTimeMillis () - startTime ;
192+ sortingTime = visualizationTime - totalDelay ; // visualizationTime < totalDelay makes negative time
193+
194+ }
195+
171196 if (stopSort ) {
172197 resetTime ();
173198 }
199+
174200 String performance = String .format (
175201 "Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms" ,
176202 indexComparisons , numSwaps , visualizationTime , sortingTime );
0 commit comments