1
+ // Main GUI part of Sorting.
2
+
1
3
import javax .swing .*;
2
4
import java .awt .*;
3
5
import java .awt .event .*;
4
6
import java .util .*;
5
7
6
8
public class Sorting extends Main {
9
+ // Object of the SortingAlgorithm, which includes the sorting algorithms
7
10
SortingAlgorithm sortAlgo = new SortingAlgorithm ();
8
11
9
- // Panels
12
+ // Panels: pPanel1 - option bar, pPanel2 - visualization bar
10
13
JPanel pPanel1 , pPanel2 ;
11
14
12
- // Sorting Buttons
13
- JButton jbtRandomize , jbtMerge , jbtBubble , jbtInsertion , jbtSelection , jbtStart ; // Sorting Buttons
14
-
15
- // Random Creator
16
- Random rand = new Random ();
15
+ // Option buttons for choosing sorting techniques, speed, and size of array
16
+ // Will be added to pPanel1
17
+ JButton jbtRandomize , jbtMerge , jbtBubble , jbtInsertion , jbtSelection , jbtStart ;
17
18
18
19
// Progress Bar
19
20
JProgressBar jb1 ;
20
21
21
22
Sorting (){
22
- // Create Panel
23
23
// Panel for options (bubble sort, insertion sort...)
24
24
pPanel1 = new JPanel ();
25
25
pPanel1 .setLayout (new GridLayout (1 , 7 ));
26
26
pPanel1 .setBackground (Color .CYAN );
27
27
28
- // Panel for main algorithm
28
+ // Panel for visualization part
29
29
pPanel2 = new JPanel ();
30
30
pPanel2 .setLayout (new BorderLayout ());
31
31
32
- // Buttons for sorting
32
+ // Buttons
33
33
jbtRandomize = new JButton ("Randomize" );//create button
34
34
jbtMerge = new JButton ("Merge Sort" );//create button
35
35
jbtBubble = new JButton ("Bubble Sort" );//create button
@@ -43,15 +43,15 @@ public class Sorting extends Main {
43
43
// jb1.setValue(rand.nextInt(100));
44
44
// jb1.setStringPainted(true);
45
45
46
- // Adding elements to Panel 1
47
- pPanel1 .add (jbtRandomize ); pPanel1 .add (jbtMerge ); pPanel1 . add ( jbtSelection );
48
- pPanel1 .add (jbtBubble ); pPanel1 .add (jbtInsertion ); pPanel1 .add (jbtStart );
46
+ // Adding elements to pPanel1
47
+ pPanel1 .add (jbtRandomize ); pPanel1 .add (jbtStart );
48
+ pPanel1 .add (jbtMerge ); pPanel1 .add (jbtSelection ); pPanel1 .add (jbtBubble ); pPanel1 . add ( jbtInsertion );
49
49
50
- // Adding elements to Panel 2
50
+ // Adding elements to pPanel2
51
51
pPanel2 .add (sortAlgo , BorderLayout .CENTER );
52
52
// pPanel2.add(jb1, BorderLayout.WEST);
53
53
54
- // Register listeners
54
+ // Register listener, event handling
55
55
ListenerClass listener = new ListenerClass ();
56
56
jbtRandomize .addActionListener (listener );
57
57
jbtMerge .addActionListener (listener );
@@ -60,18 +60,21 @@ public class Sorting extends Main {
60
60
jbtSelection .addActionListener (listener );
61
61
jbtStart .addActionListener (listener );
62
62
63
- // Add Panels to the panel
63
+ // Add Panels to the Main JFrame
64
64
add (pPanel1 , BorderLayout .NORTH );
65
65
add (pPanel2 , BorderLayout .CENTER );
66
66
}
67
67
68
68
class ListenerClass implements ActionListener {
69
+ // Handles the Button operations
70
+
69
71
public void actionPerformed (ActionEvent e ) {
70
72
if (e .getSource () == jbtRandomize ) {
71
73
sortAlgo .initShuffler ();
72
74
}
73
- else if (e .getSource () == jbtMerge )
75
+ else if (e .getSource () == jbtMerge ) {
74
76
System .out .println ("jbtMerge button clicked" );
77
+ }
75
78
else if (e .getSource () == jbtBubble ) {
76
79
sortAlgo .bubbleSort (); // Bubble sort algotithm
77
80
sortAlgo .initShuffler (); // shuffling
@@ -82,13 +85,14 @@ else if (e.getSource() == jbtInsertion) {
82
85
sortAlgo .initShuffler (); // shuffling
83
86
System .out .println ("jbtInsertion button clicked" );
84
87
}
85
- else if (e .getSource () == jbtSelection )
88
+ else if (e .getSource () == jbtSelection ) {
86
89
System .out .println ("jbtSelection button clicked" );
90
+ }
87
91
else if (e .getSource () == jbtStart ) {
88
92
System .out .println ("jbtStart button clicked" );
89
93
}
90
-
91
94
// setVisible(false); // will close the previous window
92
95
}
93
- }
94
- }
96
+ } // ListenerClass
97
+
98
+ } // Sorting
0 commit comments