November 3, 2013

Create a Random Image in Java Pixel by Pixel

Hey check it out how to create a random image in Java pixel by pixel:

        int width = 500;
        int height = 500;

        try {
            Random rand = new Random();
            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    // Color clc = new Color(rand.nextInt(255), 21, 60);
                    Color clc = new Color(rand.nextInt(255), 21, 60);
                    int rgb = clc.getRGB();
                    img.setRGB(i, j, rgb);
                }
            }
            // retrieve image
            File outputfile = new File("saved.jpg");
            ImageIO.write(img, "jpg", outputfile);
        } catch (IOException e) {
        }

No comments:

Post a Comment