public Rectangle detect() {
		JFrame frame = new JFrame();
		BufferedImage dbgImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
		
		
		nearImage = new NearestPointImg(image);
		nearPattern = new NearestPointImg(pattern);
		
		frame.add(new DisplayPanel(dbgImg));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		
		double bestCandidate = Double.MAX_VALUE; 
		int bestX = 0;
		int bestY = 0;
		
		int width = image.getWidth() - pattern.getWidth();
		int height = image.getHeight() - pattern.getHeight();
		for (int i = 0; i < width; i=i+INCREMENT) {
			for (int j = 0; j < height; j=j+INCREMENT) {
				hausdorffDistance.setImage(new NearestPointImgSub(nearImage, i, j, pattern.getWidth(), pattern.getHeight()));
				hausdorffDistance.setPattern(nearPattern);
				double dist = hausdorffDistance.getDistance();
				int val = (int) dist; 
				if(val>255) {
					val = 255;
				}
				int color = ImgUtil.toRGB(val, val, val);
				draw(dbgImg, i, j, color, INCREMENT);
				
				if (dist < bestCandidate) {
					//remove old color
					int oldColor = ImgUtil.toRGB((int)bestCandidate, (int)bestCandidate, (int)bestCandidate);
					draw(dbgImg, bestX, bestY, oldColor, INCREMENT);
					
					draw(dbgImg, i, j, ImgUtil.toRGB(255, 0, 0), INCREMENT);
					bestX = i;
					bestY = j;
					bestCandidate = dist;
					System.out.println(dist);
					this.listener.onAction(new Rectangle(bestX, bestY, pattern.getWidth(), pattern.getHeight()));
				}
				
				//if (dist < 15.5f) {
				/*if (dist < 20.5f) {
					this.listener.onAction(new Rectangle(i, j, pattern.getWidth(), pattern.getHeight()));
				}*/
				
				
				
				frame.repaint();
			}
		}
		
		return new Rectangle(bestX, bestY, pattern.getWidth(), pattern.getHeight());
	}