1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
}
}
}
}
|