Skip to content

Commit 6fe4691

Browse files
committed
Enhance the visuals
1 parent 5993048 commit 6fe4691

File tree

5 files changed

+106
-87
lines changed

5 files changed

+106
-87
lines changed

src/main/java/sortVisualiser/ArrayCanvas.java

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
package sortVisualiser;
22

3+
import java.awt.Color;
4+
import java.awt.Dimension;
5+
import java.awt.Graphics;
6+
import java.awt.Graphics2D;
37
import java.util.Random;
8+
import javax.swing.JPanel;
49
import static util.Sleep.sleepFor;
510

611
/**
712
* The array that can be sorted
813
* @author mhops
914
*/
10-
public class SortArray {
11-
private final ArrayCanvas arrayVisualiser;
15+
public class SortArray extends JPanel {
16+
public static final int WIN_WIDTH = 1280;
17+
public static final int WIN_HEIGHT = 720;
18+
private static final int BAR_WIDTH = 8;
19+
private static final int NUM_BARS = WIN_WIDTH / BAR_WIDTH;
1220
private int[] array;
21+
private int[] accessColourMod;
1322

14-
public SortArray(int len, ArrayCanvas arrayVisualiser) {
15-
array = new int[len];
16-
for (int i = 0; i < len; i++) {
23+
public SortArray() {
24+
setBackground(Color.darkGray);
25+
array = new int[NUM_BARS];
26+
accessColourMod = new int[NUM_BARS];
27+
for (int i = 0; i < NUM_BARS; i++) {
1728
array[i] = i;
18-
}
19-
shuffleArray();
20-
21-
this.arrayVisualiser = arrayVisualiser;
22-
}
23-
24-
private void shuffleArray() {
25-
Random rng = new Random();
26-
for (int i = 0; i < array.length; i++) {
27-
int swapWithIndex = rng.nextInt(array.length - 1);
28-
int temp = array[i];
29-
array[i] = array[swapWithIndex];
30-
array[swapWithIndex] = temp;
29+
accessColourMod[i] = 0;
3130
}
3231
}
3332

34-
public int size() {
33+
public int arraySize() {
3534
return array.length;
3635
}
3736

@@ -43,7 +42,50 @@ public void swapUpdate(int firstIndex, int secondIndex) {
4342
int temp = array[firstIndex];
4443
array[firstIndex] = array[secondIndex];
4544
array[secondIndex] = temp;
46-
arrayVisualiser.repaint();
45+
46+
accessColourMod[firstIndex] = 100;
47+
accessColourMod[secondIndex] = 100;
48+
repaint();
4749
sleepFor(10000);
4850
}
51+
52+
/**
53+
* Gets the canvas size
54+
* @return size
55+
*/
56+
@Override
57+
public Dimension getPreferredSize() {
58+
return new Dimension(WIN_WIDTH, WIN_HEIGHT);
59+
}
60+
61+
public void resetColours() {
62+
for (int i = 0; i < NUM_BARS; i++) {
63+
accessColourMod[i] = 0;
64+
}
65+
repaint();
66+
}
67+
68+
/**
69+
* Draws the array
70+
* @param g The graphics device for drawing
71+
*/
72+
@Override
73+
public void paintComponent(Graphics g) {
74+
Graphics2D graphics = (Graphics2D)g;
75+
super.paintComponent(graphics);
76+
77+
graphics.setColor(Color.white);
78+
for (int x = 0; x < NUM_BARS; x++) {
79+
int height = getValue(x) * 3;
80+
int xBegin = x + (BAR_WIDTH - 1) * x;
81+
int yBegin = WIN_HEIGHT - height;
82+
83+
int val = accessColourMod[x] * 2;
84+
graphics.setColor(new Color(255, 255 - val, 255 - val));
85+
graphics.fillRect(xBegin, yBegin, BAR_WIDTH, height);
86+
if (accessColourMod[x] > 0) {
87+
accessColourMod[x]-= 5;
88+
}
89+
}
90+
}
4991
}
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package sortVisualiser;
22

3-
import java.awt.Dimension;
3+
import java.util.ArrayList;
44
import javax.swing.JFrame;
5+
import sortVisualiser.algorithms.BubbleSort;
6+
import sortVisualiser.algorithms.ISortAlgorithm;
7+
import sortVisualiser.algorithms.Shuffle;
8+
import static util.Sleep.sleepFor;
59

610
/**
711
* The main class for the sort visualiser GUI
812
* @author Matt Hopson
913
*/
1014
public class SortVisualiser {
1115
private JFrame window;
12-
private ArrayCanvas sortArray;
16+
private SortArray sortArray;
17+
18+
private ArrayList<ISortAlgorithm> sortQueue;
1319

1420
/**
1521
* Creates the GUI
@@ -18,15 +24,26 @@ public SortVisualiser() {
1824
window = new JFrame("Sort Visualiser");
1925
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2026

21-
sortArray = new ArrayCanvas();
27+
sortArray = new SortArray();
2228
window.add(sortArray);
2329
window.pack();
2430
window.setVisible(true);
2531

26-
sortArray.run();
32+
sortQueue = new ArrayList<>();
33+
sortQueue.add(new Shuffle());
34+
sortQueue.add(new BubbleSort());
35+
}
36+
37+
public void run() {
38+
for (ISortAlgorithm algorithm : sortQueue) {
39+
algorithm.runSort(sortArray);
40+
sortArray.resetColours();
41+
sleepFor(2000000);
42+
}
2743
}
2844

2945
public static void main(String... args) {
3046
SortVisualiser sortVisualiser = new SortVisualiser();
47+
sortVisualiser.run();
3148
}
3249
}

src/main/java/sortVisualiser/algorithms/BubbleSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class BubbleSort implements ISortAlgorithm
1010
{
1111
@Override
1212
public void runSort(SortArray array) {
13-
int len = array.size();
13+
int len = array.arraySize();
1414
for(int i = 0; i < len - 1; i++) {
1515
for (int j = 0; j < len - i - 1; j++) {
1616
if (array.getValue(j) > array.getValue(j + 1)) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sortVisualiser.algorithms;
2+
3+
import java.util.Random;
4+
import sortVisualiser.SortArray;
5+
6+
/**
7+
*
8+
* @author mhops
9+
*/
10+
public class Shuffle implements ISortAlgorithm
11+
{
12+
13+
@Override
14+
public void runSort(SortArray array) {
15+
Random rng = new Random();
16+
for (int i = 0; i < array.arraySize(); i++) {
17+
int swapWithIndex = rng.nextInt(array.arraySize() - 1);
18+
array.swapUpdate(i, swapWithIndex);
19+
}
20+
}
21+
22+
}

0 commit comments

Comments
 (0)