@SuppressWarnings("resource")
	public static void main(final String[] args) throws ImageFormatException {
		SysOutPrinter printer = new SysOutPrinter();
		Scanner in = new Scanner(System.in);
		printer.print("Please select the picture with a 'green screen' background.");
		Picture greenScreenedPic = new Picture(FileChooser.pickAFile());
		// Picture greenScreenedPic = new
		// Picture("P:\\Programming\\Workspace\\ProgrammingClass\\util\\Maze_Background.jpg");
		printer.print("Please select the picture that you want to be applied as a background to the other picture.");
		Picture backgroundPic = new Picture(FileChooser.pickAFile());
		// Picture backgroundPic = new
		// Picture("P:\\Programming\\Workspace\\ProgrammingClass\\util\\BeauPicture.jpg");
		Preconditions.checkArgument(
				greenScreenedPic.getHeight() <= backgroundPic.getHeight(),
				"Background picture has to small height.");
		Preconditions.checkArgument(
				greenScreenedPic.getWidth() <= backgroundPic.getWidth(),
				"Background picture has to small width.");
		Picture combinedPicture = new Picture(backgroundPic.getWidth(),
				backgroundPic.getHeight());
		combinedPicture.setAllPixelsToAColor(Color.WHITE);
		printer.print("How much error do you think is in your picture? (integer, example is 10)");
		int ERROR_TRESH = in.nextInt();

		// Figure out the padding for the picture to be applied.
		int limitLeft = (int) trunc((backgroundPic.getWidth() - greenScreenedPic
				.getWidth()) / 2);
		int limitTop = (int) trunc((backgroundPic.getHeight() - greenScreenedPic
				.getHeight()) / 2);

		// Find the main color of the green screen
		Map<Color, Integer> numColors = new HashMap<>();

		for (Pixel pixel : greenScreenedPic.getPixels()) {
			Color pixColor = pixel.getColor();
			if (!numColors.containsKey(pixColor)) {
				numColors.put(pixColor, 1);
			}
			int currNum = numColors.get(pixColor);
			numColors.remove(pixColor);
			numColors.put(pixColor, currNum + 1);
		}

		int highest = 0;
		Color highestColor = Color.WHITE; // The color of our green screen
		for (Color highestLocalColor : numColors.keySet()) {
			int numOfColors = numColors.get(highestLocalColor);
			if (numOfColors > highest) {
				highest = numOfColors;
				highestColor = highestLocalColor;
			}
		}
		System.out.printf("Using color as chroma key (R,G,B): %1$s,%2$s,%3$s",
				highestColor.getRed(), highestColor.getGreen(),
				highestColor.getBlue());

		// Make the magic
		for (Pixel combinedPixel : combinedPicture.getPixels()) {
			int x = combinedPixel.getX();
			int y = combinedPixel.getY();
			try {
				Pixel appliedPixel = greenScreenedPic.getPixel(x - limitLeft, y
						- limitTop);
				if (appliedPixel.colorDistance(highestColor) < ERROR_TRESH) {
					throw new ArrayIndexOutOfBoundsException("Catch me!");
				} else {
					combinedPixel.setColor(appliedPixel.getColor());
				}
			} catch (ArrayIndexOutOfBoundsException arioobe) {
				Color backgroundColor = backgroundPic.getPixel(x, y).getColor();
				combinedPixel.setColor(backgroundColor);
			}

			// if(x < limitLeft || x > limitRight || y > limitBottom || y <
			// limitTop) {
			// combinedPixel.setColor(backgroundPic.getPixel(x, y).getColor());
			// } else {
			// if (greenScreenedPic.getPixel(x - limitLeft, y -
			// limitTop).getColor().equals(highestColor)){
			// combinedPixel.setColor(backgroundPic.getPixel(x, y).getColor());
			// i++;
			// } else {
			// combinedPixel.setColor(greenScreenedPic.getPixel(x - limitLeft, y
			// - limitTop).getColor());
			// System.out.println(x + ", " + y);
			// }
			// }
		}

		combinedPicture.show(); // Show the magic
	}