我正在实施种子填充算法。对于颜色,我使用通用变量。但是我不知道如何实现patt
整数模式。这是源代码:
public class SeedFill4In<PixelType> implements SeedFill<PixelType> {
@Override
public @NotNull RasterImage<PixelType> fill(
final @NotNull RasterImage<PixelType> img, final int c, final int r,
final @NotNull PixelType areaValue,
final @NotNull PixelType newValue) {
int [ ][ ] patt = {
{0xFF0000,0x0000FF,0x0000FF,0xFF0000,0xFF0000},
{0xFF0000,0x0000FF,0x00FF00,0xFF0000,0xFF0000},
{0xFF0000,0x0000FF,0x0000FF,0x00FF00,0xFF0000},
{0xFF0000,0x0000FF,0x0000FF,0xFF0000,0xFF0000},
{0xFF0000,0x0000FF,0x0000FF,0xFF0000,0xFF0000}
};
int i = c % patt.length;
int j = r % patt.length;
return new Object() {
@NotNull RasterImage<PixelType> fill(
final @NotNull RasterImage<PixelType> img, final int c, final int r) {
return img.getPixel(c, r)
.flatMap((final @NotNull PixelType actualValue) -> {
if (actualValue.equals(areaValue)) {
return Optional.of(
fill(
fill(
fill(
fill(
img.withPixel(c, r, newValue),
c, r - 1),
c + 1, r),
c, r + 1),
c - 1, r)
);
}
return Optional.empty();
})
.orElse(img);
}
}.fill(img, c, r);
}
对于没有模式的填充,我使用了newValue
变量。如何将其更改为模式?