import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;

public class ImagePosterizer
{
  public static final int IMAGE_WIDTH       = 4500;
  public static final int IMAGE_HEIGHT      = 6000;
  public static final int BORDER_THICKNESS  =   50;
  public static void main(String args[]) throws Exception
  {
    new ImagePosterizer(args[0], args[1], args[2]);
  }
  public ImagePosterizer(String imageName, String name, String outputName) throws Exception
  {
    BufferedImage image;
    Graphics2D graphics;

    System.out.printf("Creating new image... "); System.out.flush();
    image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
    System.out.printf("done.\n"); System.out.flush();

    System.out.printf("Getting graphics... "); System.out.flush();
    graphics = (Graphics2D)image.getGraphics();
    System.out.printf("done.\n"); System.out.flush();

    graphics.setColor(Color.BLACK);
    System.out.printf("Setting image to black... "); System.out.flush();
    graphics.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
    System.out.printf("done.\n"); System.out.flush();

    System.out.printf("Drawing border part... "); System.out.flush();
    graphics.setColor(new Color(0xFFEEDA00));
    for (int i = 0; i < BORDER_THICKNESS; ++i)
    {
      graphics.drawRect(IMAGE_WIDTH   / 10 - BORDER_THICKNESS + i,
                        IMAGE_HEIGHT  / 10 - BORDER_THICKNESS - 300 + i,
                        IMAGE_WIDTH   * 8 / 10 + BORDER_THICKNESS + 50 - i * 2,
                        IMAGE_HEIGHT  * 8 / 10 + BORDER_THICKNESS * 2 - i * 2);
    }
    System.out.printf("done.\n"); System.out.flush();
/*
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String fontFamilyNames[] = env.getAvailableFontFamilyNames();
		for (String s : fontFamilyNames)
		{
		  System.err.printf("%s\n", s);
		}
		System.exit(0);*/

    System.out.printf("Drawing text (%s)... ", name); System.out.flush();
    graphics.setFont(new Font("Palatino Linotype", Font.BOLD, 400));
    FontMetrics fm = graphics.getFontMetrics();
    int width = fm.stringWidth(name);
    graphics.drawString(name,
                        (IMAGE_WIDTH - width) / 2,
                        5700);
    System.out.printf("done.\n"); System.out.flush();

    System.out.printf("Reading image file (%s)... ", imageName); System.out.flush();
    BufferedImage picture = ImageIO.read(new File(imageName));
    System.out.printf("done.\n"); System.out.flush();

    System.out.printf("Drawing image in border... "); System.out.flush();
    graphics.drawImage(picture,
                       IMAGE_WIDTH / 10 + BORDER_THICKNESS,
                       IMAGE_HEIGHT / 10 - 300 + BORDER_THICKNESS,
                       IMAGE_WIDTH * 8 / 10 - BORDER_THICKNESS * 2,
                       IMAGE_HEIGHT * 8 / 10 - BORDER_THICKNESS * 2,
                       null);
    System.out.printf("done.\n"); System.out.flush();

    System.out.printf("Saving image... "); System.out.flush();
    ImageIO.write(image, "png", new File(outputName));
    System.out.printf("done.\n"); System.out.flush();

  }
  public void drawHorizontalLine(int pixels[], int pixelColor, int x0, int x1, int y)
  {
    Arrays.fill(pixels, y * IMAGE_WIDTH + x0, y * IMAGE_WIDTH + x1, pixelColor);
  }
  public void drawVerticalLine(int pixels[], int pixelColor, int x, int y0, int y1)
  {
    for (int y = y0; y <= y1; ++y)
    {
      pixels[y * IMAGE_WIDTH + x] = pixelColor;
    }
  }
  public void drawRectangle(int pixels[], int pixelColor, int x, int y, int w, int h, int thickness)
  {
    for (int i = 0; i < thickness; ++i) drawHorizontalLine(pixels,  pixelColor, x, x + w - 1, y + i);
    for (int i = 0; i < thickness; ++i) drawHorizontalLine(pixels,  pixelColor, x, x + w - 1, y + h - i - 1);
    for (int i = 0; i < thickness; ++i) drawVerticalLine(pixels,    pixelColor, x + i, y, y + h - i - 1);
    for (int i = 0; i < thickness; ++i) drawVerticalLine(pixels,    pixelColor, x + w - i - 1, y, y + h - i - 1);
  }
}

