Skip to content

Commit 4efb434

Browse files
committed
updated
1 parent eb27014 commit 4efb434

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ Updated: December 13, 2020
2828

2929
- https://www.youtube.com/watch?v=RxjXC1SM1A4
3030

31-
- https://stackoverflow.com/questions/4246351/creating-random-colour-in-java
31+
- https://stackoverflow.com/questions/4246351/creating-random-colour-in-java
32+
33+
- https://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java
34+
35+
- https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html
36+
37+
- https://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint
38+

Sorting.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1+
// Main GUI part of Sorting.
2+
13
import javax.swing.*;
24
import java.awt.*;
35
import java.awt.event.*;
46
import java.util.*;
57

68
public 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

SortingAlgorithm.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Implementation of Sorting algorithms
2+
13
import java.awt.Color;
24
import java.awt.Dimension;
35
import java.awt.Graphics;
@@ -20,22 +22,23 @@ public class SortingAlgorithm extends JPanel {
2022
setBackground(Color.BLACK);
2123
setPreferredSize(new Dimension(WIDTH, HEIGHT));
2224
initBarHeight(); // initialize the height of each bar
23-
//initShuffler(); // shuffle each bar
25+
// initShuffler(); // shuffle each bar
2426
}
2527

2628
@Override
2729
public void paintComponent(Graphics g) {
2830
super.paintComponent(g);
2931

30-
Random random = new Random(); // Random
32+
// Create randomizer
33+
Random random = new Random();
3134

3235
// Drawing the rectangles
3336
Graphics2D g2d = (Graphics2D)g;
3437
Rectangle2D.Float bar;
3538

3639
for(int i = 0; i < SIZE; i++ ) {
3740
final float hue = random.nextFloat();
38-
final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
41+
final float saturation = 0.9f; //1.0 for brilliant, 0.0 for dull
3942
final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
4043

4144
g2d.setColor(Color.getHSBColor(hue, saturation, luminance));
@@ -78,7 +81,7 @@ public Void doInBackground() throws InterruptedException {
7881
}
7982

8083
public void bubbleSort() {
81-
/*Buuble sort algorithm*/
84+
/*Bubble sorting algorithm*/
8285
sorter = new SwingWorker<>() {
8386
@Override
8487
public Void doInBackground() throws InterruptedException {
@@ -89,7 +92,7 @@ public Void doInBackground() throws InterruptedException {
8992
traversing_index--; // just for annimation
9093

9194
Thread.sleep(1); // controls the speed
92-
repaint(); //
95+
repaint(); // we need it because we ofter replace the contents of a JPanel
9396
}
9497
}
9598
}

0 commit comments

Comments
 (0)