1
+ /*
2
+ * An application that visualizes sorting algorithms through bar graphs.
3
+ *
4
+ * Author : Daniel La Rocque
5
+ *
6
+ * Date started : April 28th, 2020
7
+ *
8
+ * This project is licensed under the MIT License.
9
+ * See LICENSE.txt for more details
10
+ *
11
+ * Instructions on how to use the application are included in README.md
12
+ */
13
+
1
14
package com .example .algorithmvisualizer ;
2
15
3
16
import java .awt .BorderLayout ;
@@ -13,52 +26,82 @@ public class AlgVisualizer implements ActionListener {
13
26
private final int CONTENT_WIDTH = 800 ;
14
27
private final int CONTENT_HEIGHT = 860 ;
15
28
private final int ARR_DISPLAY_HEIGHT = 800 ;
16
- private Integer [] arr ;
17
- private JFrame frame = new JFrame ("Algorithm Visualizer" );
29
+ private final String [] SIZE_OPTIONS = { "10" , "50" , "100" , "200" , "400" , "800" }; // array size options
30
+ private Integer indexComparisons ;
31
+ private long startTime ; // start time of a sort
32
+ private long endTime ; // end time of a sort
33
+ private boolean doBubbleSort ;
34
+ private boolean doInsertionSort ;
35
+ private boolean doSelectionSort ;
36
+ private boolean doMergeSort ;
37
+ private boolean doQuickSort ;
38
+ private boolean stopSort ; // True if sorting is stopped
39
+ private Integer [] arr ; // array that is going to be sorted
40
+ private JFrame frame ;
18
41
private JPanel arrPanel ;
19
42
private ArrDisplay arrDisplay ;
20
- private JButton performanceButton ;
21
43
private JPanel buttonPanel ;
44
+ private JButton resetButton ;
22
45
private JButton bubbleButton ;
23
46
private JButton insertionButton ;
24
47
private JButton selectionButton ;
25
48
private JButton mergeButton ;
26
49
private JButton quickButton ;
27
- private JButton resetButton ;
50
+ private JButton performanceButton ;
28
51
private JComboBox <String > sizeChanger ;
29
- private final String [] SIZE_OPTIONS = { "10" , "50" , "100" , "200" , "400" , "800" };
30
52
private SwingWorker <Void , Integer []> arrSort ;
31
- private boolean doBubbleSort ;
32
- private boolean doInsertionSort ;
33
- private boolean doSelectionSort ;
34
- private boolean doMergeSort ;
35
- private boolean doQuickSort ;
36
- private boolean stopSort ;
37
- private Integer indexComparisons ;
38
- private long startTime ;
39
- private long endTime ;
40
53
54
+ /*
55
+ * In main(), we initialize an AlgVisualizer object, all instance variables, set
56
+ * up the frame / window that the application will run inside, and make it
57
+ * visible. By the end, a window containing what is meant to be shown on
58
+ * application start up will open on the users screen, waiting for input.
59
+ */
41
60
public static void main (String [] args ) {
42
61
AlgVisualizer algVisualizer = new AlgVisualizer ();
43
62
algVisualizer .initializeVars ();
44
63
algVisualizer .setFrame ();
45
64
}
46
65
66
+ public AlgVisualizer () {
67
+ // empty constructor, variables are initialized in main method.
68
+ }
69
+
70
+ /*
71
+ * This method initializes all of this classes instance variables. The array is
72
+ * initialized, filled, and shuffled. The arrDisplay object that paints the
73
+ * array in bar graph form is initialized and passed the array. All buttons are
74
+ * initialized and include an action listener.
75
+ *
76
+ */
47
77
public void initializeVars () {
48
78
49
79
setN (10 );
50
80
51
81
arr = new Integer [n ];
52
82
arr = fillArr (arr );
53
83
arr = shuffleArr (arr );
54
-
84
+
55
85
indexComparisons = 0 ;
56
86
87
+ // Initialize objects that will display and sort the array
88
+
57
89
arrDisplay = new ArrDisplay (this , arr );
58
90
arrDisplay .setPreferredSize (new Dimension (CONTENT_WIDTH , ARR_DISPLAY_HEIGHT ));
59
-
91
+
60
92
arrSort = new ArrSorting (this , this .arr , this .arrDisplay );
61
-
93
+
94
+ // Panels in the frame that will hold all components.
95
+ // JPanels use the flowLayout to manage their components automatically.
96
+
97
+ buttonPanel = new JPanel ();
98
+ buttonPanel .setBackground (Color .DARK_GRAY );
99
+
100
+ arrPanel = new JPanel ();
101
+ arrPanel .add (arrDisplay );
102
+
103
+ // Initialize all components and add action listeners
104
+
62
105
resetButton = new JButton ("Reset" );
63
106
resetButton .addActionListener (this );
64
107
resetButton .setBackground (Color .WHITE );
@@ -83,20 +126,24 @@ public void initializeVars() {
83
126
quickButton .addActionListener (this );
84
127
quickButton .setBackground (Color .WHITE );
85
128
86
- sizeChanger = new JComboBox <String >(SIZE_OPTIONS );
129
+ sizeChanger = new JComboBox <String >(SIZE_OPTIONS ); // Pass the String containing all of the size options
87
130
sizeChanger .addActionListener (this );
88
131
sizeChanger .setBackground (Color .WHITE );
89
-
132
+
90
133
performanceButton = new JButton ("Performance" );
91
134
performanceButton .addActionListener (this );
92
135
performanceButton .setBackground (Color .WHITE );
93
- performanceButton .setEnabled (false );
136
+ performanceButton .setEnabled (false ); // This button is not available until a sort is complete
94
137
}
95
138
139
+ /*
140
+ * setFrame() will add all the components that were initialized in the
141
+ * initializeVars() method to JPanels, which will then also be added to the main
142
+ * JFrame. The frame is initialized and made visible.
143
+ */
96
144
public void setFrame () {
97
145
98
- buttonPanel = new JPanel ();
99
- buttonPanel .setBackground (Color .DARK_GRAY );
146
+ // Add JButtons / components to button panel
100
147
buttonPanel .add (resetButton );
101
148
buttonPanel .add (bubbleButton );
102
149
buttonPanel .add (selectionButton );
@@ -105,29 +152,38 @@ public void setFrame() {
105
152
buttonPanel .add (quickButton );
106
153
buttonPanel .add (sizeChanger );
107
154
buttonPanel .add (performanceButton );
108
- buttonPanel .setVisible (true );
109
155
110
- arrPanel = new JPanel ();
111
- arrPanel .add (arrDisplay );
112
- arrPanel .setVisible (true );
113
-
114
- frame .setVisible (true );
156
+ // Initialize and make the frame visible
157
+ frame = new JFrame ("Algorithm Visualizer" );
115
158
frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
116
- frame .setResizable (false );
117
- frame .add (buttonPanel , BorderLayout .PAGE_START );
118
- frame .add (arrPanel , BorderLayout .PAGE_END );
159
+ frame .setResizable (false ); // Cannot be resizable, causes visual issues
160
+ frame .add (buttonPanel , BorderLayout .PAGE_START ); // Button panel added to the top of the frame
161
+ frame .add (arrPanel , BorderLayout .PAGE_END ); // Array display is added to the bottom of the frame
119
162
frame .pack ();
120
- frame .setLocationRelativeTo (null );
163
+ frame .setLocationRelativeTo (null ); // center of the screen
164
+ frame .setVisible (true );
165
+
121
166
}
122
167
168
+ /*
169
+ * When an action is performed on a component on the JFrame that has had this
170
+ * classes actionListener added to it, this method will decide what to do based
171
+ * on what component was clicked on. When a sorting button is clicked, its
172
+ * respective boolean do(..)Sort will be set to true, and arrSort().execute.
173
+ * This will call the doInBackground() method in the arrSort object, where it
174
+ * will use the do(..)Sort variable to discover which sorting algorithm to use,
175
+ * or if there is simply a reset.
176
+ */
123
177
public void actionPerformed (ActionEvent event ) {
178
+ // Any time an action is performed, sorting is stopped
124
179
setStopSort (false );
125
180
doBubbleSort = false ;
126
181
doSelectionSort = false ;
127
182
doInsertionSort = false ;
128
183
doMergeSort = false ;
129
184
doQuickSort = false ;
130
-
185
+
186
+ // Find the source of the action
131
187
if (event .getSource () == bubbleButton ) {
132
188
doBubbleSort = true ;
133
189
arrSort .execute ();
@@ -147,24 +203,37 @@ public void actionPerformed(ActionEvent event) {
147
203
reset ();
148
204
arrSort .execute ();
149
205
} else if (event .getSource () == sizeChanger ) {
150
- // Create a new array of the size selected
206
+ // Find what size was selected, and set n to that value
151
207
String selectedSize = (String ) sizeChanger .getSelectedItem ();
152
208
setN (Integer .valueOf (selectedSize ));
209
+
210
+ // Create the new array of length n, and it will be shuffled in reset()
153
211
arr = new Integer [n ];
154
212
arr = fillArr (arr );
155
- // Clear and paint the new array
213
+
214
+ // reset and paint the new array
156
215
reset ();
157
216
arrSort .execute ();
158
217
} else if (event .getSource () == performanceButton ) {
159
- //open JOptionPane
160
218
int numSwaps = arrDisplay .getSwappedIndexes ().size ();
161
- long timeElapsed = endTime - startTime ;
162
- long realTimeElapsed = timeElapsed - (60 * numSwaps );
163
- String statsMessage = String .format ("Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms" , indexComparisons , numSwaps , timeElapsed , realTimeElapsed );
219
+ long visualizationTime = endTime - startTime ; // net time
220
+ long sortingTime = visualizationTime - (60 * numSwaps + 1 ); // - 60 seconds of sleep time between each swap
221
+ String statsMessage = String .format (
222
+ "Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms" ,
223
+ indexComparisons , numSwaps , visualizationTime , sortingTime );
164
224
JOptionPane .showMessageDialog (frame , statsMessage , "Performance" , JOptionPane .PLAIN_MESSAGE );
165
225
}
166
226
}
167
227
228
+ /*
229
+ * Reset method is called whenever the user presses the reset button, or when a
230
+ * new size of array is chosen from the size changer. This method stops sorting,
231
+ * re-shuffles the array, clears all swapped indexes, frames painted, tracked
232
+ * time, and comparisons. It must also reset the swingWorker so that the user is
233
+ * able to see another sort. Since sort.execute() can only be called once for
234
+ * SwingWorker, we simply re-instantiate it so that we are able to call it
235
+ * again.
236
+ */
168
237
public void reset () {
169
238
setStopSort (true );
170
239
arr = shuffleArr (arr );
@@ -177,11 +246,14 @@ public void reset() {
177
246
resetSwingWorker (this , arr , arrDisplay );
178
247
}
179
248
249
+ // Re-instantiates the SwingWorker so that execute() can be called again.
180
250
public void resetSwingWorker (AlgVisualizer alg , Integer [] arr , ArrDisplay displayArr ) {
181
- arrSort = new ArrSorting (this , arr , displayArr ); // need to reset arrAccesses and timeElapsed
251
+ arrSort = new ArrSorting (this , arr , displayArr );
182
252
}
183
-
184
- public void resetTime (){
253
+
254
+ // Reset the timer on the previous sort that was done, used in the reset()
255
+ // method
256
+ public void resetTime () {
185
257
startTime = 0 ;
186
258
endTime = 0 ;
187
259
}
@@ -243,6 +315,10 @@ public void setSort(String sort) {
243
315
}
244
316
}
245
317
318
+ /*
319
+ * getSort() returns a string containing the sorting algorithm that is currently
320
+ * being used to sort the array.
321
+ */
246
322
public String getSort () {
247
323
String sort = "Not Sorting" ;
248
324
if (doBubbleSort )
@@ -258,12 +334,21 @@ public String getSort() {
258
334
return sort ;
259
335
}
260
336
337
+ /*
338
+ * Returns the boolean value representing the status of the application. If
339
+ * there is currently a sorting algorithm being used, it will return false.
340
+ */
261
341
public boolean stopSort () {
262
342
return stopSort ;
263
343
}
264
344
345
+ /*
346
+ * Since the availability of the buttons is based on the stopSort variable, we
347
+ * control them from this setter method. If we want to stop sorting, we enable
348
+ * all of the sorting buttons again, if we are starting a sorting algorithm,
349
+ * disable them.
350
+ */
265
351
public void setStopSort (boolean toSet ) {
266
- // The availability of the sort buttons depends on the status of stopSort
267
352
bubbleButton .setEnabled (toSet );
268
353
selectionButton .setEnabled (toSet );
269
354
insertionButton .setEnabled (toSet );
@@ -279,15 +364,15 @@ public int getN() {
279
364
public void setN (int n ) {
280
365
this .n = n ;
281
366
}
282
-
283
- public JButton getStatsButton () {
367
+
368
+ public JButton getPerformanceButton () {
284
369
return performanceButton ;
285
370
}
286
-
371
+
287
372
public Integer getIndexComparisons () {
288
373
return indexComparisons ;
289
374
}
290
-
375
+
291
376
public void setIndexComparisons (int indexComparisons ) {
292
377
this .indexComparisons = indexComparisons ;
293
378
}
@@ -306,5 +391,5 @@ public long getEndTime() {
306
391
307
392
public void setEndTime (long endTime ) {
308
393
this .endTime = endTime ;
309
- }
394
+ }
310
395
}
0 commit comments