Skip to content

Commit 266be6f

Browse files
committed
Created Bar Color (Red, Green, Blue)
1 parent 5a12edd commit 266be6f

10 files changed

Lines changed: 52 additions & 8 deletions

File tree

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/com/marcomsl/algorithmvisualization/AlgorithmFrame.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class AlgorithmFrame extends JFrame {
88
int size;
99

1010

11-
public AlgorithmFrame(int[] array){
11+
public AlgorithmFrame(int[] array, BarColor barColor){
1212

1313
size = array.length * 10 + 16;
1414

@@ -20,7 +20,7 @@ public AlgorithmFrame(int[] array){
2020
setBackground(Color.WHITE);
2121
setSize(size, size);
2222

23-
ValueBars valueBars = new ValueBars(array, size);
23+
ValueBars valueBars = new ValueBars(array, size, barColor);
2424
valueBars.setBounds(0,0,size, size);
2525
valueBars.setVisible(true);
2626
valueBars.setBackground(Color.WHITE);

src/com/marcomsl/algorithmvisualization/AlgorithmVisualizer.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ public class AlgorithmVisualizer {
1010

1111
private int[] array;
1212
private ArrayList<Integer> arrayList = new ArrayList<>();
13+
private BarColor barColor;
1314

1415
public AlgorithmVisualizer() {
1516
array = new int[50];
1617
fillArray();
17-
algorithmFrame = new AlgorithmFrame(array);
18+
algorithmFrame = new AlgorithmFrame(array, barColor);
19+
20+
}
21+
22+
public AlgorithmVisualizer(BarColor barColor) {
23+
this.barColor = barColor;
24+
array = new int[50];
25+
fillArray();
26+
algorithmFrame = new AlgorithmFrame(array, barColor);
1827

1928
}
2029

@@ -25,7 +34,21 @@ public AlgorithmVisualizer(int size) {
2534
array = new int[50];
2635
}
2736
fillArray();
28-
algorithmFrame = new AlgorithmFrame(array);
37+
algorithmFrame = new AlgorithmFrame(array, barColor);
38+
39+
}
40+
41+
public AlgorithmVisualizer(int size, BarColor barColor) {
42+
if (size >= 25 && size <= 50) {
43+
array = new int[size];
44+
} else {
45+
array = new int[50];
46+
}
47+
48+
this.barColor = barColor;
49+
50+
fillArray();
51+
algorithmFrame = new AlgorithmFrame(array, barColor);
2952

3053
}
3154

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.marcomsl.algorithmvisualization;
2+
3+
public enum BarColor {
4+
5+
RED, GREEN, BLUE;
6+
7+
}

src/com/marcomsl/algorithmvisualization/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Main {
66
// main function for testing
77
public static void main(String[] args) throws InterruptedException {
88

9-
AlgorithmVisualizer algorithmVisualizer = new AlgorithmVisualizer(50);
9+
AlgorithmVisualizer algorithmVisualizer = new AlgorithmVisualizer(50, BarColor.RED);
1010
algorithmVisualizer.selectionSort(100);
1111
algorithmVisualizer.resetArray();
1212
algorithmVisualizer.bubblesort(15);

src/com/marcomsl/algorithmvisualization/ValueBars.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ public class ValueBars extends JPanel {
77

88
int[] array;
99
int size;
10+
BarColor barColor;
1011

1112

12-
public ValueBars(int[] array, int size){
13+
public ValueBars(int[] array, int size, BarColor barColor){
1314

1415
this.array = array;
1516
this.size = size;
17+
this.barColor = barColor;
1618

1719
}
1820

@@ -28,8 +30,20 @@ protected void paintComponent(Graphics g){
2830
g.setColor(Color.WHITE);
2931

3032
for(int i = 0; i < array.length; i++){
31-
if(array[i] <= 255 && array[i] >= 0){
32-
g.setColor(new Color(0,array[i] / 2,0));
33+
if(array[i] / 2<= 255 && array[i] / 2 >= 0){
34+
35+
if(barColor == BarColor.GREEN){
36+
g.setColor(new Color(0,array[i] / 2,0));
37+
}else if(barColor == BarColor.RED){
38+
g.setColor(new Color(array[i] / 2,0,0));
39+
}else if(barColor == BarColor.BLUE){
40+
g.setColor(new Color(0,0,array[i] / 2));
41+
}else{
42+
g.setColor(Color.BLACK);
43+
}
44+
45+
}else{
46+
g.setColor(Color.BLACK);
3347
}
3448

3549
g.fillRect(x , size - 60 - array[i], 10, array[i] + 30);

0 commit comments

Comments
 (0)