Skip to content

Commit 85f6693

Browse files
committed
SortingAlgorithm added
1 parent 8c85dbf commit 85f6693

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

InsertionSort.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import javax.swing.SwingWorker;
1212

1313
public class InsertionSort extends JPanel {
14-
private final int WIDTH = 1000, HEIGHT = WIDTH * 9 /16;
14+
private final int WIDTH = 800, HEIGHT = WIDTH * 9 /16;
1515
private final int SIZE = 200; // the number if sorting elements
1616
private final float BAR_WIDTH = (float)WIDTH / SIZE; // bar width
1717
private float[] bar_height = new float[SIZE]; // height of bars
1818
private SwingWorker<Void, Void> shuffler, sorter;
1919
private int current_index, traversing_index;
2020

21-
private InsertionSort() {
21+
InsertionSort() {
2222
setBackground(Color.BLACK);
2323
setPreferredSize(new Dimension(WIDTH, HEIGHT));
2424
initBarHeight(); // initialize the height of each bar
@@ -54,7 +54,7 @@ public void initBarHeight() {
5454
}
5555
}
5656

57-
private void initSorter() {
57+
public void initSorter() {
5858
sorter = new SwingWorker<>() {
5959
@Override
6060
public Void doInBackground() throws InterruptedException {
@@ -76,7 +76,7 @@ public Void doInBackground() throws InterruptedException {
7676
};
7777
}
7878

79-
private void initShuffler() {
79+
public void initShuffler() {
8080
shuffler = new SwingWorker<>() {
8181
@Override
8282
public Void doInBackground() throws InterruptedException {
@@ -93,7 +93,7 @@ public Void doInBackground() throws InterruptedException {
9393
}
9494
return null;
9595
}
96-
96+
9797
@Override
9898
public void done() {
9999
super.done();
@@ -103,7 +103,7 @@ public void done() {
103103
shuffler.execute();
104104
}
105105

106-
private void swap(int indexA, int indexB) {
106+
public void swap(int indexA, int indexB) {
107107
float temp = bar_height[indexA];
108108
bar_height[indexA] = bar_height[indexB];
109109
bar_height[indexB] = temp;

Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public void actionPerformed(ActionEvent e) {
6464
else if (e.getSource() == menuItem2) {
6565
// Creating Object
6666
Sorting sort = new Sorting();
67-
setVisible(false); // will close the previous window
6867
System.out.println("Menu Item 2 choosed");
6968
}
7069
else if (e.getSource() == menuItem2) {
7170
System.out.println("Menu Item 3 choosed");
7271
}
72+
setVisible(false); // will close the previous window
7373
}
7474
}
7575

Sorting.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.*;
55

66
public class Sorting extends Main {
7+
SortingAlgorithm sortAlgo = new SortingAlgorithm();
8+
79
// Values
810
ArrayList<Integer> list=new ArrayList<Integer>();//Creating arraylist
911

@@ -27,7 +29,11 @@ public class Sorting extends Main {
2729

2830
// Panel for main algorithm
2931
pPanel2 = new JPanel();
30-
pPanel2.setLayout(new GridLayout(10, 1));
32+
pPanel2.setLayout(new BorderLayout());
33+
34+
// Set backgrounds for Panels
35+
pPanel1.setBackground(Color.CYAN);
36+
pPanel2.setBackground(Color.YELLOW);
3137

3238
// Buttons for sorting
3339
jbtRandomize = new JButton("Randomize");//create button
@@ -48,11 +54,8 @@ public class Sorting extends Main {
4854
pPanel1.add(jbtBubble); pPanel1.add(jbtInsertion); pPanel1.add(jbtStart);
4955

5056
// Adding elements to Panel 2
51-
pPanel2.add(jb1, BorderLayout.WEST);
52-
53-
// Set backgrounds for Panels
54-
pPanel1.setBackground(Color.CYAN);
55-
pPanel2.setBackground(Color.YELLOW);
57+
pPanel2.add(sortAlgo, BorderLayout.CENTER);
58+
// pPanel2.add(jb1, BorderLayout.WEST);
5659

5760
// Register listeners
5861
ListenerClass listener = new ListenerClass();
@@ -77,11 +80,13 @@ else if (e.getSource() == jbtMerge)
7780
else if (e.getSource() == jbtBubble)
7881
System.out.println("jbtBubble button clicked");
7982
else if (e.getSource() == jbtInsertion)
80-
System.out.println("jbtInsertion button clicked");
83+
new SortingAlgorithm(); // Sorting algotithm
8184
else if (e.getSource() == jbtSelection)
8285
System.out.println("jbtSelection button clicked");
8386
else if (e.getSource() == jbtStart)
8487
System.out.println("jbtStart button clicked");
88+
89+
// setVisible(false); // will close the previous window
8590
}
8691
}
8792
}

SortingAlgorithm.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.awt.Color;
2+
3+
import javax.swing.JFrame;
4+
import javax.swing.JPanel;
5+
6+
public class SortingAlgorithm extends JPanel {
7+
SortingAlgorithm() {
8+
setBackground(Color.BLUE);
9+
}
10+
11+
}

0 commit comments

Comments
 (0)