@@ -20,13 +20,14 @@ public class SortingAlgorithm extends JPanel {
2020 setBackground (Color .BLACK );
2121 setPreferredSize (new Dimension (WIDTH , HEIGHT ));
2222 initBarHeight (); // initialize the height of each bar
23- initShuffler ();
23+ initShuffler ();// shuffle each bar
2424 }
2525
2626 @ Override
2727 public void paintComponent (Graphics g ) {
2828 super .paintComponent (g );
2929
30+ // Drawing the rectangles
3031 Graphics2D g2d = (Graphics2D )g ;
3132 g2d .setColor (Color .CYAN );
3233 Rectangle2D .Float bar ;
@@ -35,23 +36,20 @@ public void paintComponent(Graphics g) {
3536 g2d .fill (bar ); // g2d.draw(bar);
3637 }
3738
39+ // Color setter for the current object
3840 g2d .setColor (Color .RED );
3941 bar = new Rectangle2D .Float (current_index * BAR_WIDTH , 0 , BAR_WIDTH , bar_height [current_index ]);
4042 g2d .fill (bar );
4143
42- g2d .setColor (Color .GREEN );
44+ // Color setter for the traversing object
45+ g2d .setColor (Color .YELLOW );
4346 bar = new Rectangle2D .Float (traversing_index * BAR_WIDTH , 0 , BAR_WIDTH , bar_height [traversing_index ]);
4447 g2d .fill (bar );
4548 }
46- 47- public void initBarHeight () {
48- float interval = (float )HEIGHT / SIZE ;
49- for (int i = 0 ; i < SIZE ; i ++) {
50- bar_height [i ] = i * interval ;
51- }
52- }
5349
5450 public void initSorter () {
51+ /*Insertion sort algorithm*/
52+ 5553 sorter = new SwingWorker <>() {
5654 @ Override
5755 public Void doInBackground () throws InterruptedException {
@@ -74,6 +72,8 @@ public Void doInBackground() throws InterruptedException {
7472 }
7573
7674 public void initShuffler () {
75+ /*Shuffles each bar*/
76+ 7777 shuffler = new SwingWorker <>() {
7878 @ Override
7979 public Void doInBackground () throws InterruptedException {
@@ -90,7 +90,6 @@ public Void doInBackground() throws InterruptedException {
9090 }
9191 return null ;
9292 }
93- 9493 @ Override
9594 public void done () {
9695 super .done ();
@@ -99,8 +98,17 @@ public void done() {
9998 };
10099 shuffler .execute ();
101100 }
101+ 102+ public void initBarHeight () {
103+ /*Initialize the height of each bar*/
104+ float interval = (float )HEIGHT / SIZE ;
105+ for (int i = 0 ; i < SIZE ; i ++) {
106+ bar_height [i ] = i * interval ;
107+ }
108+ }
102109
103110 public void swap (int indexA , int indexB ) {
111+ /*Swaps the elements*/
104112 float temp = bar_height [indexA ];
105113 bar_height [indexA ] = bar_height [indexB ];
106114 bar_height [indexB ] = temp ;
0 commit comments