import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Driver {

	public static void main(String[] args) {
		// read in image
		BufferedImage image = null;
		try {
			image = ImageIO.read(new File(args[0]));
		} catch (IOException e) {
			System.out.println("Couldn't open original image file.");
			System.exit(-1);
		}
		PixelSorter sorter = new PixelSorter(image);
		// get all pixels in file as a 2d array
		
		
		// create a new BufferedImage for creating the sorted image, using the
		// same width, height, and type of original image
		BufferedImage imageToWrite = sorter.rowSortWrite();
		File outputFile = new File(getNewFileNameForRowSort(args[0]));
		// create new file and write it.
		try {

			ImageIO.write(imageToWrite, "jpg", outputFile);

		} catch (IOException e) {
			System.out.println("Problem writing to new sorted file");
			System.exit(-1);

		}
		
		//create a gradient image and write it to file 
		imageToWrite = sorter.pixelSortWrite();
		outputFile = new File(getNewFileNameForGradientSort(args[0]));
		try {
			ImageIO.write(imageToWrite, "jpg", outputFile);
		} catch (IOException e) {
			System.out.println("Problem writing to new gradient sorted file");
			System.exit(-1);
		}

		imageToWrite = sorter.flattenImage();
		outputFile = new File(getNewFileNameForFlattenedImage(args[0]));
		try {
			ImageIO.write(imageToWrite, "jpg", outputFile);
		} catch (IOException e) {
			System.out.println("Problem writing to new gradient sorted file");
			System.exit(-1);
		}
	}

    private static String getNewFileNameForFlattenedImage(String originalFile) {
        return originalFile.replace(originalFile.substring(originalFile.length() - 4, originalFile.length()),
                "Flattened.jpg");

    }


    // takes original file name from original image, removes its old type, adds
		// "Sorted.jpg" to the end of it
		private static String getNewFileNameForRowSort(String originalFile) {
			return originalFile.replace(originalFile.substring(originalFile.length() - 4, originalFile.length()),
					"Sorted.jpg");
		}
		// takes original file name from original image, removes its old type, adds
				// "GradientSorted.jpg" to the end of it
		private static String getNewFileNameForGradientSort(String originalFile)
		{
			return originalFile.replace(originalFile.substring(originalFile.length() - 4, originalFile.length()),
					"GradientSorted.jpg");
		
		}
}

@@ -632,32 +632,159 @@
 e imageToWrite =
+ null;%0A%09%09File outputFile = null; %0A%09%09// create new file and write it.%0A%09%09if(args%5B1%5D.compareTo(%22regular%22) == 0)%7B%0A%09%09%09imageToWrite =
  sorter.rowSortW
@@ -789,29 +789,25 @@
 tWrite();%0A%09%09
-File 
+%09
 outputFile =
@@ -856,43 +856,8 @@
 ));%0A
-%09%09// create new file and write it.%0A
 %09%09tr
@@ -1029,16 +1029,25 @@
 %0A%0A%09%09%7D%0A%09%09
+%7D%0A%09%09else%7B
 %0A%09%09//cre
@@ -1377,19 +1377,24 @@
 1);%0A%09%09%7D%0A
+%09%09%7D
 %0A%09%09
+//
 imageToW
@@ -1421,24 +1421,26 @@
 nImage();%0A%09%09
+//
 outputFile =
@@ -1486,39 +1486,43 @@
 ge(args%5B0%5D));%0A%09%09
+//
 try %7B%0A%09
+//
 %09%09ImageIO.write(
@@ -1548,32 +1548,34 @@
 , outputFile);%0A%09
+//
 %09%7D catch (IOExce
@@ -1578,32 +1578,34 @@
 Exception e) %7B%0A%09
+//
 %09%09System.out.pri
@@ -1650,32 +1650,34 @@
 sorted file%22);%0A%09
+//
 %09%09System.exit(-1
@@ -1676,24 +1676,26 @@
 .exit(-1);%0A%09
+//
 %09%7D%0A%09%7D%0A%0A    p

Update Driver.java