Newer
Older
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package no.uib.inf101.gridview;
import no.uib.inf101.colorgrid.CellColor;
import no.uib.inf101.colorgrid.CellPosition;
import no.uib.inf101.colorgrid.GridDimension;
import no.uib.inf101.colorgrid.IColorGrid;
import org.junit.jupiter.api.Test;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class TestCellPositionToPixelConverter {
@Test
public void illustratedSample() {
IColorGrid grid = newGridFromString(String.join("\n",
"R--B",
"----",
"Y--G")
);
CellPositionToPixelConverter converter = getConverter(
grid, new Rectangle2D.Double(30, 30, 340, 240), 30);
Rectangle2D expected = new Rectangle2D.Double(215, 130, 47.5, 40);
assertEquals(expected, converter.getBoundsForCell(new CellPosition(1, 2)));
}
/////////////////////////////
// Helper methods
/////////////////////////////
static CellPositionToPixelConverter getConverter(GridDimension grid, Rectangle2D rect, double margin) {
try {
Constructor<?> constructor = CellPositionToPixelConverter.class.getConstructor(
GridDimension.class, Rectangle2D.class, double.class
);
// Check that the constructor is public
assertTrue(Modifier.isPublic(constructor.getModifiers()),
"The constructor CellPositionToPixelConverter(IColorGrid, Rectangle2D, double)"
+ " should be public");
// Create a new object using the constructor and return it
return (CellPositionToPixelConverter) constructor.newInstance(grid, rect, margin);
} catch (NoSuchMethodException e) {
fail("Could not find the constructor CellPositionToPixelConverter(IColorGrid, Rectangle2D, " +
"double) in the CellPositionToPixelConverter class");
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
throw new IllegalStateException("Should not be possible to reach this point");
}
static IColorGrid newGridFromString(String stringGrid) {
return new IColorGrid() {
private String[] lines = stringGrid.split("\n");
@Override
public int rows() {
return this.lines.length;
}
@Override
public int cols() {
return this.lines[0].length();
}
@Override
public Color get(CellPosition pos) {
if (pos.row() < 0 || pos.row() >= this.rows()) {
throw new IllegalArgumentException("Row out of bounds");
}
if (pos.col() < 0 || pos.col() >= this.cols()) {
throw new IllegalArgumentException("Column out of bounds");
}
return switch (this.lines[pos.row()].charAt(pos.col())) {
case 'R' -> Color.RED;
case 'G' -> Color.GREEN;
case 'B' -> Color.BLUE;
case 'Y' -> Color.YELLOW;
default -> null;
};
}
@Override
public void set(CellPosition pos, Color color) {
// ignore
}
@Override
public java.util.List<CellColor> getCells() {
List<CellColor> a = new ArrayList<>();
for (int r = 0; r < this.rows(); r++) {
for (int c = 0; c < this.cols(); c++) {
Color color = this.get(new CellPosition(r, c));
a.add(new CellColor(new CellPosition(r, c), color));
}
}
return a;
}
};
}
}