Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a9071a

Browse files
committed
updated
1 parent 3acfaaa commit 2a9071a

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

‎Sorting.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,25 @@ public class Sorting extends Main {
2323
// Panel for options (bubble sort, insertion sort...)
2424
pPanel1 = new JPanel();
2525
pPanel1.setLayout(new GridLayout(1, 7));
26+
pPanel1.setBackground(Color.CYAN);
2627

2728
// Panel for main algorithm
2829
pPanel2 = new JPanel();
2930
pPanel2.setLayout(new BorderLayout());
3031

31-
// Set backgrounds for Panels
32-
pPanel1.setBackground(Color.CYAN);
33-
pPanel2.setBackground(Color.YELLOW);
34-
3532
// Buttons for sorting
3633
jbtRandomize = new JButton("Randomize");//create button
3734
jbtMerge = new JButton("Merge Sort");//create button
3835
jbtBubble = new JButton("Bubble Sort");//create button
3936
jbtInsertion = new JButton("Insertion Sort");//create button
4037
jbtSelection = new JButton("Selection Sort");//create button
4138
jbtStart = new JButton("Start");//create button
42-
jbtStart.setBackground(Color.GREEN);
39+
jbtStart.setBackground(Color.GRAY);
4340

4441
// Progress bar
45-
jb1 = new JProgressBar(0,100);
46-
jb1.setValue(rand.nextInt(100));
47-
jb1.setStringPainted(true);
42+
// jb1 = new JProgressBar(0,100);
43+
// jb1.setValue(rand.nextInt(100));
44+
// jb1.setStringPainted(true);
4845

4946
// Adding elements to Panel 1
5047
pPanel1.add(jbtRandomize); pPanel1.add(jbtMerge); pPanel1.add(jbtSelection);
@@ -85,7 +82,7 @@ else if (e.getSource() == jbtSelection)
8582
System.out.println("jbtSelection button clicked");
8683
else if (e.getSource() == jbtStart) {
8784
System.out.println("jbtStart button clicked");
88-
}
85+
}
8986

9087
// setVisible(false); // will close the previous window
9188
}

‎SortingAlgorithm.java‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /