Skip to content

Commit 261a1d1

Browse files
committed
Fix #11 Naming conventions
1 parent 52a6857 commit 261a1d1

File tree

3 files changed

+126
-124
lines changed

3 files changed

+126
-124
lines changed

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

+71-70
Original file line numberDiff line numberDiff line change
@@ -9,67 +9,45 @@
99

1010
public class AlgVisualizer implements ActionListener {
1111

12-
private int n = 10;
13-
final int CONTENT_WIDTH = 800;
14-
final int CONTENT_HEIGHT = 860;
15-
final int ARR_DISPLAY_HEIGHT = 800;
12+
private int n;
13+
private final int CONTENT_WIDTH = 800;
14+
private final int CONTENT_HEIGHT = 860;
15+
private final int ARR_DISPLAY_HEIGHT = 800;
1616
private Integer[] arr;
1717
private JFrame frame = new JFrame("Algorithm Visualizer");
1818
private JPanel arrPanel;
19-
private DisplayArr displayArr;
19+
private ArrDisplay arrDisplay;
2020
private JPanel buttonPanel;
2121
private JButton bubbleButton;
2222
private JButton insertionButton;
2323
private JButton selectionButton;
2424
private JButton resetButton;
2525
private JComboBox<String> sizeChanger;
26-
private String[] sizeOptions = {"10", "50", "100", "200", "400", "800"};
27-
private SwingWorker<Void, Integer[]> sort;
26+
final String[] SIZE_OPTIONS = { "10", "50", "100", "200", "400", "800" };
27+
private SwingWorker<Void, Integer[]> arrSort;
2828
private boolean doBubbleSort;
2929
private boolean doInsertionSort;
3030
private boolean doSelectionSort;
3131
private boolean stopSort;
3232

3333
public static void main(String[] args) {
34-
AlgVisualizer alg = new AlgVisualizer();
35-
alg.initializeVars();
36-
alg.setFrame();
34+
AlgVisualizer algVisualizer = new AlgVisualizer();
35+
algVisualizer.initializeVars();
36+
algVisualizer.setFrame();
3737
}
38-
39-
public void setFrame() {
40-
frame.setVisible(true);
41-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42-
frame.setResizable(false);
43-
44-
buttonPanel = new JPanel();
45-
buttonPanel.setBackground(Color.DARK_GRAY);
46-
buttonPanel.add(resetButton);
47-
buttonPanel.add(bubbleButton);
48-
buttonPanel.add(selectionButton);
49-
buttonPanel.add(insertionButton);
50-
buttonPanel.add(sizeChanger);
51-
buttonPanel.setVisible(true);
52-
53-
arrPanel = new JPanel();
54-
arrPanel.add(displayArr);
55-
arrPanel.setVisible(true);
56-
57-
frame.add(buttonPanel, BorderLayout.PAGE_START);
58-
frame.add(arrPanel, BorderLayout.PAGE_END);
59-
frame.pack();
60-
frame.setLocationRelativeTo(null);
61-
}
62-
38+
6339
public void initializeVars() {
6440

41+
setN(10);
42+
6543
arr = new Integer[n];
6644
arr = fillArr(arr);
6745
arr = shuffleArr(arr);
6846

69-
displayArr = new DisplayArr(this, arr);
70-
displayArr.setPreferredSize(new Dimension(CONTENT_WIDTH, ARR_DISPLAY_HEIGHT));
47+
arrDisplay = new ArrDisplay(this, arr);
48+
arrDisplay.setPreferredSize(new Dimension(CONTENT_WIDTH, ARR_DISPLAY_HEIGHT));
7149

72-
sort = new Sorting(this, arr, displayArr);
50+
arrSort = new ArrSorting(this, arr, arrDisplay);
7351

7452
resetButton = new JButton("Reset");
7553
resetButton.addActionListener(this);
@@ -86,53 +64,76 @@ public void initializeVars() {
8664
insertionButton = new JButton("Insertion Sort");
8765
insertionButton.addActionListener(this);
8866
insertionButton.setBackground(Color.WHITE);
89-
90-
sizeChanger = new JComboBox<String>(sizeOptions);
67+
68+
sizeChanger = new JComboBox<String>(SIZE_OPTIONS);
9169
sizeChanger.addActionListener(this);
9270
}
9371

72+
public void setFrame() {
73+
frame.setVisible(true);
74+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75+
frame.setResizable(false);
76+
77+
buttonPanel = new JPanel();
78+
buttonPanel.setBackground(Color.DARK_GRAY);
79+
buttonPanel.add(resetButton);
80+
buttonPanel.add(bubbleButton);
81+
buttonPanel.add(selectionButton);
82+
buttonPanel.add(insertionButton);
83+
buttonPanel.add(sizeChanger);
84+
buttonPanel.setVisible(true);
85+
86+
arrPanel = new JPanel();
87+
arrPanel.add(arrDisplay);
88+
arrPanel.setVisible(true);
89+
90+
frame.add(buttonPanel, BorderLayout.PAGE_START);
91+
frame.add(arrPanel, BorderLayout.PAGE_END);
92+
frame.pack();
93+
frame.setLocationRelativeTo(null);
94+
}
95+
9496
public void actionPerformed(ActionEvent event) {
9597
stopSort = false;
9698
doBubbleSort = false;
9799
doSelectionSort = false;
98100
doInsertionSort = false;
99101
if (event.getSource() == bubbleButton) {
100102
doBubbleSort = true;
101-
sort.execute();
103+
arrSort.execute();
102104
} else if (event.getSource() == selectionButton) {
103105
doSelectionSort = true;
104-
sort.execute();
106+
arrSort.execute();
105107
} else if (event.getSource() == insertionButton) {
106108
doInsertionSort = true;
107-
sort.execute();
109+
arrSort.execute();
108110
} else if (event.getSource() == resetButton) {
109111
reset();
110-
sort.execute(); // update the display of the array that is now shuffled
111-
} else if(event.getSource() == sizeChanger) {
112+
arrSort.execute();
113+
} else if (event.getSource() == sizeChanger) {
112114
// Create a new array of the size selected
113-
String selectedSize = (String)sizeChanger.getSelectedItem();
115+
String selectedSize = (String) sizeChanger.getSelectedItem();
114116
setN(Integer.valueOf(selectedSize));
115117
arr = new Integer[n];
116118
arr = fillArr(arr);
117-
System.out.println(arr.length);
118119
// Clear and paint the new array
119120
reset();
120-
sort.execute();
121+
arrSort.execute();
121122
}
122123
}
123124

124125
public void reset() {
125126
setStopSort(true);
126127
shuffleArr(arr);
127-
displayArr.clearSwappedIndexes();
128-
displayArr.setFramesPainted(0);
129-
displayArr.setComplete(false);
130-
displayArr.setArr(arr);
131-
resetSwingWorker(this, arr, displayArr);
128+
arrDisplay.clearSwappedIndexes();
129+
arrDisplay.setFramesPainted(0);
130+
arrDisplay.setComplete(false);
131+
arrDisplay.setArr(arr);
132+
resetSwingWorker(this, arr, arrDisplay);
132133
}
133-
134-
public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, DisplayArr displayArr) {
135-
sort = new Sorting(this, arr, displayArr);
134+
135+
public void resetSwingWorker(AlgVisualizer alg, Integer[] arr, ArrDisplay displayArr) {
136+
arrSort = new ArrSorting(this, arr, displayArr);
136137
}
137138

138139
public Integer[] shuffleArr(Integer[] arr) {
@@ -170,33 +171,33 @@ public JFrame getJFrame() {
170171
return frame;
171172
}
172173

173-
public DisplayArr getDisplayArr() {
174-
return displayArr;
174+
public ArrDisplay getDisplayArr() {
175+
return arrDisplay;
175176
}
176177

177178
public SwingWorker<Void, Integer[]> getSorting() {
178-
return sort;
179+
return arrSort;
179180
}
180181

181-
public void setSort(String algorithm) {
182-
if (algorithm.equals("Bubble Sort")) {
182+
public void setSort(String sort) {
183+
if (sort.equals("Bubble Sort")) {
183184
doBubbleSort = true;
184-
} else if (algorithm.equals("Insertion Sort")) {
185+
} else if (sort.equals("Insertion Sort")) {
185186
doInsertionSort = true;
186-
} else if (algorithm.equals("Selection Sort")) {
187+
} else if (sort.equals("Selection Sort")) {
187188
doSelectionSort = true;
188189
}
189190
}
190191

191192
public String getSort() {
192-
String algorithm = "Not Sorting";
193+
String sort = "Not Sorting";
193194
if (doBubbleSort)
194-
algorithm = "Bubble Sort";
195+
sort = "Bubble Sort";
195196
if (doInsertionSort)
196-
algorithm = "Insertion Sort";
197+
sort = "Insertion Sort";
197198
if (doSelectionSort)
198-
algorithm = "Selection Sort";
199-
return algorithm;
199+
sort = "Selection Sort";
200+
return sort;
200201
}
201202

202203
public boolean stopSort() {
@@ -206,11 +207,11 @@ public boolean stopSort() {
206207
public void setStopSort(boolean toSet) {
207208
stopSort = toSet;
208209
}
209-
210+
210211
public int getN() {
211212
return n;
212213
}
213-
214+
214215
public void setN(Integer n) {
215216
this.n = n;
216217
}

Diff for: src/com/example/algorithm_visualizer/DisplayArr.java renamed to src/com/example/algorithm_visualizer/ArrDisplay.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
import javax.swing.*;
55
import java.util.ArrayList;
66

7-
public class DisplayArr extends JComponent {
7+
public class ArrDisplay extends JComponent {
88

99
private static final long serialVersionUID = 1L;
10-
protected Integer[] arr;
11-
protected AlgVisualizer alg;
10+
private Integer[] arr;
11+
private AlgVisualizer algVisualizer;
1212
private int framesPainted;
1313
private ArrayList<Integer[]> swappedIndexes;
1414
private int swappedIndex1;
1515
private int swappedIndex2;
16-
private boolean complete;
16+
private boolean isComplete;
1717

18-
public DisplayArr(AlgVisualizer alg, Integer[] arr) {
19-
this.alg = alg;
18+
public ArrDisplay(AlgVisualizer alg, Integer[] arr) {
19+
this.algVisualizer = alg;
2020
this.arr = arr;
2121
swappedIndexes = new ArrayList<Integer[]>();
2222
}
@@ -26,33 +26,33 @@ public void paintComponent(Graphics g) {
2626
// Takes ~ 40ms to draw ( depending on the system )
2727
Graphics2D graphics2d = (Graphics2D) g;
2828
graphics2d.setColor(Color.DARK_GRAY);
29-
graphics2d.fillRect(0, 0, alg.getWidth(), alg.getArrDispHeight());
29+
graphics2d.fillRect(0, 0, algVisualizer.getWidth(), algVisualizer.getArrDispHeight());
3030

31-
if (alg.getSort().equals("Not Sorting") || complete) {
31+
if (algVisualizer.getSort().equals("Not Sorting") || isComplete) {
3232
swappedIndex1 = -1;
3333
swappedIndex2 = -1;
34-
} else if(!alg.stopSort()){
34+
} else if (!algVisualizer.stopSort()) {
3535
swappedIndex1 = swappedIndexes.get(framesPainted)[0];
3636
swappedIndex2 = swappedIndexes.get(framesPainted++)[1];
3737
}
3838

3939
for (int i = 0; i < arr.length; i++) {
40-
int width = (int) (alg.getWidth() / (double) arr.length);
41-
int height = arr[i] * (alg.getArrDispHeight() / arr.length);
40+
int width = (int) (algVisualizer.getWidth() / (double) arr.length);
41+
int height = arr[i] * (algVisualizer.getArrDispHeight() / arr.length);
4242
int x = i * width;
43-
int y = alg.getArrDispHeight() - height;
44-
if ((i == swappedIndex1 || i == swappedIndex2) && !alg.stopSort()) {
45-
graphics2d.setColor(Color.RED);
46-
} else if (complete) {
47-
graphics2d.setColor(Color.GREEN);
48-
} else {
49-
graphics2d.setColor(Color.WHITE);
50-
}
43+
int y = algVisualizer.getArrDispHeight() - height;
44+
if ((i == swappedIndex1 || i == swappedIndex2) && !algVisualizer.stopSort()) {
45+
graphics2d.setColor(Color.RED);
46+
} else if (isComplete) {
47+
graphics2d.setColor(Color.GREEN);
48+
} else {
49+
graphics2d.setColor(Color.WHITE);
50+
}
5151
graphics2d.fillRect(x, y, width, height);
5252
}
5353
}
54-
55-
public ArrayList<Integer[]> getSwappedIndexes(){
54+
55+
public ArrayList<Integer[]> getSwappedIndexes() {
5656
return swappedIndexes;
5757
}
5858

@@ -73,17 +73,17 @@ public int getFramesPainted() {
7373
}
7474

7575
public boolean isComplete() {
76-
return complete;
76+
return isComplete;
7777
}
7878

7979
public void setComplete(boolean complete) {
80-
this.complete = complete;
80+
this.isComplete = complete;
8181
}
82-
82+
8383
public Integer[] getArr() {
8484
return arr;
8585
}
86-
86+
8787
public void setArr(Integer[] arr) {
8888
this.arr = arr;
8989
}

0 commit comments

Comments
 (0)