import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import sun.awt.image.codec.JPEGImageEncoderImpl;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageMaker {
public BufferedImage createImage(int width, int height) throws IOException{
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Random r = new Random();
r.setSeed(new Date().getTime());
Graphics g = bi.getGraphics();
for (int i=0; i<bi.getWidth(); i++){
for (int j=0; j<bi.getHeight(); j++){
int red = Math.abs(r.nextInt()%256);
int green = Math.abs(r.nextInt()%256);
int blue = Math.abs(r.nextInt()%256);
Color c = new Color(red, green, blue);
g.setColor(c);
g.drawRect(i, j, 0, 0);
}
System.out.println((i+1) + " of 1024 lines done");
}
bi.flush();
return bi;
}
public static void main(String[] args) throws IOException {
final String FILENAME = "/Users/bfo/Desktop/random.jpg";
ImageMaker im = new ImageMaker();
BufferedImage bi = im.createImage(1024, 768);
FileOutputStream fos = new FileOutputStream(FILENAME);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder e = new JPEGImageEncoderImpl(bos);
e.encode(bi);
bos.flush();
bos.close();
System.out.println("OK, Image saved to " + FILENAME);
}
}