9
9
10
10
public class AlgVisualizer implements ActionListener {
11
11
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 ;
16
16
private Integer [] arr ;
17
17
private JFrame frame = new JFrame ("Algorithm Visualizer" );
18
18
private JPanel arrPanel ;
19
- private DisplayArr displayArr ;
19
+ private ArrDisplay arrDisplay ;
20
20
private JPanel buttonPanel ;
21
21
private JButton bubbleButton ;
22
22
private JButton insertionButton ;
23
23
private JButton selectionButton ;
24
24
private JButton resetButton ;
25
25
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 ;
28
28
private boolean doBubbleSort ;
29
29
private boolean doInsertionSort ;
30
30
private boolean doSelectionSort ;
31
31
private boolean stopSort ;
32
32
33
33
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 ();
37
37
}
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
+
63
39
public void initializeVars () {
64
40
41
+ setN (10 );
42
+
65
43
arr = new Integer [n ];
66
44
arr = fillArr (arr );
67
45
arr = shuffleArr (arr );
68
46
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 ));
71
49
72
- sort = new Sorting (this , arr , displayArr );
50
+ arrSort = new ArrSorting (this , arr , arrDisplay );
73
51
74
52
resetButton = new JButton ("Reset" );
75
53
resetButton .addActionListener (this );
@@ -86,53 +64,76 @@ public void initializeVars() {
86
64
insertionButton = new JButton ("Insertion Sort" );
87
65
insertionButton .addActionListener (this );
88
66
insertionButton .setBackground (Color .WHITE );
89
-
90
- sizeChanger = new JComboBox <String >(sizeOptions );
67
+
68
+ sizeChanger = new JComboBox <String >(SIZE_OPTIONS );
91
69
sizeChanger .addActionListener (this );
92
70
}
93
71
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
+
94
96
public void actionPerformed (ActionEvent event ) {
95
97
stopSort = false ;
96
98
doBubbleSort = false ;
97
99
doSelectionSort = false ;
98
100
doInsertionSort = false ;
99
101
if (event .getSource () == bubbleButton ) {
100
102
doBubbleSort = true ;
101
- sort .execute ();
103
+ arrSort .execute ();
102
104
} else if (event .getSource () == selectionButton ) {
103
105
doSelectionSort = true ;
104
- sort .execute ();
106
+ arrSort .execute ();
105
107
} else if (event .getSource () == insertionButton ) {
106
108
doInsertionSort = true ;
107
- sort .execute ();
109
+ arrSort .execute ();
108
110
} else if (event .getSource () == resetButton ) {
109
111
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 ) {
112
114
// Create a new array of the size selected
113
- String selectedSize = (String )sizeChanger .getSelectedItem ();
115
+ String selectedSize = (String ) sizeChanger .getSelectedItem ();
114
116
setN (Integer .valueOf (selectedSize ));
115
117
arr = new Integer [n ];
116
118
arr = fillArr (arr );
117
- System .out .println (arr .length );
118
119
// Clear and paint the new array
119
120
reset ();
120
- sort .execute ();
121
+ arrSort .execute ();
121
122
}
122
123
}
123
124
124
125
public void reset () {
125
126
setStopSort (true );
126
127
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 );
132
133
}
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 );
136
137
}
137
138
138
139
public Integer [] shuffleArr (Integer [] arr ) {
@@ -170,33 +171,33 @@ public JFrame getJFrame() {
170
171
return frame ;
171
172
}
172
173
173
- public DisplayArr getDisplayArr () {
174
- return displayArr ;
174
+ public ArrDisplay getDisplayArr () {
175
+ return arrDisplay ;
175
176
}
176
177
177
178
public SwingWorker <Void , Integer []> getSorting () {
178
- return sort ;
179
+ return arrSort ;
179
180
}
180
181
181
- public void setSort (String algorithm ) {
182
- if (algorithm .equals ("Bubble Sort" )) {
182
+ public void setSort (String sort ) {
183
+ if (sort .equals ("Bubble Sort" )) {
183
184
doBubbleSort = true ;
184
- } else if (algorithm .equals ("Insertion Sort" )) {
185
+ } else if (sort .equals ("Insertion Sort" )) {
185
186
doInsertionSort = true ;
186
- } else if (algorithm .equals ("Selection Sort" )) {
187
+ } else if (sort .equals ("Selection Sort" )) {
187
188
doSelectionSort = true ;
188
189
}
189
190
}
190
191
191
192
public String getSort () {
192
- String algorithm = "Not Sorting" ;
193
+ String sort = "Not Sorting" ;
193
194
if (doBubbleSort )
194
- algorithm = "Bubble Sort" ;
195
+ sort = "Bubble Sort" ;
195
196
if (doInsertionSort )
196
- algorithm = "Insertion Sort" ;
197
+ sort = "Insertion Sort" ;
197
198
if (doSelectionSort )
198
- algorithm = "Selection Sort" ;
199
- return algorithm ;
199
+ sort = "Selection Sort" ;
200
+ return sort ;
200
201
}
201
202
202
203
public boolean stopSort () {
@@ -206,11 +207,11 @@ public boolean stopSort() {
206
207
public void setStopSort (boolean toSet ) {
207
208
stopSort = toSet ;
208
209
}
209
-
210
+
210
211
public int getN () {
211
212
return n ;
212
213
}
213
-
214
+
214
215
public void setN (Integer n ) {
215
216
this .n = n ;
216
217
}
0 commit comments