//*
// * Theresa.java
// */

import java.lang.Math.*;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Theresa extends Frame
{
	private java.awt.Image image;

	// set the boundaries of the image
	int x = 0; 		// |- bottom left corner
	int y = 0; 		// |

	int w = 220;	// |- top right corner
	int h = 268;	// |

	int GradientPerPixel[][]=new int[w][h]; // 2D array to store the pixels according to their coordinates
	int[] pixels = new int[w*h]; // creating an array to store the pixels

	int gray;

	int pixel;



	// just copied from the PixelGrabber sheet
	public void handlesinglepixel(int x, int y, int pixel)
	{
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >> 8)  & 0xff;
		int blue  = (pixel)       & 0xff;
		gray  = (red+green+blue) / 3;

//		int z = 0;
//		for(int a=0; a<h; a++)				//a = h = height of the image
//		{
//			for(int b=0; b<w; b++)			//b = w = width of the image
//			{
//				pixel = pixels[z];
//				GradientPerPixel[b][a] = gray;
//
//				z=z+1;// int c makes sure all the pixels stored in pixels[x] are transferred to the GradientPerPixel-array
//			}
//		}//end for-loop
	}

	public void handlepixels(java.awt.Image image, int x, int y, int w, int h)
	{
		System.out.println("handling pixels..");
		PixelGrabber pg = new PixelGrabber(image, x, y, w, h, pixels, 0, w); 	// grabs pixels and puts them into one long array

		try
		{
			pg.grabPixels();													// try to grab the pixels as set above
		}
		catch(InterruptedException e)
		{
			System.err.println("interrupted waiting for pixels!");
			return;
		}
		if ((pg.getStatus() & ImageObserver.ABORT) !=0)
		{
			System.err.println("image fetch aborted or errored");
		}

		int z = 0;
		for (int j = 0; j < h; j++)
		{
			for (int i = 0; i < w; i++)
			{
				handlesinglepixel(i, j, pixels[z]);
				GradientPerPixel[i][j]=gray/30;
				z=z+1;
		    }
		}

	}// end void handlepixels






	public Theresa(String fileName)
	{
		// loading picture -------- WORKING---------------------------------
		fileName="EMUU212.jpg";
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		image = toolkit.getImage(fileName);
		MediaTracker mediaTracker = new MediaTracker(this);
		mediaTracker.addImage(image, 0);
		try
		{
			mediaTracker.waitForID(0);
		}
		catch (InterruptedException ie)
		{
			System.err.println(ie);
			System.exit(1);
		}

		addWindowListener(new WindowAdapter()
		{
      		public void windowClosing(WindowEvent e)
      		{
        		System.exit(0);
      		}
		});

		setSize(image.getWidth(null), image.getHeight(null));
		setTitle(fileName);
		show();
		//------------------------------------------------------------------


		handlepixels(image, 0,0,220,268);


		//printing coordinates of all the pixels + its info (ARGB)-----------------------------------------------------------------------------------
		int c = 0;
		for(int a=0; a<h; a++)				//a = h = height of the image
		{
			for(int b=0; b<w; b++)			//b = w = width of the image
			{

				System.out.println(b+" " + a+" "+GradientPerPixel[b][a]);

				c=c+1;// int c makes sure all the pixels stored in pixels[x] are transferred to the GradientPerPixel-array
			}
		}//end for-loop

		//-------------------------------------------------------------------------------------------------------------------------------------------





		// I text - creating a new A0 document
		Document document = new Document(PageSize.A0); // (A0 Size: ;2384;3370;)
		System.out.println("PDF is being printed");

		try
		{
			// I text - open the pdf-document so it can be edited
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("EMUU212.pdf"));
			document.open();
			PdfContentByte cb = writer.getDirectContent();

			// Black background
			cb.setCMYKColorFillF(75f,68f,67f,90f);
			cb.rectangle(0,0,2384,3370);
			cb.fill();

			for(int yScan=0; yScan<h; yScan++)
			{
				for(int xScan=0; xScan<w; xScan++)
				{
					cb.setCMYKColorFillF(0f, 0f, 0f, 0f);                           // Filled white circles
					cb.circle(96+(xScan*15), ((((h+35)-yScan)*15)), GradientPerPixel[xScan][yScan]);
					cb.fill();
				}
			}
		}
		catch (Exception de)
		{
			de.printStackTrace();						// Exception catch
		}

		// Close the pdf-document so it can be viewed
    	document.close();

	}//end of public Theresa



	public void paint(Graphics graphics)
	{
		graphics.drawImage(image, 0, 0, null);
	}

	public static void main(String[] args)
	{
		args = new String[1];
		new Theresa(args[0]);
	}


}//end public class Theresa extends Frame
