package lab.art; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; public class Generator { public static void main(String[] args) { // create a (pseudo) random number generator Random random = new Random(System.currentTimeMillis()); // generate a random expression for red, green and blue Expression redExpression = Expressions.getRandom(random); System.out.printf("Red: %s%n", redExpression); Expression greenExpression = Expressions.getRandom(random); System.out.printf("Green: %s%n", greenExpression); Expression blueExpression = Expressions.getRandom(random); System.out.printf("Blue: %s%n", blueExpression); // create an "empty" image final int PIXELS = 800; BufferedImage image = new BufferedImage(PIXELS, PIXELS, BufferedImage.TYPE_INT_RGB); // set each pixel to an amount of red, green and blue based on the expressions final int MAX = 255; // maximal value of each color for (int xCoordinate = 0; xCoordinate < PIXELS; xCoordinate++) { for (int yCoordinate = 0; yCoordinate < PIXELS; yCoordinate++) { // map [0, PIXELS) to [-1, 1] double x = 2 * xCoordinate / (double) PIXELS - 1; double y = 2 * yCoordinate / (double) PIXELS - 1; // evaluate red, green and blue double redValue = redExpression.evaluate(x, y); double greenValue = greenExpression.evaluate(x, y); double blueValue = blueExpression.evaluate(x, y); // map [-1, 1] to [0, MAX] int red = (int) (MAX * (1 + redValue) / 2.0); int green = (int) (MAX * (1 + greenValue) / 2.0); int blue = (int) (MAX * (1 + blueValue) / 2.0); // set the color of the pixel Color color = new Color(red, green, blue); image.setRGB(xCoordinate, yCoordinate, color.getRGB()); } } // write the image to file File file = new File("Art.jpg"); try { ImageIO.write(image, "JPEG", file); System.out.println("Image generated"); } catch (IOException e) { System.out.println("Image generation failed"); } } }