import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; /** Can we try the UK (Union jack) ? */ public class Flag4 { public static void main(String [] args) { // First, create an image object. // We need 600 by 300. BufferedImage image = new BufferedImage (600,300,1); // Second, write pixel values into the image. // 1. paint everything blue. // 2. fill in diagonals. (hardest part) // 3. fill in horizontal and vertical stripes. // Note that it's ok to overwrite pixel values like this. int blueNumber = 0*65536 + 0*256 + 140; int whiteNumber = 255*65536 + 255*256 + 255; int redNumber = 255*65536 + 0*256 + 0; // everything blue for (int x = 0; x < 600; ++x) for (int y = 0; y < 300; ++y) image.setRGB(x, y, blueNumber); // Diagonals! // Left half of flag: (values of x from 0 to 299) // white from y = (1/2)x - 30 to y = (1/2)x: beware bounds! // red from y = (1/2)x to y = (1/2)x + 20 // white from y = (1/2)x + 20 to y = (1/2)x + 30 // // white from y = (-1/2)x + 270 to y = (-1/2)x + 300 // red from y = (-1/2)x + 300 to y = (-1/2)x + 320): beware bounds // white from y = (-1/2)x + 320 to y = (-1/2)x + 330: beware bounds // // Right half of flag: (values of x from 300 to 599) // white from y = (-1/2)x + 270 to y = (-1/2)x + 280: beware bounds // red from y = (-1/2)x + 280 to y = (-1/2)x + 300: beware bounds // white from y = (-1/2)x + 300 to y = (-1/2)x + 330 // // white from y = (1/2)x - 30 to y = (1/2)x - 20 // red from y = (1/2)x - 20 to y = (1/2)x // white from y = (1/2)x to y = (1/2)x + 30: beware bounds for (int x = 0; x < 300; ++x) { for (int y = x/2 - 30; y < x/2; ++y) { if (y < 0) continue; image.setRGB(x, y, whiteNumber); } for (int y = x/2; y < x/2 + 20; ++y) image.setRGB(x, y, redNumber); for (int y = x/2 + 20; y < x/2 + 30; ++y) image.setRGB(x, y, whiteNumber); for (int y = -x/2 + 270; y < -x/2 + 300; ++y) image.setRGB(x, y, whiteNumber); for (int y = -x/2 + 300; y < -x/2 + 320; ++y) { if (y >= 300) continue; image.setRGB(x, y, redNumber); } for (int y = -x/2 + 320; y < -x/2 + 330; ++y) { if (y >= 300) continue; image.setRGB(x, y, whiteNumber); } } // right half of flag for (int x = 300; x < 600; ++x) { for (int y = -x/2 + 270; y < -x/2 + 280; ++y) { if (y < 0) continue; image.setRGB(x, y, whiteNumber); } for (int y = -x/2 + 280; y < -x/2 + 300; ++y) { if (y < 0) continue; image.setRGB(x, y, redNumber); } for (int y = -x/2 + 300; y < -x/2 + 330; ++y) image.setRGB(x, y, whiteNumber); for (int y = x/2 - 30; y < x/2 - 20; ++y) image.setRGB(x, y, whiteNumber); for (int y = x/2 - 20; y < x/2; ++y) image.setRGB(x, y, redNumber); for (int y = x/2; y < x/2 + 30; ++y) { if (y >= 300) continue; image.setRGB(x, y, whiteNumber); } } // Horizontal and vertical // So far, so good! This is what we need to finish. :) // White horizontal stripe first: for (int x = 0; x < 600; ++x) for (int y = 100; y < 200; ++y) image.setRGB(x, y, whiteNumber); // white vertical for (int x = 250; x < 350; ++x) for (int y = 0; y < 300; ++y) image.setRGB(x, y, whiteNumber); // red horizontal for (int x = 0; x < 600; ++x) for (int y = 120; y < 180; ++y) image.setRGB(x, y, redNumber); // red vertical for (int x = 270; x < 330; ++x) for (int y = 0; y < 300; ++y) image.setRGB(x, y, redNumber); // Third, write the image to a file. // May automatically throw "IOException" try { ImageIO.write(image, "png", new File ("uk.png")); } catch (IOException e) { System.out.printf("Encountered problem trying to write to file\n"); } } }