20
20
21
21
public class AlgVisualizer implements ActionListener , ChangeListener {
22
22
23
- private final int FPS_MIN = 2 ;
23
+ private final int FPS_MIN = 2 ;
24
24
private final int FPS_INIT = 10 ;
25
25
private final int FPS_MAX = 100 ;
26
26
private String [] sizeOptions = { "10" , "50" , "100" , "300" , "450" , "900" }; // array size options
27
27
private int n ;
28
28
private int numSwaps ;
29
29
private int delay ;
30
30
private Integer indexComparisons ;
31
- private long startTime ; // start time of a sort
31
+ private long startTime ; // start time of a sort
32
32
private long visualizationTime ;
33
33
private boolean doBubbleSort ;
34
34
private boolean doInsertionSort ;
35
35
private boolean doSelectionSort ;
36
36
private boolean doMergeSort ;
37
37
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
40
40
private ContentWindow frame ;
41
41
private SwingWorker <Void , Integer []> arrSort ;
42
42
43
-
44
43
/*
45
44
* 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
48
47
* classes actionListener added to it, this method will decide what to do based
49
48
* 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.
56
54
*/
57
55
public void actionPerformed (ActionEvent event ) {
58
56
// Any time an action is performed, sorting is stopped
@@ -64,7 +62,7 @@ public void actionPerformed(ActionEvent event) {
64
62
doQuickSort = false ;
65
63
66
64
// Find the source of the action
67
- // Is there a better way to do this?
65
+ // Is there a better way to do this?
68
66
if (event .getSource () == frame .getBubbleButton ()) {
69
67
doBubbleSort = true ;
70
68
arrSort .execute ();
@@ -93,34 +91,34 @@ public void actionPerformed(ActionEvent event) {
93
91
}
94
92
}
95
93
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
+ */
105
103
@ Override
106
104
public void stateChanged (ChangeEvent e ) {
107
105
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 );
111
109
}
112
110
113
- /*
114
- * reset()
115
- *
111
+ /*
112
+ * reset()
113
+ *
116
114
* 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.
124
122
*/
125
123
public void reset () {
126
124
setStopSort (true );
@@ -162,9 +160,9 @@ public Integer[] fillArr(Integer[] arr) {
162
160
163
161
/*
164
162
* 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.
168
166
*
169
167
* Finds the values for each performance statistic being tracked (number of
170
168
* swaps, number of comparisons, visualization time, sorting time), formats them
@@ -175,17 +173,17 @@ public Integer[] fillArr(Integer[] arr) {
175
173
*/
176
174
public void updatePerformance () {
177
175
numSwaps = frame .getArrDisplay ().getSwappedIndexes ().size ();
178
-
176
+
179
177
if (stopSort ) {
180
178
resetTime ();
181
179
} 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
+ }
186
185
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" ,
189
187
indexComparisons , numSwaps , visualizationTime );
190
188
frame .getPerformanceLabel ().setText (performance );
191
189
frame .pack ();
0 commit comments