1+ // Main GUI part of Sorting.
2+ 13import javax .swing .*;
24import java .awt .*;
35import java .awt .event .*;
46import java .util .*;
57
68public class Sorting extends Main {
9+ // Object of the SortingAlgorithm, which includes the sorting algorithms
710 SortingAlgorithm sortAlgo = new SortingAlgorithm ();
811
9- // Panels
12+ // Panels: pPanel1 - option bar, pPanel2 - visualization bar
1013 JPanel pPanel1 , pPanel2 ;
1114
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 ;
1718
1819 // Progress Bar
1920 JProgressBar jb1 ;
2021
2122 Sorting (){
22- // Create Panel
2323 // Panel for options (bubble sort, insertion sort...)
2424 pPanel1 = new JPanel ();
2525 pPanel1 .setLayout (new GridLayout (1 , 7 ));
2626 pPanel1 .setBackground (Color .CYAN );
2727
28- // Panel for main algorithm
28+ // Panel for visualization part
2929 pPanel2 = new JPanel ();
3030 pPanel2 .setLayout (new BorderLayout ());
3131
32- // Buttons for sorting
32+ // Buttons
3333 jbtRandomize = new JButton ("Randomize" );//create button
3434 jbtMerge = new JButton ("Merge Sort" );//create button
3535 jbtBubble = new JButton ("Bubble Sort" );//create button
@@ -43,15 +43,15 @@ public class Sorting extends Main {
4343 // jb1.setValue(rand.nextInt(100));
4444 // jb1.setStringPainted(true);
4545
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 );
4949
50- // Adding elements to Panel 2
50+ // Adding elements to pPanel2
5151 pPanel2 .add (sortAlgo , BorderLayout .CENTER );
5252 // pPanel2.add(jb1, BorderLayout.WEST);
5353
54- // Register listeners
54+ // Register listener, event handling
5555 ListenerClass listener = new ListenerClass ();
5656 jbtRandomize .addActionListener (listener );
5757 jbtMerge .addActionListener (listener );
@@ -60,18 +60,21 @@ public class Sorting extends Main {
6060 jbtSelection .addActionListener (listener );
6161 jbtStart .addActionListener (listener );
6262
63- // Add Panels to the panel
63+ // Add Panels to the Main JFrame
6464 add (pPanel1 , BorderLayout .NORTH );
6565 add (pPanel2 , BorderLayout .CENTER );
6666 }
6767
6868 class ListenerClass implements ActionListener {
69+ // Handles the Button operations
70+ 6971 public void actionPerformed (ActionEvent e ) {
7072 if (e .getSource () == jbtRandomize ) {
7173 sortAlgo .initShuffler ();
7274 }
73- else if (e .getSource () == jbtMerge )
75+ else if (e .getSource () == jbtMerge ) {
7476 System .out .println ("jbtMerge button clicked" );
77+ }
7578 else if (e .getSource () == jbtBubble ) {
7679 sortAlgo .bubbleSort (); // Bubble sort algotithm
7780 sortAlgo .initShuffler (); // shuffling
@@ -82,13 +85,14 @@ else if (e.getSource() == jbtInsertion) {
8285 sortAlgo .initShuffler (); // shuffling
8386 System .out .println ("jbtInsertion button clicked" );
8487 }
85- else if (e .getSource () == jbtSelection )
88+ else if (e .getSource () == jbtSelection ) {
8689 System .out .println ("jbtSelection button clicked" );
90+ }
8791 else if (e .getSource () == jbtStart ) {
8892 System .out .println ("jbtStart button clicked" );
8993 }
90- 9194 // setVisible(false); // will close the previous window
9295 }
93- }
94- }
96+ } // ListenerClass
97+ 98+ } // Sorting
0 commit comments