이제 메서드를 구현할 차례다.
BufferedImage createImage(int width, int height, PixelFunction f) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
Color color = f.apply(x, y);
image.setRGB(x, y, color.getRGB());
}
return image;
}
이제 두 정수에 해당하는 색상 값을 만들어 주는 람다 표현식으로 이 함수를 호출하면 된다.
BufferedImage frenchFlag = createImage(150, 100,
(x, y) -> x < 50 ? Color.BLUE : x < 100 ? Color.WHITE : Color.RED);