Skip to content

Commit 1d33d44

Browse files
committed
Added Working FPS bar (Close #14)
1 parent bbd0780 commit 1d33d44

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

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

+31-7
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@
1818
import java.awt.Dimension;
1919
import java.util.*;
2020
import javax.swing.*;
21+
import javax.swing.event.ChangeEvent;
22+
import javax.swing.event.ChangeListener;
23+
2124
import java.awt.event.*;
2225

23-
public class AlgVisualizer implements ActionListener {
26+
public class AlgVisualizer implements ActionListener, ChangeListener {
2427

2528
private final int CONTENT_WIDTH = 900;
2629
private final int CONTENT_HEIGHT = 960;
2730
private final int ARR_DISPLAY_HEIGHT = 900;
31+
private final int FPS_MIN = 1;
32+
private final int FPS_INIT = 10;
33+
private final int FPS_MAX = 100;
2834
private final String[] SIZE_OPTIONS = { "10", "50", "100", "300", "450", "900" }; // array size options
2935
private int n;
3036
private int numSwaps;
3137
private int delay;
3238
private Integer indexComparisons;
3339
private long startTime; // start time of a sort
3440
private long endTime; // end time of a sort
41+
private long visualizationTime;
42+
private long sortingTime;
3543
private boolean doBubbleSort;
3644
private boolean doInsertionSort;
3745
private boolean doSelectionSort;
@@ -88,7 +96,9 @@ public void initializeVars() {
8896
indexComparisons = 0;
8997
startTime = 0;
9098
endTime = 0;
91-
setDelay(2);
99+
visualizationTime = 0;
100+
sortingTime = 0;
101+
setDelay(1000 / FPS_INIT);
92102

93103
// Initialize objects that will display and sort the array
94104

@@ -136,9 +146,10 @@ public void initializeVars() {
136146
sizeChanger = new JComboBox<String>(SIZE_OPTIONS); // Pass the String containing all of the size options
137147
sizeChanger.addActionListener(this);
138148
sizeChanger.setBackground(Color.WHITE);
139-
140-
FPSslider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
141149

150+
FPSslider = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT);
151+
FPSslider.addChangeListener(this);
152+
FPSslider.setBackground(Color.DARK_GRAY);
142153
// Initialize the performance label and center it
143154

144155
performanceLabel = new JLabel();
@@ -227,6 +238,16 @@ public void actionPerformed(ActionEvent event) {
227238
}
228239
}
229240

241+
@Override
242+
public void stateChanged(ChangeEvent e) {
243+
JSlider source = (JSlider) e.getSource();
244+
if (!source.getValueIsAdjusting()) {
245+
int fps = (int) source.getValue();
246+
delay = 1000 / fps; // ms
247+
setDelay(delay);
248+
}
249+
}
250+
230251
/*
231252
* Reset method is called whenever the user presses the reset button, or when a
232253
* new size of array is chosen from the size changer. This method stops sorting,
@@ -257,6 +278,8 @@ public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displa
257278
public void resetTime() {
258279
startTime = 0;
259280
endTime = 0;
281+
visualizationTime = 0;
282+
sortingTime = 0;
260283
}
261284

262285
public Integer[] shuffleArr(Integer[] arr) {
@@ -288,11 +311,12 @@ public Integer[] fillArr(Integer[] arr) {
288311
public void updatePerformance() {
289312
numSwaps = arrDisplay.getSwappedIndexes().size();
290313

291-
long visualizationTime = 0;
292-
long sortingTime = 0;
293-
if (!getSort().equals("Not Sorting")) {
314+
if (!getSort().equals("Not Sorting") && arrDisplay.getNumChunks() == 1) {
294315
visualizationTime = System.currentTimeMillis() - startTime;
295316
sortingTime = visualizationTime - (delay * (arrDisplay.getNumChunks() - 1));
317+
} else if (arrDisplay.getNumChunks() > 1) {
318+
visualizationTime = System.currentTimeMillis() - startTime;
319+
sortingTime = sortingTime + (System.currentTimeMillis() - sortingTime);
296320
}
297321

298322
String performance = String.format(

0 commit comments

Comments
 (0)