// An example of image writing in Java // // Create a new png image, and save it // // REF (BufferedImage Class API): // http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html // // M. Branicky, 10/11/06 import java.awt.image.*; import javax.imageio.*; import java.io.*; public class NewImage2 { // 0xNUM is how to put a HEXadecimal (base 16: digits 0-F) constant into Java // See http://cloford.com/resources/colours/500col.htm for HEX color codes public static final int BLACK = 0, WHITE = 0xFFFFFF; public static final int BLUE = 0xFF, GREEN = 0xFF00, RED=0xFF0000; public static final int CYAN = GREEN+BLUE, MAGENTA = RED+BLUE, YELLOW = RED+GREEN; // Converts R-G-B values to a single integer // See http://www.tayloredmktg.com/rgb/ for RGB color codes public static int RGBint (int R, int G, int B) { return (R*256*256+G*256+B); } public static void main (String args[]) throws IOException { // create a new, "blank" 500 by 300 image BufferedImage myimage = new BufferedImage(500,300,BufferedImage.TYPE_INT_RGB); // See http://www.tayloredmktg.com/rgb/ for RGB color codes int goldenrod = RGBint(184, 134, 11); int thistle = RGBint(216, 191, 216); // set the values for the image for (int i=0; i<500; i++) { for (int j=0; j<300; j++) { if (i<50) myimage.setRGB(i,j,BLACK); else if (i<100) myimage.setRGB(i,j,WHITE); else if (i<150) myimage.setRGB(i,j,RED); else if (i<200) myimage.setRGB(i,j,GREEN); else if (i<250) myimage.setRGB(i,j,BLUE); else if (i<300) myimage.setRGB(i,j,CYAN); else if (i<350) myimage.setRGB(i,j,MAGENTA); else if (i<400) myimage.setRGB(i,j,YELLOW); else if (i<450) myimage.setRGB(i,j,goldenrod); else myimage.setRGB(i,j,thistle); } } // store the image in a file File outImage = new File("myimage2.jpg"); ImageIO.write(myimage, "jpg", outImage); } }