Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 16 additions & 80 deletions src/com/example/algorithmvisualizer/AlgVisualizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
public class AlgVisualizer implements ActionListener, ChangeListener {

private final int CONTENT_WIDTH = 900;
private final int CONTENT_HEIGHT = 960;
private final int ARR_DISPLAY_HEIGHT = 900;
private final int FPS_MIN = 1;
private final int FPS_MIN = 2;
private final int FPS_INIT = 10;
private final int FPS_MAX = 100;
private final String[] SIZE_OPTIONS = { "10", "50", "100", "300", "450", "900" }; // array size options
Expand All @@ -38,7 +37,6 @@ public class AlgVisualizer implements ActionListener, ChangeListener {
private long totalDelay;
private Integer indexComparisons;
private long startTime; // start time of a sort
private long endTime; // end time of a sort
private long visualizationTime;
private long sortingTime;
private boolean doBubbleSort;
Expand Down Expand Up @@ -74,11 +72,7 @@ public static void main(String[] args) {
algVisualizer.initializeVars();
algVisualizer.setFrame();
}

public AlgVisualizer() {
// empty constructor, variables are initialized in main method.
}


/*
* This method initializes all of this classes instance variables. The array is
* initialized, filled, and shuffled. The arrDisplay object that paints the
Expand All @@ -88,21 +82,13 @@ public AlgVisualizer() {
*/
public void initializeVars() {

setN(10);

arr = new Integer[n];
arr = fillArr(arr);
arr = shuffleArr(arr);
n = Integer.parseInt(SIZE_OPTIONS[0]);
arr = initArr();

indexComparisons = 0;
startTime = 0;
endTime = 0;
visualizationTime = 0;
sortingTime = 0;
setDelay(1000 / FPS_INIT);

// Initialize objects that will display and sort the array

arrDisplay = new ArrDisplay(this);
arrDisplay.setArr(arr);
arrDisplay.setPreferredSize(new Dimension(CONTENT_WIDTH, ARR_DISPLAY_HEIGHT));
Expand All @@ -111,15 +97,13 @@ public void initializeVars() {

// Panels in the frame that will hold all components.
// JPanels use the flowLayout to manage their components automatically.

buttonPanel = new JPanel();
buttonPanel.setBackground(Color.DARK_GRAY);

arrPanel = new JPanel();
arrPanel.add(arrDisplay);

// Initialize all components and add action listeners

resetButton = new JButton("Reset");
resetButton.addActionListener(this);
resetButton.setBackground(Color.WHITE);
Expand Down Expand Up @@ -152,7 +136,6 @@ public void initializeVars() {
FPSslider.addChangeListener(this);
FPSslider.setBackground(Color.DARK_GRAY);
// Initialize the performance label and center it

performanceLabel = new JLabel();
performanceLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
Expand All @@ -163,7 +146,6 @@ public void initializeVars() {
* JFrame. The frame is initialized and made visible.
*/
public void setFrame() {

// Add JButtons / components to button panel
buttonPanel.add(resetButton);
buttonPanel.add(bubbleButton);
Expand All @@ -173,7 +155,6 @@ public void setFrame() {
buttonPanel.add(quickButton);
buttonPanel.add(sizeChanger);
buttonPanel.add(FPSslider);

// Initialize and make the frame visible
frame = new JFrame("Algorithm Visualizer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand All @@ -184,7 +165,6 @@ public void setFrame() {
frame.pack();
frame.setLocationRelativeTo(null); // center of the screen
frame.setVisible(true);

}

/*
Expand All @@ -204,7 +184,6 @@ public void actionPerformed(ActionEvent event) {
doInsertionSort = false;
doMergeSort = false;
doQuickSort = false;

// Find the source of the action
if (event.getSource() == bubbleButton) {
doBubbleSort = true;
Expand All @@ -227,12 +206,7 @@ public void actionPerformed(ActionEvent event) {
} else if (event.getSource() == sizeChanger) {
// Find what size was selected, and set n to that value
String selectedSize = (String) sizeChanger.getSelectedItem();
setN(Integer.valueOf(selectedSize));

// Create the new array of length n, and it will be shuffled in reset()
arr = new Integer[n];
arr = fillArr(arr);

n = Integer.valueOf(selectedSize);
// reset and paint the new array
reset();
arrSort.execute();
Expand All @@ -258,12 +232,12 @@ public void stateChanged(ChangeEvent e) {
* SwingWorker, we simply re-instantiate it so that we are able to call it
* again.
*/

public void reset() {
setStopSort(true);
arr = shuffleArr(arr);
arr = initArr();
arrDisplay.clearSwappedIndexes();
arrDisplay.setComplete(false);
arrDisplay.setArr(arr);
indexComparisons = 0;
resetTime();
resetSwingWorker(this, arr, arrDisplay);
Expand All @@ -278,11 +252,17 @@ public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displa
// method
public void resetTime() {
startTime = 0;
endTime = 0;
visualizationTime = 0;
sortingTime = 0;
totalDelay = 0;
}

public Integer[] initArr() {
Integer[] arr = new Integer[n];
arr = fillArr(arr);
arr = shuffleArr(arr);
return arr;
}

public Integer[] shuffleArr(Integer[] arr) {
arr = fillArr(arr);
Expand Down Expand Up @@ -312,24 +292,20 @@ public Integer[] fillArr(Integer[] arr) {
*/
public void updatePerformance() {
numSwaps = arrDisplay.getSwappedIndexes().size();
System.out.println("total delay " + totalDelay);
if (!getSort().equals("Not Sorting") && arrDisplay.getNumChunks() == 1) {
if (!getSort().equals("Not Sorting") && arrDisplay.getNumChunks() == 0) {
visualizationTime = System.currentTimeMillis() - startTime;
sortingTime = visualizationTime - totalDelay;
} else if (arrDisplay.getNumChunks() > 1) {
} else if (arrDisplay.getNumChunks() > 1 && !arrDisplay.isComplete()) {
visualizationTime = System.currentTimeMillis() - startTime;
sortingTime = visualizationTime - totalDelay;
}
if(stopSort) {
resetTime();
}

String performance = String.format(
"Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms",
indexComparisons, numSwaps, visualizationTime, sortingTime);

performanceLabel.setText(performance);

frame.pack();
}

Expand All @@ -341,10 +317,6 @@ public int getArrDispHeight() {
return ARR_DISPLAY_HEIGHT;
}

public int getHeight() {
return CONTENT_HEIGHT;
}

public int getWidth() {
return CONTENT_WIDTH;
}
Expand Down Expand Up @@ -417,14 +389,6 @@ public void setStopSort(boolean toSet) {
stopSort = toSet;
}

public int getN() {
return n;
}

public void setN(int n) {
this.n = n;
}

public Integer getIndexComparisons() {
return indexComparisons;
}
Expand All @@ -433,30 +397,10 @@ public void setIndexComparisons(int indexComparisons) {
this.indexComparisons = indexComparisons;
}

public long getStartTime() {
return startTime;
}

public void setStartTime(long startTime) {
this.startTime = startTime;
}

public long getEndTime() {
return endTime;
}

public void setEndTime(long endTime) {
this.endTime = endTime;
}

public JLabel getPerformanceLabel() {
return performanceLabel;
}

public void setPerformanceLabel(JLabel performanceLabel) {
this.performanceLabel = performanceLabel;
}

public int getNumSwaps() {
return numSwaps;
}
Expand All @@ -473,14 +417,6 @@ public void setDelay(int delay) {
this.delay = delay;
}

public JSlider getFPSslider() {
return FPSslider;
}

public void setFPSslider(JSlider fPSslider) {
FPSslider = fPSslider;
}

public long getTotalDelay() {
return totalDelay;
}
Expand Down
15 changes: 2 additions & 13 deletions src/com/example/algorithmvisualizer/ArrDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,23 @@ public ArrDisplay(AlgVisualizer algVisualizer) {
*/
@Override
public void paintComponent(Graphics g) {
// Takes ~ 40ms to draw ( depending on the system )
Graphics2D graphics2d = (Graphics2D) g;
// Draw the DARK_GREY background
graphics2d.setColor(Color.DARK_GRAY);
graphics2d.fillRect(0, 0, algVisualizer.getWidth(), algVisualizer.getArrDispHeight());

// Find the values of the swappedIndexes and update framesPainted
if (algVisualizer.getSort().equals("Not Sorting") || isComplete) {
swappedIndex1 = -1;
swappedIndex2 = -1;
} else if (!algVisualizer.stopSort()) {
// exclude the first chunk as its used to draw the first array, not related to the sorting
swappedIndex1 = swappedIndexes.get(numChunks - 1)[0]; // index out of bounds exception?
swappedIndex2 = swappedIndexes.get(numChunks - 1)[1];
swappedIndex1 = swappedIndexes.get(numChunks)[0]; // index out of bounds exception?
swappedIndex2 = swappedIndexes.get(numChunks)[1];
}

// Iterate through the array and drawn every index
for (int i = 0; i < arr.length; i++) {

// Dimensions of the bars are calculated to make sure they take up the entire
// Panel
int width = (int) (algVisualizer.getWidth() / (double) arr.length);
int height = arr[i] * (algVisualizer.getArrDispHeight() / arr.length);
int x = i * width;
int y = algVisualizer.getArrDispHeight() - height;

// Determine the colour of the bar we are currently drawing
if (i == swappedIndex1 && !algVisualizer.stopSort()) {
graphics2d.setColor(Color.RED);
} else if (i == swappedIndex2 && !algVisualizer.stopSort()) {
Expand All @@ -86,7 +76,6 @@ public void paintComponent(Graphics g) {
} else {
graphics2d.setColor(Color.WHITE);
}

// Draw the current indexes bar
graphics2d.fillRect(x, y, width, height);
}
Expand Down
Loading