import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; /** Czech Republic - this time we need slanted lines. */ public class Flag3 { 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. // For values x from 0 to 149: // Use white for y = 0 to (2/3)x. // Use blue for y = (2/3)x to 200-(2/3)x // Use red for y = 200-(2/3) to 199. // For values of x from 150 to 299: // Use white for y = 0 to 99. // Use red for y = 100 to 199. // I should take the color definitions out of the loop. int blueNumber = 0*65536 + 0*256 + 255; int whiteNumber = 255*65536 + 255*256 + 255; int redNumber = 255*65536 + 0*256 + 0; for (int x = 0; x < 150; ++x) { // white for (int y = 0; y < 2*x/3; ++y) image.setRGB(x, y, whiteNumber); // blue for (int y = 2*x/3; y < 200 - 2*x/3; ++y) image.setRGB(x, y, blueNumber); // red for (int y = 200 - 2*x/3; y < 200; ++y) image.setRGB(x, y, redNumber); } for (int x = 150; x < 300; ++x) { // 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 ("czech.png")); } catch (IOException e) { System.out.printf("Encountered problem trying to write to file\n"); } } }