Skip to content

Commit d83aa08

Browse files
committed
Added Performance label
Added a JLabel between the buttonPanel and arrPanel that shows performance statistics and is updated every time that the frame is repainted.
1 parent c59ee74 commit d83aa08

File tree

3 files changed

+79
-36
lines changed

3 files changed

+79
-36
lines changed

src/com/example/algorithmvisualizer/AlgVisualizer.java

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
public 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
}

src/com/example/algorithmvisualizer/ArrDisplay.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ArrDisplay extends JComponent {
2222
private static final long serialVersionUID = 1L;
2323
private int swappedIndex1;
2424
private int swappedIndex2;
25-
private int numChunk; // amount of frames painted since the array was shuffled
25+
private int numChunks; // amount of frames painted since the array was shuffled
2626
private boolean isComplete; // if the array is sorted
2727
private ArrayList<Integer[]> swappedIndexes;
2828
private Integer[] arr;
@@ -61,8 +61,9 @@ public void paintComponent(Graphics g) {
6161
swappedIndex1 = -1;
6262
swappedIndex2 = -1;
6363
} else if (!algVisualizer.stopSort()) {
64-
swappedIndex1 = swappedIndexes.get(numChunk - 1)[0];
65-
swappedIndex2 = swappedIndexes.get(numChunk - 1)[1];
64+
// exclude the first chunk as its used to draw the first array, not related to the sorting
65+
swappedIndex1 = swappedIndexes.get(numChunks - 1)[0]; // index out of bounds exception?
66+
swappedIndex2 = swappedIndexes.get(numChunks - 1)[1];
6667
}
6768

6869
// Iterate through the array and drawn every index
@@ -89,6 +90,7 @@ public void paintComponent(Graphics g) {
8990
// Draw the current indexes bar
9091
graphics2d.fillRect(x, y, width, height);
9192
}
93+
algVisualizer.updatePerformance();
9294
}
9395

9496
public ArrayList<Integer[]> getSwappedIndexes() {
@@ -104,23 +106,19 @@ public void clearSwappedIndexes() {
104106
swappedIndexes = new ArrayList<Integer[]>();
105107
}
106108

107-
public void setNumChunk(int numChunk) {
108-
this.numChunk = numChunk;
109+
public void setNumChunk(int numChunks) {
110+
this.numChunks = numChunks;
109111
}
110112

111-
public int getNumChunk() {
112-
return numChunk;
113+
public int getNumChunks() {
114+
return numChunks;
113115
}
114116

115117
public boolean isComplete() {
116118
return isComplete;
117119
}
118120

119121
public void setComplete(boolean complete) {
120-
// Performance Button's availability is based on whether or not the array is
121-
// sorted.
122-
algVisualizer.getPerformanceButton().setEnabled(complete);
123-
124122
this.isComplete = complete;
125123
}
126124

src/com/example/algorithmvisualizer/ArrSorting.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
public class ArrSorting extends SwingWorker<Void, Integer[]> {
2020

21-
private Integer delay = 10;
2221
private int n;
2322
private Integer[] arr;
2423
private AlgVisualizer algVisualizer;
2524
private ArrDisplay arrDisplay;
26-
private int numChunk;
25+
private int numChunks;
2726

2827
public ArrSorting(AlgVisualizer algVisualizer, Integer[] arr, ArrDisplay arrDisplay) {
2928
this.algVisualizer = algVisualizer;
@@ -87,8 +86,8 @@ protected Void doInBackground() throws Exception {
8786
protected void process(List<Integer[]> chunks) {
8887
while (chunks.size() > 0) {
8988
// Paint each cloned array and update number of chunks
90-
numChunk++;
91-
arrDisplay.setNumChunk(this.numChunk);
89+
numChunks++;
90+
arrDisplay.setNumChunk(this.numChunks);
9291
arrDisplay.setArr(chunks.get(0));
9392
arrDisplay.repaint();
9493
chunks.remove(0);
@@ -101,7 +100,7 @@ protected void process(List<Integer[]> chunks) {
101100
*/
102101
private void sleep() {
103102
try {
104-
Thread.sleep(delay);
103+
Thread.sleep(algVisualizer.getDelay());
105104
} catch (InterruptedException e) {
106105
e.printStackTrace();
107106
}

0 commit comments

Comments
 (0)