import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class GenerateImage extends Frame {
	private BufferedImage image1;
	private BufferedImage image2;
	private int threshold = 200;
	private Color on = Color.white;
	private Color off = Color.black;
	private int width = 50;
	private int height = 50;
	private int inputWidth = 25;
	private int inputHeight = 25;
	
	private static String newline = System.getProperty("line.separator");

	public void paint(Graphics g) {
		if (image1 != null)
			g.drawImage(image1, 10, 30, this);
			g.drawImage(image2, width + 20, 30, this);
	}

	public void update(Graphics g) {
		paint(g);
	}

	class WindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
		
	public GenerateImage() {
		readImage();
		addWindowListener(new WindowListener());
		setTitle("CreateImage");
		
		image1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		int color = 0;
		int colorx = 0;
		int colory = 0;
		for(int loop1 = 0; loop1 < height; loop1++) {
			for(int loop2 = 0; loop2 < width; loop2++) {
				color = (color +1)%128;
				int [] pixel = {color, color, color};
				image1.getRaster().setPixel(loop2, loop1, pixel);
			}
		}
		setSize(image1.getWidth(this) + image2.getWidth(this)+ 30, image1.getHeight(this) + image2.getHeight(this)+ 40);
		setVisible(true);
		setBackground(Color.green);
		
		try {
			FileWriter fileout = new FileWriter("imageTest.txt");
			for(int loop1 = 0; loop1 < image1.getHeight(); loop1++) {
				for(int loop2 = 0; loop2 < image1.getWidth(); loop2++) {
					Color pixel = new Color(image1.getRGB(loop2,loop1));
					int gray = (pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3;
					String s = Integer.toHexString(gray);
					if(s.length() == 1) {
						s = "0"+s;
					}
					s = s +" "+ s;
					fileout.write(s+" ");
					fileout.write(newline);
				}	
			}
			fileout.flush();
			}catch (IOException ioe) {
				System.out.println(ioe.getMessage());
			}
		while(true) {
				repaint();
			try {
				Thread.sleep(1);
			} catch(Exception e) {
				System.out.println("Doh");
			}
		}
	}

	public BufferedImage readImage() {
		try{	
			BufferedReader in = new BufferedReader(new FileReader("resizeImage.txt"));
			image2 = new BufferedImage(inputWidth, inputHeight, BufferedImage.TYPE_INT_RGB);
			for(int loop1 = 0; loop1 < inputHeight; loop1++) {
				for(int loop2 = 0; loop2 < inputWidth; loop2++) {
					String text = in.readLine().substring(0,2);
					int hex = Integer.parseInt(text, 16);
					int [] pixel = {hex, hex, hex};
					image2.getRaster().setPixel(loop2, loop1, pixel);
				}
			}
			in.close();
			return image2;
		}catch (IOException ioe) {
			System.out.println(ioe.getMessage());
			return null;
		}
	}
	
	public static void main(String[] args) {
			new GenerateImage();
	}
}
