2020
2121public class AlgVisualizer implements ActionListener , ChangeListener {
2222
23- private final int FPS_MIN = 2 ;
23+ private final int FPS_MIN = 2 ;
2424 private final int FPS_INIT = 10 ;
2525 private final int FPS_MAX = 100 ;
2626 private String [] sizeOptions = { "10" , "50" , "100" , "300" , "450" , "900" }; // array size options
2727 private int n ;
2828 private int numSwaps ;
2929 private int delay ;
3030 private Integer indexComparisons ;
31- private long startTime ; // start time of a sort
31+ private long startTime ; // start time of a sort
3232 private long visualizationTime ;
3333 private boolean doBubbleSort ;
3434 private boolean doInsertionSort ;
3535 private boolean doSelectionSort ;
3636 private boolean doMergeSort ;
3737 private boolean doQuickSort ;
38- private boolean stopSort ; // True if sorting is stopped
39- private Integer [] arr ; // array that is going to be sorted
38+ private boolean stopSort ; // True if sorting is stopped
39+ private Integer [] arr ; // array that is going to be sorted
4040 private ContentWindow frame ;
4141 private SwingWorker <Void , Integer []> arrSort ;
4242
43-
4443 /*
4544 * actionPerformed(ActionEvent event)
46- *
47- * When an action is performed on a component on the JFrame that has had this
45+ *
46+ * When an action is performed on a component on the JFrame that has had this
4847 * classes actionListener added to it, this method will decide what to do based
4948 * on the event.
50- *
51- * When a sorting button is clicked, its
52- * respective boolean do(..)Sort will be set to true, and arrSort().execute.
53- * This will call the doInBackground() method in the arrSort object, where it
54- * will use the do(..)Sort variable to discover which sorting algorithm to use,
55- * or if there is simply a reset.
49+ *
50+ * When a sorting button is clicked, its respective boolean do(..)Sort will be
51+ * set to true, and arrSort().execute. This will call the doInBackground()
52+ * method in the arrSort object, where it will use the do(..)Sort variable to
53+ * discover which sorting algorithm to use, or if there is simply a reset.
5654 */
5755 public void actionPerformed (ActionEvent event ) {
5856 // Any time an action is performed, sorting is stopped
@@ -64,7 +62,7 @@ public void actionPerformed(ActionEvent event) {
6462 doQuickSort = false ;
6563
6664 // Find the source of the action
67- // Is there a better way to do this?
65+ // Is there a better way to do this?
6866 if (event .getSource () == frame .getBubbleButton ()) {
6967 doBubbleSort = true ;
7068 arrSort .execute ();
@@ -93,34 +91,34 @@ public void actionPerformed(ActionEvent event) {
9391 }
9492 }
9593
96- /*
97- * stateChanged(ChangeEvent e)
98- *
99- * This method is called when the FPS slider
100- * is shifted to change the FPS of sorting.
101- *
102- * We change the amount of delay occuring in the
103- * sorting to what the value of the slider was moved to.
104- */
94+ /*
95+ * stateChanged(ChangeEvent e)
96+ *
97+ * This method is called when the FPS slider is shifted to change the FPS of
98+ * sorting.
99+ *
100+ * We change the amount of delay occuring in the sorting to what the value of
101+ * the slider was moved to.
102+ */
105103 @ Override
106104 public void stateChanged (ChangeEvent e ) {
107105 JSlider source = (JSlider ) e .getSource ();
108- int fps = (int ) source .getValue ();
109- delay = 1000 / fps ; // ms
110- setDelay (delay );
106+ int fps = (int ) source .getValue ();
107+ delay = 1000 / fps ; // ms
108+ setDelay (delay );
111109 }
112110
113- /*
114- * reset()
115- *
111+ /*
112+ * reset()
113+ *
116114 * Reset method is called whenever the user presses the reset button, or when a
117- * new size of array is chosen from the size changer.
118- *
119- * This method stops sorting, re-shuffles the array, clears all swapped indexes, frames painted, tracked
120- * time, and comparisons. It must also reset the swingWorker so that the user is
121- * able to see another sort. Since sort.execute() can only be called once for
122- * SwingWorker, we simply re-instantiate it so that we are able to call it
123- * again.
115+ * new size of array is chosen from the size changer.
116+ *
117+ * This method stops sorting, re-shuffles the array, clears all swapped indexes,
118+ * frames painted, tracked time, and comparisons. It must also reset the
119+ * swingWorker so that the user is able to see another sort. Since
120+ * sort.execute() can only be called once for SwingWorker, we simply
121+ * re-instantiate it so that we are able to call it again.
124122 */
125123 public void reset () {
126124 setStopSort (true );
@@ -162,9 +160,9 @@ public Integer[] fillArr(Integer[] arr) {
162160
163161 /*
164162 * updatePerformance()
165- *
166- * This method will be called every time that the frame is updated in order to
167- * update our performance statistics.
163+ *
164+ * This method will be called every time that the frame is updated in order to
165+ * update our performance statistics.
168166 *
169167 * Finds the values for each performance statistic being tracked (number of
170168 * swaps, number of comparisons, visualization time, sorting time), formats them
@@ -175,17 +173,17 @@ public Integer[] fillArr(Integer[] arr) {
175173 */
176174 public void updatePerformance () {
177175 numSwaps = frame .getArrDisplay ().getSwappedIndexes ().size ();
178-
176+
179177 if (stopSort ) {
180178 resetTime ();
181179 } else {
182- if (!frame .getArrDisplay ().isComplete () && !getSort ().equals ("Not Sorting" ) && (frame .getArrDisplay ().getNumChunks () == 0 || frame .getArrDisplay ().getNumChunks () > 1 )) {
183- visualizationTime = System .currentTimeMillis () - startTime ;
184- }
185- }
180+ if (!frame .getArrDisplay ().isComplete () && !getSort ().equals ("Not Sorting" )
181+ && (frame .getArrDisplay ().getNumChunks () == 0 || frame .getArrDisplay ().getNumChunks () > 1 )) {
182+ visualizationTime = System .currentTimeMillis () - startTime ;
183+ }
184+ }
186185
187- String performance = String .format (
188- "Index Comparisons : %d Index Swaps : %d Visualization Time : %dms" ,
186+ String performance = String .format ("Index Comparisons : %d Index Swaps : %d Visualization Time : %dms" ,
189187 indexComparisons , numSwaps , visualizationTime );
190188 frame .getPerformanceLabel ().setText (performance );
191189 frame .pack ();
0 commit comments