Given:
@Entity
public class Pixel {
@Id
Point position;
String color;
I store pixels like this:
for (int row = 0; row < 20; row++) {
for (int col = 0; col < 20; col++) {
repository.save(new Pixel(new Point(row, col), "w3-flat-turquoise"));
}
}
This represents a square of 20x20
Now I need to display that list using layouts.
I can display easily a square like this (working):
Now I would like to retrieve the pixels from the database and display them... but I can't find a proper way.
So far, I have this (not working because final 'i' can't be change):
VerticalLayout lines = new VerticalLayout();
HorizontalLayout line;
int width = 20;
List<Pixel> pixels = pixelRepository.findAll();
for(final int i=0;i<width;++i) {
line = new HorizontalLayout();
Stream<Pixel> pixelLine = pixels.stream().filter(pixel -> pixel.getPosition().x == i);
for(int j=0;j<width;++j) {
addNewColorButton(line, pixelLine.filter(pixel -> pixel.getPosition().y == j).findFirst().get().color);
}
lines.add(line);
}
add(lines);
What would be a proper algorithm to do it ?
Working solution with final variables:
VerticalLayout lines = new VerticalLayout();
HorizontalLayout line;
int width = 20;
List<Pixel> pixels = pixelRepository.findAll();
for(int i=0;i<width;++i) {
int finalI = i;
line = new HorizontalLayout();
Supplier<Stream<Pixel>> pixelLine = () -> pixels.stream().filter(pixel -> pixel.getPosition().x == finalI);
for(int j=0;j<width;++j) {
int finalJ = j;
addNewColorButton(line, pixelLine.get().filter(pixel -> pixel.getPosition().y == finalJ).findFirst().get().color);
}
lines.add(line);
}
add(lines);