Skip to content

Commit f821140

Browse files
committed
Moved Frame setup to ContentWindow.java
Added ContentWindow.java
1 parent 6eda47c commit f821140

File tree

4 files changed

+245
-162
lines changed

4 files changed

+245
-162
lines changed

src/com/example/algorithmvisualizer/AlgVisualizer.java

Lines changed: 60 additions & 156 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();
@@ -232,20 +121,14 @@ public void stateChanged(ChangeEvent e) {
232121
* SwingWorker, we simply re-instantiate it so that we are able to call it
233122
* again.
234123
*/
235-
236124
public void reset() {
237125
setStopSort(true);
238126
arr = initArr();
239-
arrDisplay.clearSwappedIndexes();
240-
arrDisplay.setComplete(false);
127+
frame.getArrDisplay().clearSwappedIndexes();
128+
frame.getArrDisplay().setComplete(false);
241129
indexComparisons = 0;
242130
resetTime();
243-
resetSwingWorker(this, arr, arrDisplay);
244-
}
245-
246-
// Re-instantiates the SwingWorker so that execute() can be called again.
247-
public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displayArr) {
248-
arrSort = new ArrSorting(this, arr, displayArr);
131+
setSwingWorker(new ArrSorting(this, arr, frame.getArrDisplay()));
249132
}
250133

251134
// Reset the timer on the previous sort that was done, used in the reset()
@@ -256,7 +139,7 @@ public void resetTime() {
256139
sortingTime = 0;
257140
totalDelay = 0;
258141
}
259-
142+
260143
public Integer[] initArr() {
261144
Integer[] arr = new Integer[n];
262145
arr = fillArr(arr);
@@ -291,48 +174,53 @@ public Integer[] fillArr(Integer[] arr) {
291174
* adjust if it does not.
292175
*/
293176
public void updatePerformance() {
294-
numSwaps = arrDisplay.getSwappedIndexes().size();
295-
if (!getSort().equals("Not Sorting") && arrDisplay.getNumChunks() == 0) {
177+
numSwaps = frame.getArrDisplay().getSwappedIndexes().size();
178+
if (!getSort().equals("Not Sorting") && frame.getArrDisplay().getNumChunks() == 0) {
296179
visualizationTime = System.currentTimeMillis() - startTime;
297180
sortingTime = visualizationTime - totalDelay;
298-
} else if (arrDisplay.getNumChunks() > 1 && !arrDisplay.isComplete()) {
181+
} else if (frame.getArrDisplay().getNumChunks() > 1 && !frame.getArrDisplay().isComplete()) {
299182
visualizationTime = System.currentTimeMillis() - startTime;
300183
sortingTime = visualizationTime - totalDelay;
301184
}
302-
if(stopSort) {
185+
if (stopSort) {
303186
resetTime();
304187
}
305188
String performance = String.format(
306189
"Index Comparisons : %d Index Swaps : %d Visualization Time : %dms Sorting Time : %dms",
307190
indexComparisons, numSwaps, visualizationTime, sortingTime);
308-
performanceLabel.setText(performance);
191+
frame.getPerformanceLabel().setText(performance);
309192
frame.pack();
310193
}
311194

312195
public Integer[] getArr() {
313196
return arr;
314197
}
315198

316-
public int getArrDispHeight() {
317-
return ARR_DISPLAY_HEIGHT;
199+
public void setArr(Integer[] arr) {
200+
this.arr = arr;
318201
}
319202

320-
public int getWidth() {
321-
return CONTENT_WIDTH;
203+
public void setN(int n) {
204+
this.n = n;
322205
}
323206

324-
public JFrame getJFrame() {
207+
public ContentWindow getFrame() {
325208
return frame;
326209
}
327210

328-
public ArrDisplay getDisplayArr() {
329-
return arrDisplay;
211+
public void setFrame(ContentWindow frame) {
212+
this.frame = frame;
330213
}
331214

332215
public SwingWorker<Void, Integer[]> getArrSorting() {
333216
return arrSort;
334217
}
335218

219+
// Re-instantiates the SwingWorker so that execute() can be called again.
220+
public void setSwingWorker(SwingWorker<Void, Integer[]> arrSort) {
221+
this.arrSort = arrSort;
222+
}
223+
336224
public void setSort(String sort) {
337225
if (sort.equals("Bubble Sort")) {
338226
doBubbleSort = true;
@@ -381,11 +269,7 @@ public boolean stopSort() {
381269
* disable them.
382270
*/
383271
public void setStopSort(boolean toSet) {
384-
bubbleButton.setEnabled(toSet);
385-
selectionButton.setEnabled(toSet);
386-
insertionButton.setEnabled(toSet);
387-
mergeButton.setEnabled(toSet);
388-
quickButton.setEnabled(toSet);
272+
frame.setSortButtons(toSet);
389273
stopSort = toSet;
390274
}
391275

@@ -424,4 +308,24 @@ public long getTotalDelay() {
424308
public void setTotalDelay(long totalDelay) {
425309
this.totalDelay = totalDelay;
426310
}
311+
312+
public String[] getSizeOptions() {
313+
return sizeOptions;
314+
}
315+
316+
public void setSizeOptions(String[] sizeOptions) {
317+
this.sizeOptions = sizeOptions;
318+
}
319+
320+
public int getMaxFPS() {
321+
return FPS_MAX;
322+
}
323+
324+
public int getInitFPS() {
325+
return FPS_INIT;
326+
}
327+
328+
public int getMinFPS() {
329+
return FPS_MIN;
330+
}
427331
}

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.getWidth(), frame.getArrDisplayHeight());
5658
if (algVisualizer.getSort().equals("Not Sorting") || isComplete) {
5759
swappedIndex1 = -1;
5860
swappedIndex2 = -1;
@@ -63,10 +65,10 @@ public void paintComponent(Graphics g) {
6365
}
6466
// Iterate through the array and drawn every index
6567
for (int i = 0; i < arr.length; i++) {
66-
int width = (int) (algVisualizer.getWidth() / (double) arr.length);
67-
int height = arr[i] * (algVisualizer.getArrDispHeight() / arr.length);
68+
int width = (int) (frame.getWidth() / (double) arr.length);
69+
int height = arr[i] * (frame.getArrDisplayHeight() / arr.length);
6870
int x = i * width;
69-
int y = algVisualizer.getArrDispHeight() - height;
71+
int y = frame.getArrDisplayHeight() - height;
7072
if (i == swappedIndex1 && !algVisualizer.stopSort()) {
7173
graphics2d.setColor(Color.RED);
7274
} 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)