2222
2323public class AlgVisualizer implements ActionListener {
2424
25- private int n ;
2625 private final int CONTENT_WIDTH = 800 ;
2726 private final int CONTENT_HEIGHT = 860 ;
2827 private final int ARR_DISPLAY_HEIGHT = 800 ;
2928 private final String [] SIZE_OPTIONS = { "10" , "50" , "100" , "200" , "400" , "800" }; // array size options
29+ private int n ;
30+ private int numSwaps ;
31+ private int delay ;
3032 private Integer indexComparisons ;
3133 private long startTime ; // start time of a sort
3234 private long endTime ; // end time of a sort
@@ -47,8 +49,8 @@ public class AlgVisualizer implements ActionListener {
4749 private JButton selectionButton ;
4850 private JButton mergeButton ;
4951 private JButton quickButton ;
50- private JButton performanceButton ;
5152 private JComboBox <String > sizeChanger ;
53+ private JLabel performanceLabel ;
5254 private SwingWorker <Void , Integer []> arrSort ;
5355
5456 /*
@@ -83,6 +85,9 @@ public void initializeVars() {
8385 arr = shuffleArr (arr );
8486
8587 indexComparisons = 0 ;
88+ startTime = 0 ;
89+ endTime = 0 ;
90+ setDelay (2 );
8691
8792 // Initialize objects that will display and sort the array
8893
@@ -131,10 +136,10 @@ public void initializeVars() {
131136 sizeChanger .addActionListener (this );
132137 sizeChanger .setBackground (Color .WHITE );
133138
134- performanceButton = new JButton ( "Performance" );
135- performanceButton . addActionListener ( this );
136- performanceButton . setBackground ( Color . WHITE );
137- performanceButton . setEnabled ( false ); // This button is not available until a sort is complete
139+ // Initialize the performance label and center it
140+
141+ performanceLabel = new JLabel ( );
142+ performanceLabel . setHorizontalAlignment ( SwingConstants . CENTER );
138143 }
139144
140145 /*
@@ -152,14 +157,14 @@ public void setFrame() {
152157 buttonPanel .add (mergeButton );
153158 buttonPanel .add (quickButton );
154159 buttonPanel .add (sizeChanger );
155- buttonPanel .add (performanceButton );
156160
157161 // Initialize and make the frame visible
158162 frame = new JFrame ("Algorithm Visualizer" );
159163 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
160164 frame .setResizable (false ); // Cannot be resizable, causes visual issues
161165 frame .add (buttonPanel , BorderLayout .PAGE_START ); // Button panel added to the top of the frame
162166 frame .add (arrPanel , BorderLayout .PAGE_END ); // Array display is added to the bottom of the frame
167+ frame .add (performanceLabel );
163168 frame .pack ();
164169 frame .setLocationRelativeTo (null ); // center of the screen
165170 frame .setVisible (true );
@@ -215,14 +220,6 @@ public void actionPerformed(ActionEvent event) {
215220 // reset and paint the new array
216221 reset ();
217222 arrSort .execute ();
218- } else if (event .getSource () == performanceButton ) {
219- int numSwaps = arrDisplay .getSwappedIndexes ().size ();
220- long visualizationTime = endTime - startTime ; // net time
221- long sortingTime = visualizationTime - (60 * numSwaps + 1 ); // - NEED TO FIX
222- String statsMessage = String .format (
223- "Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms" ,
224- indexComparisons , numSwaps , visualizationTime , sortingTime );
225- JOptionPane .showMessageDialog (frame , statsMessage , "Performance" , JOptionPane .PLAIN_MESSAGE );
226223 }
227224 }
228225
@@ -239,7 +236,6 @@ public void reset() {
239236 setStopSort (true );
240237 arr = shuffleArr (arr );
241238 arrDisplay .clearSwappedIndexes ();
242- arrDisplay .setNumChunk (0 );
243239 arrDisplay .setComplete (false );
244240 arrDisplay .setArr (arr );
245241 indexComparisons = 0 ;
@@ -274,6 +270,36 @@ public Integer[] fillArr(Integer[] arr) {
274270 return arr ;
275271 }
276272
273+ /*
274+ * updatePerformance will be called every time the array is repainted. This
275+ * makes it slower / not real time when there is a high delay.
276+ *
277+ * We get the values for each performance statistic we want to track (number of
278+ * swaps, number of comparisons, visualization time, sorting time), format them
279+ * into a string that will then be assigned to the JLabel's text.
280+ *
281+ * frame.pack() makes sure that our new text will fit in the frame, and will
282+ * adjust if it does not.
283+ */
284+ public void updatePerformance () {
285+ numSwaps = arrDisplay .getSwappedIndexes ().size ();
286+
287+ long visualizationTime = 0 ;
288+ long sortingTime = 0 ;
289+ if (!getSort ().equals ("Not Sorting" )) {
290+ visualizationTime = System .currentTimeMillis () - startTime ;
291+ sortingTime = visualizationTime - (delay * (arrDisplay .getNumChunks () - 1 ));
292+ }
293+
294+ String performance = String .format (
295+ "Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms" ,
296+ indexComparisons , numSwaps , visualizationTime , sortingTime );
297+
298+ performanceLabel .setText (performance );
299+
300+ frame .pack ();
301+ }
302+
277303 public Integer [] getArr () {
278304 return arr ;
279305 }
@@ -366,10 +392,6 @@ public void setN(int n) {
366392 this .n = n ;
367393 }
368394
369- public JButton getPerformanceButton () {
370- return performanceButton ;
371- }
372-
373395 public Integer getIndexComparisons () {
374396 return indexComparisons ;
375397 }
@@ -393,4 +415,28 @@ public long getEndTime() {
393415 public void setEndTime (long endTime ) {
394416 this .endTime = endTime ;
395417 }
418+
419+ public JLabel getPerformanceLabel () {
420+ return performanceLabel ;
421+ }
422+
423+ public void setPerformanceLabel (JLabel performanceLabel ) {
424+ this .performanceLabel = performanceLabel ;
425+ }
426+
427+ public int getNumSwaps () {
428+ return numSwaps ;
429+ }
430+
431+ public void setNumSwaps (int numSwaps ) {
432+ this .numSwaps = numSwaps ;
433+ }
434+
435+ public int getDelay () {
436+ return delay ;
437+ }
438+
439+ public void setDelay (int delay ) {
440+ this .delay = delay ;
441+ }
396442}
0 commit comments