Skip to content

Commit 1dda6e0

Browse files
authored
Merge pull request #31 from dlarocque/NewFeature
Now resizes application based on screen resolution
2 parents d736f6e + 36ac6de commit 1dda6e0

File tree

4 files changed

+285
-161
lines changed

4 files changed

+285
-161
lines changed

src/com/example/algorithmvisualizer/AlgVisualizer.java

Lines changed: 60 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
package com.example.algorithmvisualizer;
1515

16-
import java.awt.BorderLayout;
17-
import java.awt.Color;
18-
import java.awt.Dimension;
1916
import java.util.*;
2017
import javax.swing.*;
2118
import javax.swing.event.ChangeEvent;
@@ -25,12 +22,10 @@
2522

2623
public class AlgVisualizer implements ActionListener, ChangeListener {
2724

28-
private final int CONTENT_WIDTH = 900;
29-
private final int ARR_DISPLAY_HEIGHT = 900;
3025
private final int FPS_MIN = 2;
3126
private final int FPS_INIT = 10;
3227
private final int FPS_MAX = 100;
33-
private final String[] SIZE_OPTIONS = { "10", "50", "100", "300", "450", "900" }; // array size options
28+
private String[] sizeOptions = { "10", "50", "100", "300", "450", "900" }; // array size options
3429
private int n;
3530
private int numSwaps;
3631
private int delay;
@@ -46,125 +41,19 @@ public class AlgVisualizer implements ActionListener, ChangeListener {
4641
private boolean doQuickSort;
4742
private boolean stopSort; // True if sorting is stopped
4843
private Integer[] arr; // array that is going to be sorted
49-
private JFrame frame;
50-
private JPanel arrPanel;
51-
private ArrDisplay arrDisplay;
52-
private JPanel buttonPanel;
53-
private JButton resetButton;
54-
private JButton bubbleButton;
55-
private JButton insertionButton;
56-
private JButton selectionButton;
57-
private JButton mergeButton;
58-
private JButton quickButton;
59-
private JComboBox<String> sizeChanger;
60-
private JSlider FPSslider;
61-
private JLabel performanceLabel;
44+
private ContentWindow frame;
6245
private SwingWorker<Void, Integer[]> arrSort;
6346

64-
/*
65-
* In main(), we initialize an AlgVisualizer object, all instance variables, set
66-
* up the frame / window that the application will run inside, and make it
67-
* visible. By the end, a window containing what is meant to be shown on
68-
* application start up will open on the users screen, waiting for input.
69-
*/
7047
public static void main(String[] args) {
7148
AlgVisualizer algVisualizer = new AlgVisualizer();
72-
algVisualizer.initializeVars();
73-
algVisualizer.setFrame();
74-
}
75-
76-
/*
77-
* This method initializes all of this classes instance variables. The array is
78-
* initialized, filled, and shuffled. The arrDisplay object that paints the
79-
* array in bar graph form is initialized and passed the array. All buttons are
80-
* initialized and include an action listener.
81-
*
82-
*/
83-
public void initializeVars() {
84-
85-
n = Integer.parseInt(SIZE_OPTIONS[0]);
86-
arr = initArr();
87-
88-
indexComparisons = 0;
89-
setDelay(1000 / FPS_INIT);
90-
91-
// Initialize objects that will display and sort the array
92-
arrDisplay = new ArrDisplay(this);
93-
arrDisplay.setArr(arr);
94-
arrDisplay.setPreferredSize(new Dimension(CONTENT_WIDTH, ARR_DISPLAY_HEIGHT));
95-
96-
arrSort = new ArrSorting(this, this.arr, this.arrDisplay);
97-
98-
// Panels in the frame that will hold all components.
99-
// JPanels use the flowLayout to manage their components automatically.
100-
buttonPanel = new JPanel();
101-
buttonPanel.setBackground(Color.DARK_GRAY);
102-
103-
arrPanel = new JPanel();
104-
arrPanel.add(arrDisplay);
105-
106-
// Initialize all components and add action listeners
107-
resetButton = new JButton("Reset");
108-
resetButton.addActionListener(this);
109-
resetButton.setBackground(Color.WHITE);
110-
111-
bubbleButton = new JButton("Bubble Sort");
112-
bubbleButton.addActionListener(this);
113-
bubbleButton.setBackground(Color.WHITE);
114-
115-
selectionButton = new JButton("Selection Sort");
116-
selectionButton.addActionListener(this);
117-
selectionButton.setBackground(Color.WHITE);
118-
119-
insertionButton = new JButton("Insertion Sort");
120-
insertionButton.addActionListener(this);
121-
insertionButton.setBackground(Color.WHITE);
122-
123-
mergeButton = new JButton("Merge Sort");
124-
mergeButton.addActionListener(this);
125-
mergeButton.setBackground(Color.WHITE);
126-
127-
quickButton = new JButton("Quick Sort");
128-
quickButton.addActionListener(this);
129-
quickButton.setBackground(Color.WHITE);
130-
131-
sizeChanger = new JComboBox<String>(SIZE_OPTIONS); // Pass the String containing all of the size options
132-
sizeChanger.addActionListener(this);
133-
sizeChanger.setBackground(Color.WHITE);
134-
135-
FPSslider = new JSlider(JSlider.HORIZONTAL, FPS_MIN, FPS_MAX, FPS_INIT);
136-
FPSslider.addChangeListener(this);
137-
FPSslider.setBackground(Color.DARK_GRAY);
138-
// Initialize the performance label and center it
139-
performanceLabel = new JLabel();
140-
performanceLabel.setHorizontalAlignment(SwingConstants.CENTER);
141-
}
142-
143-
/*
144-
* setFrame() will add all the components that were initialized in the
145-
* initializeVars() method to JPanels, which will then also be added to the main
146-
* JFrame. The frame is initialized and made visible.
147-
*/
148-
public void setFrame() {
149-
// Add JButtons / components to button panel
150-
buttonPanel.add(resetButton);
151-
buttonPanel.add(bubbleButton);
152-
buttonPanel.add(selectionButton);
153-
buttonPanel.add(insertionButton);
154-
buttonPanel.add(mergeButton);
155-
buttonPanel.add(quickButton);
156-
buttonPanel.add(sizeChanger);
157-
buttonPanel.add(FPSslider);
158-
// Initialize and make the frame visible
159-
frame = new JFrame("Algorithm Visualizer");
160-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
161-
frame.setResizable(false); // Cannot be resizable, causes visual issues
162-
frame.add(buttonPanel, BorderLayout.PAGE_START); // Button panel added to the top of the frame
163-
frame.add(arrPanel, BorderLayout.PAGE_END); // Array display is added to the bottom of the frame
164-
frame.add(performanceLabel);
165-
frame.pack();
166-
frame.setLocationRelativeTo(null); // center of the screen
167-
frame.setVisible(true);
49+
algVisualizer.setN(Integer.parseInt(algVisualizer.getSizeOptions()[0]));
50+
algVisualizer.setArr(algVisualizer.initArr());
51+
algVisualizer.setFrame(new ContentWindow(algVisualizer));
52+
// Seems very messy
53+
algVisualizer.setIndexComparisons(0);
54+
algVisualizer.setDelay(1000 / algVisualizer.getInitFPS());
55+
algVisualizer.setSwingWorker(
56+
new ArrSorting(algVisualizer, algVisualizer.arr, algVisualizer.getFrame().getArrDisplay()));
16857
}
16958

17059
/*
@@ -185,27 +74,27 @@ public void actionPerformed(ActionEvent event) {
18574
doMergeSort = false;
18675
doQuickSort = false;
18776
// Find the source of the action
188-
if (event.getSource() == bubbleButton) {
77+
if (event.getSource() == frame.getBubbleButton()) {
18978
doBubbleSort = true;
19079
arrSort.execute();
191-
} else if (event.getSource() == selectionButton) {
80+
} else if (event.getSource() == frame.getSelectionButton()) {
19281
doSelectionSort = true;
19382
arrSort.execute();
194-
} else if (event.getSource() == insertionButton) {
83+
} else if (event.getSource() == frame.getInsertionButton()) {
19584
doInsertionSort = true;
19685
arrSort.execute();
197-
} else if (event.getSource() == mergeButton) {
86+
} else if (event.getSource() == frame.getMergeButton()) {
19887
doMergeSort = true;
19988
arrSort.execute();
200-
} else if (event.getSource() == quickButton) {
89+
} else if (event.getSource() == frame.getQuickButton()) {
20190
doQuickSort = true;
20291
arrSort.execute();
203-
} else if (event.getSource() == resetButton) {
92+
} else if (event.getSource() == frame.getResetButton()) {
20493
reset();
20594
arrSort.execute();
206-
} else if (event.getSource() == sizeChanger) {
95+
} else if (event.getSource() == frame.getSizeChanger()) {
20796
// Find what size was selected, and set n to that value
208-
String selectedSize = (String) sizeChanger.getSelectedItem();
97+
String selectedSize = (String) frame.getSizeChanger().getSelectedItem();
20998
n = Integer.valueOf(selectedSize);
21099
// reset and paint the new array
211100
reset();
@@ -233,16 +122,11 @@ public void stateChanged(ChangeEvent e) {
233122
public void reset() {
234123
setStopSort(true);
235124
arr = initArr();
236-
arrDisplay.clearSwappedIndexes();
237-
arrDisplay.setComplete(false);
125+
frame.getArrDisplay().clearSwappedIndexes();
126+
frame.getArrDisplay().setComplete(false);
238127
indexComparisons = 0;
239128
resetTime();
240-
resetSwingWorker(this, arr, arrDisplay);
241-
}
242-
243-
// Re-instantiates the SwingWorker so that execute() can be called again.
244-
public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displayArr) {
245-
arrSort = new ArrSorting(this, arr, displayArr);
129+
setSwingWorker(new ArrSorting(this, arr, frame.getArrDisplay()));
246130
}
247131

248132
// Reset the timer on the previous sort that was done, used in the reset()
@@ -252,7 +136,7 @@ public void resetTime() {
252136
sortingTime = 0;
253137
totalDelay = 0;
254138
}
255-
139+
256140
public Integer[] initArr() {
257141
Integer[] arr = new Integer[n];
258142
arr = fillArr(arr);
@@ -287,48 +171,53 @@ public Integer[] fillArr(Integer[] arr) {
287171
* adjust if it does not.
288172
*/
289173
public void updatePerformance() {
290-
numSwaps = arrDisplay.getSwappedIndexes().size();
291-
if (!getSort().equals("Not Sorting") && arrDisplay.getNumChunks() == 0) {
174+
numSwaps = frame.getArrDisplay().getSwappedIndexes().size();
175+
if (!getSort().equals("Not Sorting") && frame.getArrDisplay().getNumChunks() == 0) {
292176
visualizationTime = System.currentTimeMillis() - startTime;
293177
sortingTime = visualizationTime - totalDelay;
294-
} else if (arrDisplay.getNumChunks() > 1 && !arrDisplay.isComplete()) {
178+
} else if (frame.getArrDisplay().getNumChunks() > 1 && !frame.getArrDisplay().isComplete()) {
295179
visualizationTime = System.currentTimeMillis() - startTime;
296180
sortingTime = visualizationTime - totalDelay;
297181
}
298-
if(stopSort) {
182+
if (stopSort) {
299183
resetTime();
300184
}
301185
String performance = String.format(
302186
"Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms",
303187
indexComparisons, numSwaps, visualizationTime, sortingTime);
304-
performanceLabel.setText(performance);
188+
frame.getPerformanceLabel().setText(performance);
305189
frame.pack();
306190
}
307191

308192
public Integer[] getArr() {
309193
return arr;
310194
}
311195

312-
public int getArrDispHeight() {
313-
return ARR_DISPLAY_HEIGHT;
196+
public void setArr(Integer[] arr) {
197+
this.arr = arr;
314198
}
315199

316-
public int getWidth() {
317-
return CONTENT_WIDTH;
200+
public void setN(int n) {
201+
this.n = n;
318202
}
319203

320-
public JFrame getJFrame() {
204+
public ContentWindow getFrame() {
321205
return frame;
322206
}
323207

324-
public ArrDisplay getDisplayArr() {
325-
return arrDisplay;
208+
public void setFrame(ContentWindow frame) {
209+
this.frame = frame;
326210
}
327211

328212
public SwingWorker<Void, Integer[]> getArrSorting() {
329213
return arrSort;
330214
}
331215

216+
// Re-instantiates the SwingWorker so that execute() can be called again.
217+
public void setSwingWorker(SwingWorker<Void, Integer[]> arrSort) {
218+
this.arrSort = arrSort;
219+
}
220+
332221
public void setSort(String sort) {
333222
if (sort.equals("Bubble Sort")) {
334223
doBubbleSort = true;
@@ -377,11 +266,7 @@ public boolean stopSort() {
377266
* disable them.
378267
*/
379268
public void setStopSort(boolean toSet) {
380-
bubbleButton.setEnabled(toSet);
381-
selectionButton.setEnabled(toSet);
382-
insertionButton.setEnabled(toSet);
383-
mergeButton.setEnabled(toSet);
384-
quickButton.setEnabled(toSet);
269+
frame.setSortButtons(toSet);
385270
stopSort = toSet;
386271
}
387272

@@ -420,4 +305,24 @@ public long getTotalDelay() {
420305
public void setTotalDelay(long totalDelay) {
421306
this.totalDelay = totalDelay;
422307
}
308+
309+
public String[] getSizeOptions() {
310+
return sizeOptions;
311+
}
312+
313+
public void setSizeOptions(String[] sizeOptions) {
314+
this.sizeOptions = sizeOptions;
315+
}
316+
317+
public int getMaxFPS() {
318+
return FPS_MAX;
319+
}
320+
321+
public int getInitFPS() {
322+
return FPS_INIT;
323+
}
324+
325+
public int getMinFPS() {
326+
return FPS_MIN;
327+
}
423328
}

src/com/example/algorithmvisualizer/ArrDisplay.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ public class ArrDisplay extends JComponent {
2727
private ArrayList<Integer[]> swappedIndexes;
2828
private Integer[] arr;
2929
private AlgVisualizer algVisualizer;
30+
private ContentWindow frame;
3031

31-
public ArrDisplay(AlgVisualizer algVisualizer) {
32+
public ArrDisplay(AlgVisualizer algVisualizer, ContentWindow frame) {
3233
this.algVisualizer = algVisualizer;
34+
this.frame = frame;
3335
swappedIndexes = new ArrayList<Integer[]>();
3436
}
3537

@@ -52,7 +54,7 @@ public ArrDisplay(AlgVisualizer algVisualizer) {
5254
public void paintComponent(Graphics g) {
5355
Graphics2D graphics2d = (Graphics2D) g;
5456
graphics2d.setColor(Color.DARK_GRAY);
55-
graphics2d.fillRect(0, 0, algVisualizer.getWidth(), algVisualizer.getArrDispHeight());
57+
graphics2d.fillRect(0, 0, frame.getArrDisplayWidth(), frame.getArrDisplayHeight());
5658
if (algVisualizer.getSort().equals("Not Sorting") || isComplete) {
5759
swappedIndex1 = -1;
5860
swappedIndex2 = -1;
@@ -62,10 +64,10 @@ public void paintComponent(Graphics g) {
6264
}
6365
// Iterate through the array and draw every index
6466
for (int i = 0; i < arr.length; i++) {
65-
int width = (int) (algVisualizer.getWidth() / (double) arr.length);
66-
int height = arr[i] * (algVisualizer.getArrDispHeight() / arr.length);
67+
int width = (int) (frame.getArrDisplayWidth() / (double) arr.length);
68+
int height = arr[i] * (frame.getArrDisplayHeight() / arr.length);
6769
int x = i * width;
68-
int y = algVisualizer.getArrDispHeight() - height;
70+
int y = frame.getArrDisplayHeight() - height;
6971
if (i == swappedIndex1 && !algVisualizer.stopSort()) {
7072
graphics2d.setColor(Color.RED);
7173
} else if (i == swappedIndex2 && !algVisualizer.stopSort()) {

src/com/example/algorithmvisualizer/ArrSorting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected Void doInBackground() throws Exception {
4646
if (algVisualizer.stopSort()) {
4747
publish(arr.clone());
4848
sleep();
49-
algVisualizer.resetSwingWorker(algVisualizer, arr, arrDisplay);
49+
algVisualizer.setSwingWorker(new ArrSorting(algVisualizer, arr, arrDisplay));
5050
} else {
5151
algVisualizer.setStartTime(System.currentTimeMillis());
5252
if (algVisualizer.getSort().equals("Bubble Sort")) {

0 commit comments

Comments
 (0)