Skip to content

Commit 2eb8bab

Browse files
committed
slader added - NOT working yet
1 parent 4efb434 commit 2eb8bab

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

Main.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void actionPerformed(ActionEvent e) {
6363
}
6464
else if (e.getSource() == menuItem2) {
6565
// Creating Object
66-
Sorting sort = new Sorting();
66+
new Sorting();
6767
System.out.println("Menu Item 2 choosed");
6868
}
6969
else if (e.getSource() == menuItem2) {

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ Updated: December 13, 2020
3636

3737
- https://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint
3838

39+
- Merge sort: https://www.baeldung.com/java-merge-sort
40+
41+
- http://www.java2s.com/Tutorials/Java/Swing/JSlider/Add_change_event_listener_to_a_JSlider_in_Java.htm

Sorting.java

+24-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import javax.swing.*;
44
import java.awt.*;
55
import java.awt.event.*;
6-
import java.util.*;
6+
7+
import javax.swing.event.ChangeEvent;
8+
import javax.swing.event.ChangeListener;
79

810
public class Sorting extends Main {
911
// Object of the SortingAlgorithm, which includes the sorting algorithms
@@ -30,26 +32,26 @@ public class Sorting extends Main {
3032
pPanel2.setLayout(new BorderLayout());
3133

3234
// Buttons
33-
jbtRandomize = new JButton("Randomize");//create button
34-
jbtMerge = new JButton("Merge Sort");//create button
35-
jbtBubble = new JButton("Bubble Sort");//create button
36-
jbtInsertion = new JButton("Insertion Sort");//create button
37-
jbtSelection = new JButton("Selection Sort");//create button
38-
jbtStart = new JButton("Start");//create button
35+
jbtRandomize = new JButton("Randomize");
36+
jbtMerge = new JButton("Merge Sort");
37+
jbtBubble = new JButton("Bubble Sort");
38+
jbtInsertion = new JButton("Insertion Sort");
39+
jbtSelection = new JButton("Selection Sort");
40+
jbtStart = new JButton("Start");
3941
jbtStart.setBackground(Color.GRAY);
4042

41-
// Progress bar
42-
// jb1 = new JProgressBar(0,100);
43-
// jb1.setValue(rand.nextInt(100));
44-
// jb1.setStringPainted(true);
43+
// Slider
44+
JSlider slider = new JSlider(0, 100, 0);
45+
slider.setPreferredSize(new Dimension(150, 30));
4546

4647
// Adding elements to pPanel1
47-
pPanel1.add(jbtRandomize); pPanel1.add(jbtStart);
48+
pPanel1.add(jbtRandomize);
4849
pPanel1.add(jbtMerge); pPanel1.add(jbtSelection); pPanel1.add(jbtBubble); pPanel1.add(jbtInsertion);
50+
// pPanel1.add(jbtStart);
51+
pPanel1.add(slider, BorderLayout.WEST);
4952

5053
// Adding elements to pPanel2
5154
pPanel2.add(sortAlgo, BorderLayout.CENTER);
52-
// pPanel2.add(jb1, BorderLayout.WEST);
5355

5456
// Register listener, event handling
5557
ListenerClass listener = new ListenerClass();
@@ -63,6 +65,13 @@ public class Sorting extends Main {
6365
// Add Panels to the Main JFrame
6466
add(pPanel1, BorderLayout.NORTH);
6567
add(pPanel2, BorderLayout.CENTER);
68+
69+
// Slider settings
70+
slider.addChangeListener(new ChangeListener() {
71+
public void stateChanged(ChangeEvent event) {
72+
int value = slider.getValue();
73+
}
74+
});
6675
}
6776

6877
class ListenerClass implements ActionListener {
@@ -73,17 +82,16 @@ public void actionPerformed(ActionEvent e) {
7382
sortAlgo.initShuffler();
7483
}
7584
else if (e.getSource() == jbtMerge) {
76-
System.out.println("jbtMerge button clicked");
85+
sortAlgo.mergeSort(); // Bubble sort algotithm
86+
sortAlgo.initShuffler(); // shuffling
7787
}
7888
else if (e.getSource() == jbtBubble) {
7989
sortAlgo.bubbleSort(); // Bubble sort algotithm
8090
sortAlgo.initShuffler(); // shuffling
81-
System.out.println("jbtBubble button clicked");
8291
}
8392
else if (e.getSource() == jbtInsertion) {
8493
sortAlgo.insertionSort(); // Insertion algotithm
8594
sortAlgo.initShuffler(); // shuffling
86-
System.out.println("jbtInsertion button clicked");
8795
}
8896
else if (e.getSource() == jbtSelection) {
8997
System.out.println("jbtSelection button clicked");

SortingAlgorithm.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import java.awt.Graphics;
66
import java.awt.Graphics2D;
77
import java.awt.geom.Rectangle2D;
8+
import java.security.PublicKey;
89
import java.util.Random;
910

1011
import javax.swing.JPanel;
1112
import javax.swing.SwingWorker;
1213

1314
public class SortingAlgorithm extends JPanel {
14-
private final int WIDTH = 800, HEIGHT = WIDTH * 9 /16;
15-
private final int SIZE = 100; // the number if sorting elements
15+
private final int WIDTH = 800, HEIGHT = WIDTH * 9 / 16;
16+
public int SIZE = 50; // the number if sorting elements
1617
private final float BAR_WIDTH = (float)WIDTH / SIZE; // bar width
1718
private float[] bar_height = new float[SIZE]; // height of bars
1819
private SwingWorker<Void, Void> shuffler, sorter;
@@ -104,6 +105,32 @@ public Void doInBackground() throws InterruptedException {
104105
};
105106
}
106107

108+
public void mergeSort() {
109+
/*Merge sorting algorithm*/
110+
// Change code from bubbleSort to mergeSort
111+
112+
sorter = new SwingWorker<>() {
113+
@Override
114+
public Void doInBackground() throws InterruptedException {
115+
for(current_index = 0; current_index < SIZE; current_index++) {
116+
for(traversing_index = 1; traversing_index < (SIZE - current_index); traversing_index++) {
117+
if(bar_height[traversing_index - 1] > bar_height[traversing_index]) {
118+
swap(traversing_index, traversing_index - 1);
119+
traversing_index--; // just for annimation
120+
121+
Thread.sleep(1); // controls the speed
122+
repaint(); // we need it because we ofter replace the contents of a JPanel
123+
}
124+
}
125+
}
126+
current_index = 0;
127+
traversing_index = 0;
128+
129+
return null;
130+
}
131+
};
132+
}
133+
107134
public void initShuffler() {
108135
/*Shuffles each bar*/
109136
shuffler = new SwingWorker<>() {

0 commit comments

Comments
 (0)