Skip to content

Commit 151da60

Browse files
initial commit
1 parent d6cae88 commit 151da60

File tree

4 files changed

+626
-0
lines changed

4 files changed

+626
-0
lines changed

src/gui/shuttle/sort/Application.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package gui.shuttle.sort;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
6+
/**
7+
* This program can be used to display how different sorting methods look.
8+
* @author Leon Wilberforce
9+
*/
10+
11+
public class Application {
12+
13+
public static void main(String[] args) {
14+
15+
//Creating the JFrame what will contain the menu.
16+
JFrame application = new JFrame("Visual Sorting");
17+
18+
application.setSize(new Dimension(300,300));
19+
20+
MenuPanel menuPanel = new MenuPanel();
21+
22+
application.add(menuPanel);
23+
24+
application.setVisible(true);
25+
26+
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27+
28+
}
29+
30+
}

src/gui/shuttle/sort/BarPanel.java

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package gui.shuttle.sort;
7+
8+
import java.awt.Color;
9+
import java.awt.Dimension;
10+
import javax.swing.BorderFactory;
11+
import javax.swing.JLabel;
12+
import javax.swing.JPanel;
13+
import javax.swing.border.Border;
14+
15+
/**
16+
* <h1> Bar Panel</h1>
17+
* @author leon Wilberforce
18+
*/
19+
public class BarPanel extends JPanel{
20+
JLabel[] bars;
21+
int[] array;
22+
private int speed = 500;
23+
24+
public BarPanel(int arraySize, int[] array, int speed) throws InterruptedException {
25+
26+
this.speed = speed;
27+
28+
this.setBackground(Color.black);
29+
30+
this.array = array;
31+
bars = new JLabel[arraySize];
32+
33+
// For loop to make the array of JLabels with the text and size that is contained within the array.
34+
for (int i = 0; i < this.array.length; i++) {
35+
bars[i] = new JLabel(String.valueOf(array[i]));
36+
bars[i].setPreferredSize(new Dimension(20, this.array[i]*2));
37+
38+
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
39+
bars[i].setBackground(Color.WHITE);
40+
bars[i].setOpaque(true);
41+
bars[i].setBorder(border);
42+
// bars[i].setVerticalAlignment(SwingConstants.BOTTOM); // Need to find a way to align this to bottom.
43+
this.add(bars[i]);
44+
45+
}
46+
}
47+
48+
//The sorting method for the Shuttle Sort.
49+
public void ShuttleSort() throws InterruptedException{
50+
Thread.sleep(1000);
51+
52+
int passes = 0;
53+
int checks = 0;
54+
int swaps = 0;
55+
boolean isDone;
56+
for (int i = 0; i < array.length-1; i++) {
57+
bars[i+1].setBackground(Color.GREEN);
58+
bars[i+1].setBorder(BorderFactory.createLineBorder(Color.GREEN));
59+
bars[i].setBackground(Color.WHITE);
60+
bars[i].setBorder(BorderFactory.createLineBorder(Color.WHITE));
61+
passes++;
62+
this.revalidate();
63+
Thread.sleep(speed);
64+
for (int j = i; j>=0; j--) {
65+
checks++;
66+
if (array[j] > array[j+1]) {
67+
swaps++;
68+
bars[j].setBorder(BorderFactory.createLineBorder(Color.RED));
69+
bars[j+1].setBorder(BorderFactory.createLineBorder(Color.RED));
70+
bars[j].setBackground(Color.red);
71+
bars[j+1].setBackground(Color.red);
72+
bars[i+1].setBackground(Color.GREEN);
73+
bars[i+1].setBorder(BorderFactory.createLineBorder(Color.GREEN));
74+
swap(array, j, j+1);
75+
this.revalidate();
76+
Thread.sleep(speed);
77+
78+
updateBars();
79+
80+
Thread.sleep(speed);
81+
82+
bars[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
83+
bars[j+1].setBorder(BorderFactory.createLineBorder(Color.BLACK));
84+
bars[j].setBackground(Color.WHITE);
85+
bars[j+1].setBackground(Color.WHITE);
86+
bars[i+1].setBackground(Color.GREEN);
87+
bars[i+1].setBorder(BorderFactory.createLineBorder(Color.GREEN));
88+
89+
this.revalidate();
90+
91+
}else{
92+
break;
93+
}
94+
}
95+
bars[i].setBackground(Color.WHITE);
96+
bars[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
97+
}
98+
99+
for (int i = 0; i <= array.length-1; i++) {
100+
Thread.sleep(50);
101+
bars[i].setBackground(Color.GREEN);
102+
this.revalidate();
103+
}
104+
105+
// System.out.println("\nPasses: "+ passes + "\nChecks: " + checks + "\nSwaps: " +swaps + "\n");
106+
// for(int a: array){
107+
// System.out.print(a+" ");
108+
// }
109+
}
110+
111+
// The sorting method of the bubble sort.
112+
public void BubbleSort() throws InterruptedException{
113+
Thread.sleep(1000);
114+
int passes = 0;
115+
int checks = 0;
116+
int swaps = 0;
117+
for (int i = 0; i < array.length; i++) {
118+
passes++;
119+
this.revalidate();
120+
Thread.sleep(speed);
121+
for (int j = 0; j < array.length-1; j++) {
122+
checks++;
123+
if (array[j]>array[j+1]) {
124+
swaps++;
125+
bars[j].setBorder(BorderFactory.createLineBorder(Color.RED));
126+
bars[j+1].setBorder(BorderFactory.createLineBorder(Color.RED));
127+
bars[j].setBackground(Color.red);
128+
bars[j+1].setBackground(Color.red);
129+
swap(array, j, j+1);
130+
this.revalidate();
131+
Thread.sleep(speed);
132+
133+
updateBars();
134+
135+
Thread.sleep(speed);
136+
137+
bars[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
138+
bars[j+1].setBorder(BorderFactory.createLineBorder(Color.BLACK));
139+
bars[j].setBackground(Color.WHITE);
140+
bars[j+1].setBackground(Color.WHITE);
141+
142+
this.revalidate();
143+
}
144+
}
145+
}
146+
for (int i = 0; i <= array.length-1; i++) {
147+
Thread.sleep(50);
148+
bars[i].setBackground(Color.GREEN);
149+
this.revalidate();
150+
}
151+
152+
System.out.println("BubbleSort: ");
153+
System.out.println("\nPasses: "+ passes + "\nChecks: " + checks + "\nSwaps: " +swaps + "\n");
154+
for(int a: array){
155+
System.out.print(a+" ");
156+
}
157+
158+
}
159+
160+
// The sorting method for the shaker sort.
161+
public void shakerSort() throws InterruptedException{
162+
Thread.sleep(1000);
163+
int passes = 0;
164+
int checks = 0;
165+
int swaps = 0;
166+
for (int i = 0; i < array.length; i++) {
167+
bars[i].setBackground(Color.RED);
168+
bars[i].setBorder(BorderFactory.createLineBorder(Color.RED));
169+
passes++;
170+
this.revalidate();
171+
Thread.sleep(speed);
172+
for (int j = 0; j < array.length-1; j++) {
173+
checks++;
174+
bars[j].setBorder(BorderFactory.createLineBorder(Color.RED));
175+
bars[j].setBackground(Color.red);
176+
this.revalidate();
177+
if (array[j]>array[j+1]) {
178+
swaps++;
179+
bars[j].setBorder(BorderFactory.createLineBorder(Color.RED));
180+
bars[j+1].setBorder(BorderFactory.createLineBorder(Color.RED));
181+
bars[j].setBackground(Color.red);
182+
bars[j+1].setBackground(Color.red);
183+
swap(array, j, j+1);
184+
this.revalidate();
185+
Thread.sleep(speed);
186+
updateBars();
187+
188+
Thread.sleep(speed);
189+
190+
bars[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
191+
bars[j+1].setBorder(BorderFactory.createLineBorder(Color.BLACK));
192+
bars[j].setBackground(Color.WHITE);
193+
bars[j+1].setBackground(Color.WHITE);
194+
195+
this.revalidate();
196+
}
197+
bars[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
198+
bars[j].setBackground(Color.WHITE);
199+
this.revalidate();
200+
}
201+
for (int j = array.length-1; j > 0; j--) {
202+
checks++;
203+
if (array[j] < array[j-1]) {
204+
swaps++;
205+
bars[j].setBorder(BorderFactory.createLineBorder(Color.RED));
206+
bars[j-1].setBorder(BorderFactory.createLineBorder(Color.RED));
207+
bars[j].setBackground(Color.red);
208+
bars[j-1].setBackground(Color.red);
209+
swap(array, j, j-1);
210+
this.revalidate();
211+
Thread.sleep(speed);
212+
updateBars();
213+
214+
Thread.sleep(speed);
215+
216+
bars[j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
217+
bars[j-1].setBorder(BorderFactory.createLineBorder(Color.BLACK));
218+
bars[j].setBackground(Color.WHITE);
219+
bars[j-1].setBackground(Color.WHITE);
220+
221+
this.revalidate();
222+
}
223+
}
224+
}
225+
for (int i = 0; i <= array.length-1; i++) {
226+
Thread.sleep(50);
227+
bars[i].setBackground(Color.GREEN);
228+
this.revalidate();
229+
}
230+
System.out.println("Shaker Sort");
231+
System.out.println("\nPasses: "+ passes + "\nChecks: " + checks + "\nSwaps: " +swaps + "\n");
232+
for(int a: array){
233+
System.out.print(a+" ");
234+
}
235+
236+
}
237+
238+
// The method used to update the bars
239+
public void updateBars(){
240+
// A for loop that changes all the bars within the screen to have the text and size equal to the array.
241+
for (int k = 0; k < this.array.length; k++) {
242+
bars[k].setText(String.valueOf(array[k]));
243+
bars[k].setPreferredSize(new Dimension(20, this.array[k]*2));
244+
245+
this.revalidate();
246+
}
247+
}
248+
249+
// swap method used for swapping elements within the array.
250+
static void swap(int[] a, int k, int l){
251+
int temp = a[k];
252+
a[k] = a[l];
253+
a[l] = temp;
254+
}
255+
256+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package gui.shuttle.sort;
7+
8+
import java.awt.BorderLayout;
9+
import java.util.Random;
10+
import javax.swing.JFrame;
11+
import javax.swing.UIManager;
12+
13+
/**
14+
* This
15+
* @author Leon Wilberforce
16+
*/
17+
public class GUIShuttleSort {
18+
BarPanel myBars;
19+
20+
21+
public GUIShuttleSort(int[] array, int speed) throws InterruptedException{
22+
// TODO code application logic here
23+
24+
JFrame myFrame = new JFrame("GUI Stuff");
25+
26+
27+
myBars = new BarPanel(array.length, array, speed);
28+
myFrame.add(myBars, BorderLayout.SOUTH);
29+
myFrame.setVisible(true);
30+
myFrame.pack();
31+
32+
33+
34+
35+
36+
}
37+
38+
}

0 commit comments

Comments
 (0)