Skip to content

Commit a1ce5ef

Browse files
committed
Added Comments to all source code
Added comments (not documentation, as it isn't really necessary here) to all of the .java files
1 parent b882189 commit a1ce5ef

File tree

3 files changed

+261
-90
lines changed

3 files changed

+261
-90
lines changed

Diff for: src/com/example/algorithmvisualizer/AlgVisualizer.java

+133-48
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
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+
114
package com.example.algorithmvisualizer;
215

316
import java.awt.BorderLayout;
@@ -13,52 +26,82 @@ public class AlgVisualizer implements ActionListener {
1326
private final int CONTENT_WIDTH = 800;
1427
private final int CONTENT_HEIGHT = 860;
1528
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;
1841
private JPanel arrPanel;
1942
private ArrDisplay arrDisplay;
20-
private JButton performanceButton;
2143
private JPanel buttonPanel;
44+
private JButton resetButton;
2245
private JButton bubbleButton;
2346
private JButton insertionButton;
2447
private JButton selectionButton;
2548
private JButton mergeButton;
2649
private JButton quickButton;
27-
private JButton resetButton;
50+
private JButton performanceButton;
2851
private JComboBox<String> sizeChanger;
29-
private final String[] SIZE_OPTIONS = { "10", "50", "100", "200", "400", "800" };
3052
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;
4053

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+
*/
4160
public static void main(String[] args) {
4261
AlgVisualizer algVisualizer = new AlgVisualizer();
4362
algVisualizer.initializeVars();
4463
algVisualizer.setFrame();
4564
}
4665

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+
*/
4777
public void initializeVars() {
4878

4979
setN(10);
5080

5181
arr = new Integer[n];
5282
arr = fillArr(arr);
5383
arr = shuffleArr(arr);
54-
84+
5585
indexComparisons = 0;
5686

87+
// Initialize objects that will display and sort the array
88+
5789
arrDisplay = new ArrDisplay(this, arr);
5890
arrDisplay.setPreferredSize(new Dimension(CONTENT_WIDTH, ARR_DISPLAY_HEIGHT));
59-
91+
6092
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+
62105
resetButton = new JButton("Reset");
63106
resetButton.addActionListener(this);
64107
resetButton.setBackground(Color.WHITE);
@@ -83,20 +126,24 @@ public void initializeVars() {
83126
quickButton.addActionListener(this);
84127
quickButton.setBackground(Color.WHITE);
85128

86-
sizeChanger = new JComboBox<String>(SIZE_OPTIONS);
129+
sizeChanger = new JComboBox<String>(SIZE_OPTIONS); // Pass the String containing all of the size options
87130
sizeChanger.addActionListener(this);
88131
sizeChanger.setBackground(Color.WHITE);
89-
132+
90133
performanceButton = new JButton("Performance");
91134
performanceButton.addActionListener(this);
92135
performanceButton.setBackground(Color.WHITE);
93-
performanceButton.setEnabled(false);
136+
performanceButton.setEnabled(false); // This button is not available until a sort is complete
94137
}
95138

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+
*/
96144
public void setFrame() {
97145

98-
buttonPanel = new JPanel();
99-
buttonPanel.setBackground(Color.DARK_GRAY);
146+
// Add JButtons / components to button panel
100147
buttonPanel.add(resetButton);
101148
buttonPanel.add(bubbleButton);
102149
buttonPanel.add(selectionButton);
@@ -105,29 +152,38 @@ public void setFrame() {
105152
buttonPanel.add(quickButton);
106153
buttonPanel.add(sizeChanger);
107154
buttonPanel.add(performanceButton);
108-
buttonPanel.setVisible(true);
109155

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");
115158
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
119162
frame.pack();
120-
frame.setLocationRelativeTo(null);
163+
frame.setLocationRelativeTo(null); // center of the screen
164+
frame.setVisible(true);
165+
121166
}
122167

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+
*/
123177
public void actionPerformed(ActionEvent event) {
178+
// Any time an action is performed, sorting is stopped
124179
setStopSort(false);
125180
doBubbleSort = false;
126181
doSelectionSort = false;
127182
doInsertionSort = false;
128183
doMergeSort = false;
129184
doQuickSort = false;
130-
185+
186+
// Find the source of the action
131187
if (event.getSource() == bubbleButton) {
132188
doBubbleSort = true;
133189
arrSort.execute();
@@ -147,24 +203,37 @@ public void actionPerformed(ActionEvent event) {
147203
reset();
148204
arrSort.execute();
149205
} 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
151207
String selectedSize = (String) sizeChanger.getSelectedItem();
152208
setN(Integer.valueOf(selectedSize));
209+
210+
// Create the new array of length n, and it will be shuffled in reset()
153211
arr = new Integer[n];
154212
arr = fillArr(arr);
155-
// Clear and paint the new array
213+
214+
// reset and paint the new array
156215
reset();
157216
arrSort.execute();
158217
} else if (event.getSource() == performanceButton) {
159-
//open JOptionPane
160218
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);
164224
JOptionPane.showMessageDialog(frame, statsMessage, "Performance", JOptionPane.PLAIN_MESSAGE);
165225
}
166226
}
167227

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+
*/
168237
public void reset() {
169238
setStopSort(true);
170239
arr = shuffleArr(arr);
@@ -177,11 +246,14 @@ public void reset() {
177246
resetSwingWorker(this, arr, arrDisplay);
178247
}
179248

249+
// Re-instantiates the SwingWorker so that execute() can be called again.
180250
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);
182252
}
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() {
185257
startTime = 0;
186258
endTime = 0;
187259
}
@@ -243,6 +315,10 @@ public void setSort(String sort) {
243315
}
244316
}
245317

318+
/*
319+
* getSort() returns a string containing the sorting algorithm that is currently
320+
* being used to sort the array.
321+
*/
246322
public String getSort() {
247323
String sort = "Not Sorting";
248324
if (doBubbleSort)
@@ -258,12 +334,21 @@ public String getSort() {
258334
return sort;
259335
}
260336

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+
*/
261341
public boolean stopSort() {
262342
return stopSort;
263343
}
264344

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+
*/
265351
public void setStopSort(boolean toSet) {
266-
// The availability of the sort buttons depends on the status of stopSort
267352
bubbleButton.setEnabled(toSet);
268353
selectionButton.setEnabled(toSet);
269354
insertionButton.setEnabled(toSet);
@@ -279,15 +364,15 @@ public int getN() {
279364
public void setN(int n) {
280365
this.n = n;
281366
}
282-
283-
public JButton getStatsButton() {
367+
368+
public JButton getPerformanceButton() {
284369
return performanceButton;
285370
}
286-
371+
287372
public Integer getIndexComparisons() {
288373
return indexComparisons;
289374
}
290-
375+
291376
public void setIndexComparisons(int indexComparisons) {
292377
this.indexComparisons = indexComparisons;
293378
}
@@ -306,5 +391,5 @@ public long getEndTime() {
306391

307392
public void setEndTime(long endTime) {
308393
this.endTime = endTime;
309-
}
394+
}
310395
}

0 commit comments

Comments
 (0)