-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColouredInfoTile.java
More file actions
56 lines (45 loc) · 1.76 KB
/
ColouredInfoTile.java
File metadata and controls
56 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class ColouredInfoTile extends InfoTile{
public ColouredInfoTile(int x, int y) {
super(x, y);
}
// Calculates what text should be written in the InfoTile to make it into a puzzle. This basically means running a Run Length Encoding algorithm
// on the slice(could be horizontal or vertical) Colours and ignoring one colour, so that the image is not fully defined making the puzzle trivial.
@Override
public void calculateConstraints() {
constraintSlice = ParsedImage.getColourSlice(getXCoord(), getYCoord());
if (constraintSlice.length == 0) {
return;
}
ArrayList<Integer> result = runLengthEncoding();
for(int i = 0; i < result.size(); i += 2) {
if(result.get(i+1) == ParsedImage.ignoredColour) {
continue;
}
JLabel label = new JLabel(String.valueOf(result.get(i)));
label.setForeground(new Color(result.get(i+1)));
this.add(label);
}
}
// Returns an integer array that contains pairs of Color.getRGB() and the colours frequency
@Override
public ArrayList<Integer> runLengthEncoding() {
ArrayList<Integer> result = new ArrayList<>();
int counter = 0;
int lastValue = constraintSlice[0];
for(int i = 1; i < constraintSlice.length; i++) {
counter++;
if (lastValue != constraintSlice[i]) {
result.add(counter);
result.add(constraintSlice[i-1]);
counter = 0;
lastValue = constraintSlice[i];
}
}
result.add(counter+1);
result.add(lastValue);
return result;
}
}