From 7a98f8d9761de8061178ec7bce79bca7cb48aade Mon Sep 17 00:00:00 2001 From: Dominic Matarese Date: Fri, 8 Oct 2021 23:53:42 +0000 Subject: Created git repo, added README.md --- randomimages.java | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 randomimages.java (limited to 'randomimages.java') diff --git a/randomimages.java b/randomimages.java new file mode 100644 index 0000000..72baffe --- /dev/null +++ b/randomimages.java @@ -0,0 +1,75 @@ +//generate a specified number of random images and place them in the +//directory of the source file. +//It is very unlikely something of meaning will be generated, but +//this is why the program can generate hundreds of them... + +//By Dominic Matarese + +import java.awt.*; +import java.io.*; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.util.Scanner; + +public class randomimages{ + public static void main(String[] args) + { + + //int defaultWidth = 720, defaultHeight = 480, defaultNum = 5; + int width = 720, height = 480, num = 5; + + + Scanner reader = new Scanner(System.in); // Reading from System.in + System.out.println("How many images do you want to generate?"); + System.out.println("Enter a number: "); + num = reader.nextInt(); // Scans the next token of the input as an int. + + System.out.println(""); + System.out.println("What will be the resolution of the images? \n(enter both width and height)"); + System.out.println("Enter width in # of pixels: "); + width = reader.nextInt(); + System.out.println("Enter height in # of pixels: "); + height = reader.nextInt(); + reader.close(); + + generateImages(width, height, num); + + if(num == 1) { + System.out.println("The image has been generated!"); + } + else { + System.out.println(num + "images have been generated!"); + } + return; + + } + public static void generateImages(int width, int height, int num) + { + for(int i = 1; i < num + 1; i++) + { + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + + //generate random RGB values for each pixel + for(int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + + int r = (int)(Math.random()*256); + int g = (int)(Math.random()*256); + int b = (int)(Math.random()*256); + + Color color = new Color(r, g, b); + image.setRGB(x, y, color.getRGB()); + } + } + + try { + File random = new File("Random" + i + ".png"); + ImageIO.write(image, "png", random); + } + catch (IOException exception){ + System.out.println("Error: IOException occured!"); + return; //prints error if IOException occurs + } + } + } +} -- cgit v1.2.1