package segmentation;

import GUI.FileChooser;
import GUI.internalFrame;
import GUI.mainWindow;
import ij.*;
import ij.gui.NewImage;
import ij.process.ImageProcessor;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import GUI.Result;

public class Line4 {
	
	ImagePlus imp, impOtsu, imp4;
	ImageProcessor ip, nip, nip2, nip3;
	int constant = 40;
	int constant2 = 400;
	final int scale = 20;
	int numberResult;
	String journey = "";
	ArrayList oldJourneys = new ArrayList();
	boolean draw;
	boolean setSelected;
	
	public Line4(ImagePlus imp, ImageProcessor ip, ImagePlus impOtsu, int numberResult, boolean draw, boolean setSelected) {
		this.imp = imp;
		this.ip = ip;
		this.impOtsu = impOtsu;
		this.numberResult = numberResult;
		this.draw = draw;
		this.setSelected = setSelected;
	}
	
	class lines{
		ArrayList min;
		ArrayList max;
	}
	
	private boolean getJourney(int x1, int x2, int y1, int y2, int i, int j, ArrayList aI, ArrayList aJ, ImageProcessor nip3, String journey){
		String newJourney = "";
		
		for (int k = 0; k < aI.size(); k++) {
			//System.out.println("aI "+aI.get(k));
			//System.out.println("aJ "+aJ.get(k));
		}
			if ( (i == y2) && (j == x2) )
				return true;  
			
			if (i != y1) {
				//System.out.println("(i != y1)");
				if ((nip3.getPixelValue(j, i-1)) == 255) {
					newJourney = journey.concat((i-1)+" "+j);
					boolean journeyYetWas = false;
					for (int k = 0; k < oldJourneys.size(); k++) {
						if (((String)(oldJourneys.get(k))).compareTo(newJourney) == 0)
							journeyYetWas = true;
					}
					if (!journeyYetWas) {
						//System.out.println("j, i-1");
						aI.add(i-1);
						aJ.add(j);
						journey = journey.concat((i-1)+" "+j);
						for (int k = 0; k < aI.size(); k++) {
							//System.out.println("aI "+aI.get(k));
							//System.out.println("aJ "+aJ.get(k));
						}
						oldJourneys.add(journey);
						getJourney(x1, x2, y1, y2, i-1, j, aI, aJ, nip3, journey);
					}
				} 
			}
			if ((i!=y1) && (j!= x2)) {
				//System.out.println("((i!=y1) && (j!= x2))");
				if ((nip3.getPixelValue(j+1, i-1)) == 255) {
					newJourney = journey.concat((i-1)+" "+(j+1));
					boolean journeyYetWas = false;
					for (int k = 0; k < oldJourneys.size(); k++) {
						if (((String)(oldJourneys.get(k))).compareTo(newJourney) == 0)
							journeyYetWas = true;
					}
					if (!journeyYetWas) {

						//System.out.println("j+1, i-1");
						aI.add(i-1);
						aJ.add(j+1);
						journey = journey.concat((i-1)+" "+(j+1));
						for (int k = 0; k < aI.size(); k++) {
							//System.out.println("aI "+aI.get(k));
							//System.out.println("aJ "+aJ.get(k));
						}
						oldJourneys.add(journey);
						getJourney(x1, x2, y1, y2, i-1, j+1, aI, aJ, nip3, journey);
					}
				}
			}
			if (j!= x2) {
				//System.out.println("(j!= x2)");
				if ((nip3.getPixelValue(j+1, i)) == 255) {
					newJourney = journey.concat((i)+" "+(j+1));
					boolean journeyYetWas = false;
					for (int k = 0; k < oldJourneys.size(); k++) {
						if (((String)(oldJourneys.get(k))).compareTo(newJourney) == 0)
							journeyYetWas = true;
					}
					if (!journeyYetWas) {

						//System.out.println("j+1, i");
						aI.add(i);
						aJ.add(j+1);
						journey = journey.concat(i+" "+(j+1));
						for (int k = 0; k < aI.size(); k++) {
							//System.out.println("aI "+aI.get(k));
							//System.out.println("aJ "+aJ.get(k));
						}
						getJourney(x1, x2, y1, y2, i, j+1, aI, aJ, nip3, journey);
					}
				}
			}
			if ((i!=y2) && (j!= x2)) {
				//System.out.println("((i!=y2) && (j!= x2))");
				if ((nip3.getPixelValue(j+1, i+1)) == 255) {
					//System.out.println("j+1, i+1");
					aI.add(i+1);
					aJ.add(j+1);
					journey = journey.concat((i+1)+" "+(j+1));
					for (int k = 0; k < aI.size(); k++) {
						//System.out.println("aI "+aI.get(k));
						//System.out.println("aJ "+aJ.get(k));
					}
					getJourney(x1, x2, y1, y2, i+1, j+1, aI, aJ, nip3, journey);
				}
			}
			if (i!=y2) {
				//System.out.println("(i!=y2)");
				if ((nip3.getPixelValue(j, i+1)) == 255) {
					//System.out.println("j, i+1");
					aI.add(i+1);
					aJ.add(j);
					journey = journey.concat((i+1)+" "+j);
					for (int k = 0; k < aI.size(); k++) {
						//System.out.println("aI "+aI.get(k));
						//System.out.println("aJ "+aJ.get(k));
					}
					getJourney(x1, x2, y1, y2, i+1, j, aI, aJ, nip3, journey);
				}
			}
			if ((i!=y2) && (j!= x1)) {
				//System.out.println("((i!=y2) && (j!= x1))");
				if ((nip3.getPixelValue(j-1, i+1)) == 255) {
					//System.out.println("j-1, i+1");
					aI.add(i+1);
					aJ.add(j-1);
					journey = journey.concat((i+1)+" "+(j-1));
					for (int k = 0; k < aI.size(); k++) {
						//System.out.println("aI "+aI.get(k));
						//System.out.println("aJ "+aJ.get(k));
					}
					getJourney(x1, x2, y1, y2, i+1, j-1, aI, aJ, nip3, journey);
				}
			}
			if (j!=x1) {
				//System.out.println("(j!=x1)");
				if ((nip3.getPixelValue(j-1, i)) == 255) {
					//System.out.println("j-1, i");
					aI.add(i);
					aJ.add(j-1);
					journey = journey.concat(i+" "+(j-1));
					for (int k = 0; k < aI.size(); k++) {
						//System.out.println("aI "+aI.get(k));
						//System.out.println("aJ "+aJ.get(k));
					}
					getJourney(x1, x2, y1, y2, i, j-1, aI, aJ, nip3, journey);
				}
			}
			if ((i!=y1) && (j!=x1)) {
				//System.out.println("((i!=y1) && (j!=x1))");
				if ((nip3.getPixelValue(j-1, i-1)) == 255) {
					//System.out.println("j-1, i-1");
					aI.add(i-1);
					aJ.add(j-1);
					journey = journey.concat((i-1)+" "+(j-1));
					for (int k = 0; k < aI.size(); k++) {
						//System.out.println("aI "+aI.get(k));
						//System.out.println("aJ "+aJ.get(k));
					}
					getJourney(x1, x2, y1, y2, i-1, j-1, aI, aJ, nip3, journey);
				}
			}
			
			return false;
	}
	
	public void run(){

		
		ImagePlus imp2 = NewImage.createRGBImage ("Lines detection", ip.getWidth(), ip.getHeight(), 1, NewImage.FILL_WHITE);
		
		nip = imp2.getProcessor();	
		
		int k = 0;
		int[][] chunks;
		chunks = new int[scale][ip.getHeight()];
		for (int i = 0; i < chunks.length; i++) {
			Arrays.fill(chunks[i], 0);
		}
		
		for (int j = 0; j < ip.getHeight(); j++)
			for (int i = 0; i < ip.getWidth(); i++) {
				nip.putPixelValue(i, j, ip.getPixelValue(i, j));
				if ((i >= (ip.getWidth() / scale * k)) && (i <  ((ip.getWidth() / scale * k)  + (ip.getWidth() / scale)))) {
					if (ip.getPixelValue(i, j) == 0) {
						chunks[k][j] ++;
						//System.out.println(chunks[k][j]);
					}
				}
				if ( i == ((ip.getWidth() / scale * k) + (ip.getWidth() / scale)) ){
					k++;
				}
				if (i == 0) k = 0;
			}
		
		int length = 0;
		double average = 0;
		double countAver = 0;
		boolean rataj = false;
		for (k = 0; k < chunks.length; k++) {
			for (int j = 0; j < chunks[k].length-1; j++) {
				
				if ( (chunks[k][j]==0) && (chunks[k][j+1]!=0) ) {
					length = 0;
					rataj = true;
				} else if ( (chunks[k][j]!=0) && (chunks[k][j+1]==0) ) {
					rataj = false;
			//		System.out.println("length "+length);
					average = average + length;
					countAver++;
				} else if (rataj) length++;
				
			}
		}
		
		average = average / countAver;
//		System.out.println("average "+average);
		

		int[] sortChunks;
		lines[] line;
		line = new lines[scale];
		
		ImageProcessor ipOtsu = impOtsu.getProcessor();
		ImagePlus imp3 = NewImage.createRGBImage ("Lines detection", ipOtsu.getWidth(), ipOtsu.getHeight(), 1, NewImage.FILL_WHITE);
		
		nip2 = imp3.getProcessor();	

		imp4 = NewImage.createRGBImage ("Lines detection", ipOtsu.getWidth(), ipOtsu.getHeight(), 1, NewImage.FILL_WHITE);
		
		nip3 = imp4.getProcessor();	
		

		for (k = 0; k < scale; k++) {
		
			sortChunks = new int[ip.getHeight()];
			for (int i = 0; i < sortChunks.length; i++) {
				sortChunks[i] = chunks[k][i];
			}
			Arrays.sort(sortChunks);
			
/*			for (int i = 0; i < sortChunks.length; i++) {
				System.out.println("sortChunkd[i]" +sortChunks[i]);
			}
			
			double averageChunks = 0;
			double count = 0;
			for (int i = 0; i < sortChunks.length; i++) {
				if (sortChunks[i] != 0) {
					averageChunks = averageChunks + sortChunks[i];
					count++;
				}
			}
			averageChunks = averageChunks / count;
			
			System.out.println("averageChunks" +averageChunks);
			
			for (int i = 0; i < chunks.length; i++) {
				for (int j = 0; j < chunks[i].length; j++) {
					if (chunks[i][j] > averageChunks )
						chunks[i][j]
				}
			}*/
	
			int max = sortChunks[sortChunks.length - 1];
			int min1 = sortChunks[0];
			int min2 = sortChunks[0];
			int m = 0;
			while ((m<sortChunks.length) && (min1 == min2)) {
				min2 = sortChunks[m];
				m++;
/*				if (m > 500)
					System.out.println("ZASEKAVAM SA??line");*/
			}
			Random random = new Random();
			Color randomColor = Color.getHSBColor( random.nextFloat(), 1.0F, 1.0F );
			int color = ((randomColor.getRed() & 0xff) << 16) + ((randomColor.getGreen() & 0xff) << 8) + (randomColor.getBlue() & 0xff);

			line[k] = new lines();
			line[k].min = new ArrayList();
			line[k].max = new ArrayList();
			int i = 1;
		//	int l = 0;
			while (i < ip.getHeight()) {
				if ((chunks[k][i-1] == min1) && (chunks[k][i] != min1)) {
					int findMax = chunks[k][i];
					int ii = i;
					int newFindMax;

					int findMin = 3434;
					int iiMin = i;
					int newFindMin;	
					int len = 0;
					
					i++;
					while ((chunks[k][i] != min1) && (i<ip.getHeight()-1) ){
					/*	if (i > 500)
							System.out.println("ZASEKAVAM SA??line");*/
						newFindMax = chunks[k][i];
						if (newFindMax > findMax) {
							findMax = newFindMax;
							ii = i;
						}
						
						newFindMin = chunks[k][i];
						if (newFindMin <= findMin) {
							findMin = newFindMin;
							iiMin = i;
						}
						
						i++;
						len++;
					}
					
	//				System.out.println("len "+len);
					if ( len > (2*average) )
						line[k].min.add(iiMin);
					
					if ( len > (average / 3) )
						line[k].max.add(ii);
				//	l++;
				} else if ((chunks[k][i-1] == min2) && (chunks[k][i] != min2)) {
					int findMax = chunks[k][i];
					int ii = i;
					int newFindMax;
					i++;
					while ((chunks[k][i] != min2) && (i<ip.getHeight()-1) ) {
			/*			if (i > 500)
							System.out.println("ZASEKAVAM SA??line");*/
						newFindMax = chunks[k][i];
						if (newFindMax > findMax) {
							findMax = newFindMax;
							ii = i;
						}
						i++;
					}
					line[k].max.add(ii);
				//	nip2.putPixel(ip.getWidth() / scale * (k) + findMax, Integer.parseInt(line[k].max.get(l).toString()), color);
					//l++;
				}	
				i++;
			/*	if (i > 500)
					System.out.println("ZASEKAVAM SA??line");*/
			}
			
		}
		
		constant = 0;
		int count = 0;
		for (k = 0; k < scale; k++) 
			for (int j = 0; j < line[k].max.size()-1; j++) {		
				int max1 = Integer.parseInt(line[k].max.get(j).toString());
				int max2 = Integer.parseInt(line[k].max.get(j+1).toString());
				constant = constant + Math.abs(max1 - max2);
				count++;				
			}
		constant = constant / count;
		constant = constant - (constant / 7);
//		System.out.println("constant "+constant);
	//	constant = 35;
		
		int u = 0;
		for (k = 0; k < scale; k++) {
				u = 0;
				for (int j = 0; j < line[k].max.size(); j++) {
					int max1 = Integer.parseInt(line[k].max.get(j).toString());
					int max2 = 0;
					if (j != line[k].max.size()-1 )
						max2 = Integer.parseInt(line[k].max.get(j+1).toString());
					else
						max2 = nip.getHeight();
					//TODO magicka konstanta !!!!!!!
					//if ((Math.abs(max1 - max2) > (constant/2)) || (j == line[k].max.size()-1 )) {
					if ((Math.abs(max1 - max2) > (constant/2)) || (j == line[k].max.size()-1 )) {
						int min = chunks[k][max1];
						int newMin;
						int ii = j;
						for (int i = max1+1; i < max2; i++) {
							newMin = chunks[k][i];
							if (newMin < min) {
								min = newMin;
								ii = i;
							}
						}
						line[k].min.add(u, ii);
						Random random = new Random();
						Color randomColor = Color.getHSBColor( random.nextFloat(), 1.0F, 1.0F );
						int color = ((randomColor.getRed() & 0xff) << 16) + ((randomColor.getGreen() & 0xff) << 8) + (randomColor.getBlue() & 0xff);
	
						nip2.putPixel(ip.getWidth() / scale * (k) + min, Integer.parseInt(line[k].min.get(u).toString()), color);
						u++;
					}	
				}
		}


		for (int i = 0; i < ipOtsu.getWidth(); i++) 
			for (int j = 0; j < ipOtsu.getHeight(); j++) {
				if (ipOtsu.getPixelValue(i, j) == 0) {
					nip2.putPixelValue(i, j, 0);
					nip3.putPixelValue(i, j, 0);
				}
		}
		
		//double[] result = new double[line[2].min.size()]; //TODO toto spadne obcas
		//double[] countResult = new double[line[2].min.size()]; //TODO toto spadne obcas
		int theNearest = 0;
		double newDistance = 0;
		double newAngle = 0;
		double distance = 0;
		double protilahla = 0;
		double newProtilahla = 0;
		double angle = 0;
		boolean fill = false;
		int ll = 0;
	
		
		for (k = 0; k < scale; k++) {

			for (int i = 0; i < line[k].min.size(); i++) {
		
					//int theNearest = Integer.parseInt(line[k+1].min.get(0).toString());
					distance = 0;
					theNearest = 0;
					ll = 0;
					newDistance = 0;
					fill = false;
					for (int l = (k+1); l < scale; l++) {
						if (line[l].min.size() > 0) {
							if (!fill) {
								distance = Math.sqrt(
										      Math.pow(Math.abs(Integer.parseInt(line[k].min.get(i).toString()) - Integer.parseInt(line[l].min.get(0).toString())), 2) +
											+ Math.pow(Math.abs(ip.getWidth() / scale * (k) - ip.getWidth() / scale * (l)), 2)
											);
								theNearest = Integer.parseInt(line[l].min.get(0).toString());
								
								double prepona = distance;
								protilahla = Math.abs(Integer.parseInt(line[k].min.get(i).toString()) - Integer.parseInt(line[l].min.get(0).toString()));
								double sinAngle = protilahla / prepona;
								angle = Math.asin(sinAngle);
							//	System.out.println("angle "+angle);
								
								ll = l;
								fill = true;
							}
							for (int j = 1; j < line[l].min.size(); j++) {
								newDistance = Math.sqrt(
												Math.pow(Math.abs(Integer.parseInt(line[k].min.get(i).toString()) - Integer.parseInt(line[l].min.get(j).toString())), 2) +
												+ Math.pow(Math.abs(ip.getWidth() / scale * (k) - ip.getWidth() / scale * (l)), 2)
											);
								
								double prepona = newDistance;
								newProtilahla = Math.abs(Integer.parseInt(line[k].min.get(i).toString()) - Integer.parseInt(line[l].min.get(j).toString()));
								double sinAngle = newProtilahla / prepona;
								newAngle = Math.asin(sinAngle);
								
								//if (newDistance < distance) {
								if ( (newDistance + newAngle) < (distance + angle) ) {
									if ( ( (newAngle * 180) / Math.PI) < 45 ) {
									//	System.out.println("newAngle "+( (newAngle * 180) / Math.PI));
										distance = newDistance;
										protilahla = newProtilahla;
										theNearest = Integer.parseInt(line[l].min.get(j).toString());
										angle = newAngle;
										ll = l;
									}
								}
							}
						}
					}
					Random random = new Random();
					Color randomColor = Color.getHSBColor( random.nextFloat(), 1.0F, 1.0F );
					int color = ((Color.red.getRed() & 0xff) << 16) + ((0 & 0xff) << 8) + (0 & 0xff);
					nip.setColor(randomColor);
					nip2.setColor(randomColor);
					nip3.setColor(color);
					//TODO magicka konstanta !!!!!!!
				//	System.out.println("distance "+distance);
					

					
					if ( (protilahla < constant - (constant/4)) && ( ( (angle * 180) / Math.PI) < 45 ) ){
					//	System.out.println("protilahla "+protilahla);
					//	System.out.println("constant "+constant);
							if (distance == 0) {
//								nip.drawLine(ip.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ip.getWidth(), Integer.parseInt(line[k].min.get(i).toString()) );
//								nip2.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth(), Integer.parseInt(line[k].min.get(i).toString()));
//							//	result[i] = result[i] + Integer.parseInt(line[k].min.get(i).toString());
//							//	countResult[i]++;
//								nip3.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth(), Integer.parseInt(line[k].min.get(i).toString()));
							} else {								
								
								int countBlack = 0;
								int index = Integer.parseInt(line[k].min.get(i).toString()) + 5;
								for (int j = ip.getWidth() / scale * (k); j < ip.getWidth() / scale * (ll); j++) {
									if (nip.getPixelValue(j, index) == 0)
										countBlack++;
									index++;
								} 	
							//	if (countBlack < 3) {
									nip3.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth() / scale * (k+1), Integer.parseInt(line[k].min.get(i).toString()));
									nip.drawLine(ip.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()) + 5, ip.getWidth() / scale * (ll), theNearest +5);
							//	}
								
								nip2.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth() / scale * (k+1), Integer.parseInt(line[k].min.get(i).toString()));
								nip2.drawLine(ipOtsu.getWidth() / scale * (k+1), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth() / scale * (ll), theNearest);
							//	result[i] = result[i] + Integer.parseInt(line[k].min.get(i).toString());
							//	countResult[i]++;
								countBlack = 0;
								ArrayList blackPixel = new ArrayList(); 
/*								for (int j = ipOtsu.getWidth() / scale * (k); j < ipOtsu.getWidth() / scale * (k+1); j++) {
									System.out.println("pixelvalue "+nip3.getPixelValue(j, Integer.parseInt(line[k].min.get(i).toString())));
									if (nip3.getPixelValue(j, Integer.parseInt(line[k].min.get(i).toString())) != 255) {
										countBlack++;
										blackPixel.add(j);
									} //else
									//	nip3.putPixel(j, Integer.parseInt(line[k].min.get(i).toString()), color);
								}*/
								//if (countBlack < 2)
									//nip3.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth() / scale * (k+1), Integer.parseInt(line[k].min.get(i).toString()));
			
								if ( (nip3.getPixel(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()) -1) != color) &&
									(nip3.getPixel((ipOtsu.getWidth() / scale * (k)), Integer.parseInt(line[k].min.get(i).toString()) +1) != color)&&
									(nip3.getPixel((ipOtsu.getWidth() / scale * (k))-1, Integer.parseInt(line[k].min.get(i).toString()) -1) != color) &&
									(nip3.getPixel((ipOtsu.getWidth() / scale * (k))-1, Integer.parseInt(line[k].min.get(i).toString())) != color) &&
									(nip3.getPixel((ipOtsu.getWidth() / scale * (k))-1, Integer.parseInt(line[k].min.get(i).toString()) +1) != color) ) {
									
									countBlack = 0;
									for (int j = 0; j < ipOtsu.getWidth() / scale * (k); j++) {
										if (ip.getPixelValue(j, Integer.parseInt(line[k].min.get(i).toString())) == 0)
											countBlack++;
										//else
											//nip3.putPixel(j, Integer.parseInt(line[k].min.get(i).toString()), color);
									} 
									if (countBlack < 1)
										nip3.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), 0, Integer.parseInt(line[k].min.get(i).toString()));
								}
								countBlack = 0;
								for (int j = Integer.parseInt(line[k].min.get(i).toString()); j < theNearest; j++) {
									if (nip3.getPixelValue(ipOtsu.getWidth() / scale * (k+1), j) == 0)
										countBlack++;
									//else
									//	nip3.putPixel(ipOtsu.getWidth() / scale * (k+1), j, color);
								} 
								//if (countBlack < 2)
									nip3.drawLine(ipOtsu.getWidth() / scale * (k+1), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth() / scale * (ll), theNearest);					
							}
					} else {
						//nip.drawLine(ip.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ip.getWidth(), Integer.parseInt(line[k].min.get(i).toString()) );
						nip2.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth(), Integer.parseInt(line[k].min.get(i).toString()));
					//	result[i] = result[i] + Integer.parseInt(line[k].min.get(i).toString());
					//	countResult[i]++;
						int countBlack = 0;
						for (int j = ipOtsu.getWidth() / scale * (k); j < ipOtsu.getWidth(); j++) {
							if (nip3.getPixelValue(j, Integer.parseInt(line[k].min.get(i).toString())) == 0)
								countBlack++;
							//else
								//nip3.putPixel(j, Integer.parseInt(line[k].min.get(i).toString()), color);
						}
						if (countBlack < 1)
							nip3.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth(), Integer.parseInt(line[k].min.get(i).toString()));
					//	nip3.drawLine(ipOtsu.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()), ipOtsu.getWidth(), Integer.parseInt(line[k].min.get(i).toString()));
					}
			}
		}	
		
/*		for (int i = 0; i < result.length; i++) {
			result[i] = result[i] / countResult[i];
			System.out.println("result "+result[i]);
		}		
		double[] result2 = new double[result.length -1];
		for (int i = (result.length -1); i >0; i--) {
				result2[i-1] = result[i] - result[i-1];
		}
		//pixle TODO zmenit na percentualnost
		Result.setDiffLines(result2, numberResult);*/

		for (int i = 0; i < scale; i++) {
			nip2.setColor(Color.black);
			//nip2.drawLine(i* (nip2.getWidth() / scale), 0, i* (nip2.getWidth() / scale), nip2.getHeight());	
		}
		
		nip.setColor(Color.red);
		for (k = 0; k < scale; k++) {

			for (int i = 0; i < line[k].min.size(); i++) {
				nip.drawDot2(ip.getWidth() / scale * (k), Integer.parseInt(line[k].min.get(i).toString()));
				//nip.drawLine(ip.getWidth() / scale * (k), Integer.parseInt(line[k].max.get(i).toString()), ip.getWidth(), Integer.parseInt(line[k].max.get(i).toString()) );
				
			}
			
		}
		
		nip.setColor(Color.green);
		for (k = 0; k < scale; k++) {

			for (int i = 0; i < line[k].max.size(); i++) {
				nip.drawDot2(ip.getWidth() / scale * (k), Integer.parseInt(line[k].max.get(i).toString()));
				//nip.drawLine(ip.getWidth() / scale * (k), Integer.parseInt(line[k].max.get(i).toString()), ip.getWidth(), Integer.parseInt(line[k].max.get(i).toString()) );
				
			}
			
		}
			
	
		if (draw) {
			mainWindow.intFrameArrayList.add(new internalFrame("Lines detection", imp2, FileChooser.number, numberResult, 1, true));
			try {
				//mainWindow.intFrame[FileChooser.number].setSelected(true);
				((internalFrame)(mainWindow.intFrameArrayList.get(FileChooser.number))).setSelected(setSelected);
			} catch(Exception e){
				System.out.println("Chyba");
			}	
			++FileChooser.number;	
			
			mainWindow.intFrameArrayList.add(new internalFrame("Lines detection", imp3, FileChooser.number, numberResult, 1, true));
			try {
				//mainWindow.intFrame[FileChooser.number].setSelected(true);
				((internalFrame)(mainWindow.intFrameArrayList.get(FileChooser.number))).setSelected(setSelected);
			} catch(Exception e){
				System.out.println("Chyba");
			}	
			++FileChooser.number;	
			
			mainWindow.intFrameArrayList.add(new internalFrame("Lines detection", imp4, FileChooser.number, numberResult, 1, true));
			try {
				//mainWindow.intFrame[FileChooser.number].setSelected(true);
				((internalFrame)(mainWindow.intFrameArrayList.get(FileChooser.number))).setSelected(setSelected);
			} catch(Exception e){
				System.out.println("Chyba");
			}	
			++FileChooser.number;
		}
		
	//	ArrayList aI = new ArrayList();
	//	ArrayList aJ = new ArrayList();
	//	System.out.println("journey "+getJourney(0, 10, 0, 20, 10, 10, aI, aJ, nip3));

	}
	
	public ImagePlus getImg(){
		return imp4;
	}
	
	public ImageProcessor getIp(){
		return nip3;
	}	
	
	protected void finalize() throws Throwable {
	    try {
	        //close();        // close open files
	    } finally {
	        super.finalize();
	        System.out.println("line has been finalized.");
	    }
	}			

}
