import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; /** 2nd flag program. Let's create the flag of Poland. * White on top, red on bottom. */ public class Flag2 { public static void main(String [] args) { // First, create an image object. // The parameters to the constructor are: // width, height, and image type (we use 1) BufferedImage image = new BufferedImage (300,200,1); // Second, write pixel values into the image. // I think it's easier to think in terms of x/y, not row/column! // For each x value (and there are 300 of those): // 0 <= y < 100 are white, and 100 <= y < 200 are red. for (int x = 0; x < 300; ++x) { int blueNumber = 0*65536 + 0*256 + 255; int whiteNumber = 255*65536 + 255*256 + 255; int redNumber = 255*65536 + 0*256 + 0; // white for (int y = 0; y < 100; ++y) image.setRGB(x, y, whiteNumber); // red for (int y = 100; y < 200; ++y) image.setRGB(x, y, redNumber); } // Third, write the image to a file. // May automatically throw "IOException" try { ImageIO.write(image, "png", new File ("poland.png")); } catch (IOException e) { System.out.printf("Encountered problem trying to write to file\n"); } } }