/*
  Author: Philip Chan

  Description: Given a photo, allow the user to crop the image
 */

import java.awt.*;
import java.util.*;

public class HW5
{
    final static Color WHITE = Color.WHITE;
    final static Color RED = Color.RED;

    // Given the location of the region:
    // upperLeftX, upperLeftY -- x,y coordinates of the upper left cornor
    // lowerRightX, lowerRightY -- x,y coordinates of the lower right cornor
    // draw a red bounding box for the region
    // change the pixels outside the box to white
    // "move" the region to the center
    public static void crop(Picture pic,
			    int upperLeftX, int upperLeftY, 
			    int lowerRightX, int lowerRightY) throws InterruptedException
    {
	int width = pic.width(),
	    height = pic.height();

	/*
	Color color = pic.get(x, y);   // get the color of a pixel at x,y
        pic.set(x, y, color2);         // set color2 at pixel x,y
	*/

	// step 1: draw the bounding box in red
	// start your instructions here











	Thread.sleep(3000);  // delay the display of the new image by 3 seconds
	pic.show();

	// step 2: change the pixels outside the region/box to white
	// start your instructions here













	Thread.sleep(3000);  // delay the display of the new image by 3 seconds
	pic.show();

	Picture pic2 = getBlankPicture(width, height);
	// step 3: "move" the region to the center
	// copy the selected region from pic to the center of pic2
	// start your instructions here










	System.out.println("Displaying the centered image...");
	Thread.sleep(3000);  // delay the display of the new image by 3 seconds
	pic2.show();         // display the new image
    }


    public static void main (String[] args) throws InterruptedException
    {
	//Picture pic = new Picture(args[0]);
	Picture pic = new Picture("beach.jpg");
	pic.setOriginLowerLeft();  // so we can use the regular x,y coordinates	
	pic.show();


	System.out.println("Picture width = " +  pic.width() + ", height = " + pic.height()); 

	Scanner keyboard = new Scanner(System.in);
	System.out.println("For the upper-left corner");
	System.out.print("Please type in the x coordinate: ");
	int upperLeftX = keyboard.nextInt();
	System.out.print("Please type in the y coordinate: ");
	int upperLeftY = keyboard.nextInt();

	System.out.println("For the lower-right corner");
	System.out.print("Please type in the x coordinate: ");
	int lowerRightX = keyboard.nextInt();
	System.out.print("Please type in the y coordinate: ");
	int lowerRightY = keyboard.nextInt();

	System.out.println("Upper-left corner: (" + upperLeftX + "," + upperLeftY + ")");
	System.out.println("Lower-right corner: (" + lowerRightX + "," + lowerRightY + ")");

	if (upperLeftX < lowerRightX && upperLeftY > lowerRightY)
	    crop(pic, upperLeftX, upperLeftY, lowerRightX, lowerRightY);
	else
	    {
		System.out.println("Error: upperLeftX should be less than lowerRightX and upperLeftY greater than lowerRightY");
		System.exit(-1);
	    }
    }

    public static Picture getBlankPicture(int width, int height)
    {
	Picture pic = new Picture(width, height);
	for (int i = 0; i < width; i++)
	    for (int j = 0; j < height; j++)
		pic.set(i,j, WHITE);
	return pic;
    }
}
