2626public class AlgVisualizer implements ActionListener , ChangeListener {
2727
2828 private final int CONTENT_WIDTH = 900 ;
29- private final int CONTENT_HEIGHT = 960 ;
3029 private final int ARR_DISPLAY_HEIGHT = 900 ;
31- private final int FPS_MIN = 1 ;
30+ private final int FPS_MIN = 2 ;
3231 private final int FPS_INIT = 10 ;
3332 private final int FPS_MAX = 100 ;
3433 private final String [] SIZE_OPTIONS = { "10" , "50" , "100" , "300" , "450" , "900" }; // array size options
@@ -38,7 +37,6 @@ public class AlgVisualizer implements ActionListener, ChangeListener {
3837 private long totalDelay ;
3938 private Integer indexComparisons ;
4039 private long startTime ; // start time of a sort
41- private long endTime ; // end time of a sort
4240 private long visualizationTime ;
4341 private long sortingTime ;
4442 private boolean doBubbleSort ;
@@ -74,11 +72,7 @@ public static void main(String[] args) {
7472 algVisualizer .initializeVars ();
7573 algVisualizer .setFrame ();
7674 }
77-
78- public AlgVisualizer () {
79- // empty constructor, variables are initialized in main method.
80- }
81-
75+
8276 /*
8377 * This method initializes all of this classes instance variables. The array is
8478 * initialized, filled, and shuffled. The arrDisplay object that paints the
@@ -88,21 +82,13 @@ public AlgVisualizer() {
8882 */
8983 public void initializeVars () {
9084
91- setN (10 );
92-
93- arr = new Integer [n ];
94- arr = fillArr (arr );
95- arr = shuffleArr (arr );
85+ n = Integer .parseInt (SIZE_OPTIONS [0 ]);
86+ arr = initArr ();
9687
9788 indexComparisons = 0 ;
98- startTime = 0 ;
99- endTime = 0 ;
100- visualizationTime = 0 ;
101- sortingTime = 0 ;
10289 setDelay (1000 / FPS_INIT );
10390
10491 // Initialize objects that will display and sort the array
105-
10692 arrDisplay = new ArrDisplay (this );
10793 arrDisplay .setArr (arr );
10894 arrDisplay .setPreferredSize (new Dimension (CONTENT_WIDTH , ARR_DISPLAY_HEIGHT ));
@@ -111,15 +97,13 @@ public void initializeVars() {
11197
11298 // Panels in the frame that will hold all components.
11399 // JPanels use the flowLayout to manage their components automatically.
114-
115100 buttonPanel = new JPanel ();
116101 buttonPanel .setBackground (Color .DARK_GRAY );
117102
118103 arrPanel = new JPanel ();
119104 arrPanel .add (arrDisplay );
120105
121106 // Initialize all components and add action listeners
122-
123107 resetButton = new JButton ("Reset" );
124108 resetButton .addActionListener (this );
125109 resetButton .setBackground (Color .WHITE );
@@ -152,7 +136,6 @@ public void initializeVars() {
152136 FPSslider .addChangeListener (this );
153137 FPSslider .setBackground (Color .DARK_GRAY );
154138 // Initialize the performance label and center it
155-
156139 performanceLabel = new JLabel ();
157140 performanceLabel .setHorizontalAlignment (SwingConstants .CENTER );
158141 }
@@ -163,7 +146,6 @@ public void initializeVars() {
163146 * JFrame. The frame is initialized and made visible.
164147 */
165148 public void setFrame () {
166-
167149 // Add JButtons / components to button panel
168150 buttonPanel .add (resetButton );
169151 buttonPanel .add (bubbleButton );
@@ -173,7 +155,6 @@ public void setFrame() {
173155 buttonPanel .add (quickButton );
174156 buttonPanel .add (sizeChanger );
175157 buttonPanel .add (FPSslider );
176-
177158 // Initialize and make the frame visible
178159 frame = new JFrame ("Algorithm Visualizer" );
179160 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
@@ -184,7 +165,6 @@ public void setFrame() {
184165 frame .pack ();
185166 frame .setLocationRelativeTo (null ); // center of the screen
186167 frame .setVisible (true );
187-
188168 }
189169
190170 /*
@@ -204,7 +184,6 @@ public void actionPerformed(ActionEvent event) {
204184 doInsertionSort = false ;
205185 doMergeSort = false ;
206186 doQuickSort = false ;
207-
208187 // Find the source of the action
209188 if (event .getSource () == bubbleButton ) {
210189 doBubbleSort = true ;
@@ -227,12 +206,7 @@ public void actionPerformed(ActionEvent event) {
227206 } else if (event .getSource () == sizeChanger ) {
228207 // Find what size was selected, and set n to that value
229208 String selectedSize = (String ) sizeChanger .getSelectedItem ();
230- setN (Integer .valueOf (selectedSize ));
231-
232- // Create the new array of length n, and it will be shuffled in reset()
233- arr = new Integer [n ];
234- arr = fillArr (arr );
235-
209+ n = Integer .valueOf (selectedSize );
236210 // reset and paint the new array
237211 reset ();
238212 arrSort .execute ();
@@ -258,12 +232,12 @@ public void stateChanged(ChangeEvent e) {
258232 * SwingWorker, we simply re-instantiate it so that we are able to call it
259233 * again.
260234 */
235+
261236 public void reset () {
262237 setStopSort (true );
263- arr = shuffleArr ( arr );
238+ arr = initArr ( );
264239 arrDisplay .clearSwappedIndexes ();
265240 arrDisplay .setComplete (false );
266- arrDisplay .setArr (arr );
267241 indexComparisons = 0 ;
268242 resetTime ();
269243 resetSwingWorker (this , arr , arrDisplay );
@@ -278,11 +252,17 @@ public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displa
278252 // method
279253 public void resetTime () {
280254 startTime = 0 ;
281- endTime = 0 ;
282255 visualizationTime = 0 ;
283256 sortingTime = 0 ;
284257 totalDelay = 0 ;
285258 }
259+
260+ public Integer [] initArr () {
261+ Integer [] arr = new Integer [n ];
262+ arr = fillArr (arr );
263+ arr = shuffleArr (arr );
264+ return arr ;
265+ }
286266
287267 public Integer [] shuffleArr (Integer [] arr ) {
288268 arr = fillArr (arr );
@@ -312,24 +292,20 @@ public Integer[] fillArr(Integer[] arr) {
312292 */
313293 public void updatePerformance () {
314294 numSwaps = arrDisplay .getSwappedIndexes ().size ();
315- System .out .println ("total delay " + totalDelay );
316- if (!getSort ().equals ("Not Sorting" ) && arrDisplay .getNumChunks () == 1 ) {
295+ if (!getSort ().equals ("Not Sorting" ) && arrDisplay .getNumChunks () == 0 ) {
317296 visualizationTime = System .currentTimeMillis () - startTime ;
318297 sortingTime = visualizationTime - totalDelay ;
319- } else if (arrDisplay .getNumChunks () > 1 ) {
298+ } else if (arrDisplay .getNumChunks () > 1 && ! arrDisplay . isComplete () ) {
320299 visualizationTime = System .currentTimeMillis () - startTime ;
321300 sortingTime = visualizationTime - totalDelay ;
322301 }
323302 if (stopSort ) {
324303 resetTime ();
325304 }
326-
327305 String performance = String .format (
328306 "Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms" ,
329307 indexComparisons , numSwaps , visualizationTime , sortingTime );
330-
331308 performanceLabel .setText (performance );
332-
333309 frame .pack ();
334310 }
335311
@@ -341,10 +317,6 @@ public int getArrDispHeight() {
341317 return ARR_DISPLAY_HEIGHT ;
342318 }
343319
344- public int getHeight () {
345- return CONTENT_HEIGHT ;
346- }
347-
348320 public int getWidth () {
349321 return CONTENT_WIDTH ;
350322 }
@@ -417,14 +389,6 @@ public void setStopSort(boolean toSet) {
417389 stopSort = toSet ;
418390 }
419391
420- public int getN () {
421- return n ;
422- }
423-
424- public void setN (int n ) {
425- this .n = n ;
426- }
427-
428392 public Integer getIndexComparisons () {
429393 return indexComparisons ;
430394 }
@@ -433,30 +397,10 @@ public void setIndexComparisons(int indexComparisons) {
433397 this .indexComparisons = indexComparisons ;
434398 }
435399
436- public long getStartTime () {
437- return startTime ;
438- }
439-
440400 public void setStartTime (long startTime ) {
441401 this .startTime = startTime ;
442402 }
443403
444- public long getEndTime () {
445- return endTime ;
446- }
447-
448- public void setEndTime (long endTime ) {
449- this .endTime = endTime ;
450- }
451-
452- public JLabel getPerformanceLabel () {
453- return performanceLabel ;
454- }
455-
456- public void setPerformanceLabel (JLabel performanceLabel ) {
457- this .performanceLabel = performanceLabel ;
458- }
459-
460404 public int getNumSwaps () {
461405 return numSwaps ;
462406 }
@@ -473,14 +417,6 @@ public void setDelay(int delay) {
473417 this .delay = delay ;
474418 }
475419
476- public JSlider getFPSslider () {
477- return FPSslider ;
478- }
479-
480- public void setFPSslider (JSlider fPSslider ) {
481- FPSslider = fPSslider ;
482- }
483-
484420 public long getTotalDelay () {
485421 return totalDelay ;
486422 }
0 commit comments