CSE-8A / PA7 / PA7.java
PA7.java
Raw
import java.awt.Color;

public class PA7 {

    // ------------------------------------------------------------------------
    // Tests
    // ------------------------------------------------------------------------

    public static void testCanvas() {
        // Create a blue canvas with width 5 and height 10
        Image canvas1 = new Image(5, 10, Color.blue);

        // Verify the result is a 10x5 blue canvas
        canvas1.explore();

        // TODO: Add two more test cases
    }

    public static void testCrop() {
        // Set of colors implemented in a variable
        Color a1 = new Color (255, 0, 115);
        Color a2 = new Color ( 180, 230, 85);
        Color a3 = new Color (0, 255, 190);
        Color a4 = new Color (45, 80, 120);
        Color a5 = new Color (240, 90, 255); 
        Color a6 = new Color (160,70, 0); 
        // Create a 4x4 2D array
        Color[][] test1 = { 
                { Color.black, Color.black, Color.red, Color.red }, // row0
                { Color.black, Color.black, Color.red, Color.red }, // row1
                { Color.black, Color.black, Color.red, Color.red }, // row2
                { Color.black, Color.black, Color.black, Color.black } // row3
        };

        // Create an image from the array and crop the bottom right corner
        Image img1 = new Image(test1);
        Image cropped1 = img1.crop(2, 0, 4, 3);

        // Visualize the cropped picture
        cropped1.explore();

        Image img2 = new Image("images/pixel-heart.png");
        Image cropped2 = img2.crop(100, 100, 500, 500);

        cropped2.explore();
        //Create 1x1 color array
        Color[][] test3 = {
            {Color.blue}
        };

        Image img3 = new Image(test3);
        Image cropped3 = img3.crop(0, 0, 1, 1);
        cropped3.explore();
        //Creating array with color values of 255 and/or 0
        Color[][] test4 = {
            {a1, a3, a6, a1},
            {a2, a4, a1, a5},
            {a3, a5, a2, a1},
            {a4, a6, a3, a2},
            {a5, a1, a2, a6},
            {a6, a4, a2, a4}
        };

        Image img4 = new Image(test4);
        Image cropped4 = img4.crop(1,1,3,4);
        cropped4.explore();



        // TODO: Add two more test cases
    }

    public static void testOverlay() {
        //Set of colors implemented in a variable
        Color a1 = new Color (255, 0, 115);
        Color a2 = new Color ( 180, 230, 85);
        Color a3 = new Color (0, 255, 190);
        Color a4 = new Color (45, 80, 120);
        Color a5 = new Color (240, 90, 255); 
        Color a6 = new Color (160,70, 0); 

        // Create a 4x4 2D array
        Color[][] bgTest1 = { 
            { Color.black, Color.black, Color.black, Color.black }, // row0
            { Color.black, Color.black, Color.black, Color.black }, // row1
            { Color.black, Color.black, Color.black, Color.black }, // row2
            { Color.black, Color.black, Color.black, Color.black } // row3
        };

        // Create a 3x2 2D array
        Color[][] fgTest1 = { 
            { Color.red, Color.red }, 
            { Color.red, Color.red }, 
            { Color.red, Color.red } 
        };

        // Create an image from the array and crop the bottom right corner
        Image bgImg1 = new Image(bgTest1);
        Image fgImg1 = new Image(fgTest1);
        Image overlayed1 = fgImg1.overlay(bgImg1, 1, 1);

        // Visualize the cropped picture
        overlayed1.explore();
        //Creates a 5x4 Color array
        Color [][] bgTest2 = {
            {Color.blue, Color.green, Color.red, Color.black},
            {Color.red, Color.magenta, Color.orange, Color.yellow},
            {Color.black, Color.blue, Color.blue, Color.black},
            {Color.pink, Color.blue, Color.white, Color.green},
            {Color.red, Color.black, Color.green, Color.blue}
        };
        //Creates a 3x3 Color array
        Color[][] fgTest2 = {
            {Color.blue, Color.red, Color.green},
            {Color.black, Color.blue, Color.red},
            {Color.blue, Color.magenta, Color.pink}
        };

        Image bgImg2 = new Image(bgTest2);
        Image fgImg2 = new Image(fgTest2);

        Image overlayed2 = fgImg2.overlay(bgImg2, 0, 1);
        overlayed2.explore();
        //Creates a 3x6 color Array
        Color [][] bgTest3 = {
            {a2, a1, a3, a4, a5, a6},
            {a1, a4, a3, a2, a1, a2},
            {a3, a6, a5, a4, a6, a1}
        };
        //Create a 1x1 array
        Color [][] fgTest3 = {
            {a2}
        };

        Image bgImg3 = new Image(bgTest3);
        Image fgImg3 = new Image(fgTest3);
        //TopLeft corners at [0][0]
        Image overlayed3 = fgImg3.overlay(bgImg3, 0, 0);
        overlayed3.explore();

        // TODO: Add two more test cases
    }

    public static void testChromakey() {
        // Set of colors implemented in a variable
        Color a1 = new Color (255, 0, 115);
        Color a2 = new Color ( 180, 230, 85);
        Color a3 = new Color (0, 255, 190);
        Color a4 = new Color (45, 80, 120);
        Color a5 = new Color (240, 90, 255); 
        Color a6 = new Color (160,70, 0); 

        // Create a 4x4 2D array
        Color[][] bgTest1 = { 
            { Color.red, Color.red, Color.red, Color.red }, // row0
            { Color.red, Color.red, Color.red, Color.red }, // row1
            { Color.red, Color.red, Color.red, Color.red }, // row2
            { Color.red, Color.red, Color.red, Color.red } // row3
        };

        // Create a 3x2 2D array
        Color[][] fgTest1 = { 
            { Color.green, Color.green, Color.black, Color.black }, // row0
            { Color.green, Color.green, Color.black, Color.black }, // row1
            { Color.green, Color.green, Color.black, Color.black }, // row2
            { Color.green, Color.green, Color.black, Color.black } // row3
        };

        // Create an image from the array and crop the bottom right corner
        Image bgImg1 = new Image(bgTest1);
        Image fgImg1 = new Image(fgTest1);
        Image chromakeyed1 = fgImg1.chromakey(bgImg1, Color.green, 1);

        // Visualize the cropped picture
        chromakeyed1.explore();
        //Creates a 3x4 Array
        Color [][] bgTest2 = {
            {Color.blue, Color.green, Color.blue, Color.blue},
            {Color.black, Color.blue, Color.pink, Color.red},
            {Color.blue, Color.magenta, Color.blue, Color.white},
        };
        //Creates a 3x4 Array
        Color [][]fgTest2 = {
             {Color.blue, Color.blue, Color.black, Color.green},
             {Color.blue, Color.green, Color.black, Color.pink},
             {Color.black, Color.red, Color.blue, Color.blue}
        };

        Image bgImg2 = new Image(bgTest2);
        Image fgImg2 = new Image(fgTest2);
        Image chromakeyed2= fgImg2.chromakey(bgImg2, Color.blue, 1);

        chromakeyed2.explore();

        //Creates a 1x1 Array
        Color [][] bgTest3 = {
            {a1}
        };
        //Creates a 1x1 Array
        Color [][] fgTest3 = {
            {a4}
        };

        Image bgImg3 = new Image(bgTest3);
        Image fgImg3 = new Image(fgTest3);
        //If the distance is less than 40, then the pixel of the calling image would be replaced by the pixel of
        //the background image at the same coordinate
        Image chromakeyed3 = fgImg3.chromakey(bgImg3, a3, 40);

        chromakeyed3.explore();

    }

    public static void testFlipHorizontal() {
        // Set of colors implemented in a variable
        Color a1 = new Color(255, 0, 115);
        Color a2 = new Color(180, 230, 85);
        Color a3 = new Color(0, 255, 190);
        Color a4 = new Color(45, 80, 120);
        Color a5 = new Color(240, 90, 255);
        Color a6 = new Color(160, 70, 0);

        // Create 4x4 2D array
        Color[][] test1 = { 
                { Color.black, Color.black, Color.black, Color.black }, // row0
                { Color.black, Color.black, Color.black, Color.black }, // row1
                { Color.red, Color.red, Color.red, Color.red }, // row2
                { Color.red, Color.red, Color.red, Color.red } // row3
        };

        // First visualize the original image
        Image img1 = new Image(test1);
        img1.explore();

        // Flip the image and visualize the result
        Image flippedImg1 = img1.flipHorizontal();
        flippedImg1.explore();
        //Create a 5x4 color 2D array
        Color [][] test2 = {
            {Color.blue, Color.red, Color.green, Color.white},
            {Color.red, Color.orange, Color.yellow, Color.black},
            {Color.magenta, Color.red, Color.pink, Color.red},
            {Color.blue, Color.black, Color.blue, Color.black},
            {Color.red, Color.red, Color.red, Color.red}
        };

        Image img2 = new Image(test2);
        Image flippedImg2 = img2.flipHorizontal();
        flippedImg2.explore();
        //Create a 2D color array, 3x5 Array
        Color [][]test3 = {
            {a1, a2, a2, a3, a4},
            {a2, a3, a2, a5, a6},
            {a1, a4, a5, a2, a3}
        };

        Image img3 = new Image(test3);
        Image flippedImg3 = img3.flipHorizontal();
        flippedImg3.explore();

    }

    // ------------------------------------------------------------------------
    // Main Method
    // ------------------------------------------------------------------------

    public static void main(String[] args) {
        // You may want to uncomment one test at a time
        // NOTE: testCanvas will error unless the canvas constructor is implemented
        // please implement the canvas constructor before uncommenting that line.

        testCanvas();
        testCrop();
        testOverlay();
        testChromakey();
        testFlipHorizontal();
    
        Image cropImg = new Image("images/crane.jpg");
        Image catNDog = new Image("images/Cute_cat_and_dog_3.jpg");
        Image graceHopper = new Image("images/grace-hopper.png");
        Image heart = new Image("images/pixel-heart.png");
//crops the crane image
        Image croppedGH = cropImg.crop(0, 0, 700, 700);
//combines the cat and Dog image with crane image using chromakey Method
        Image craneNCatNDog = catNDog.chromakey(croppedGH, Color.white, 100);
//combines image with grace-hopper using overlay method
        Image overlay3 = graceHopper.overlay(craneNCatNDog, 50, 1200);
//flips the heart image
        Image flippedHeart = heart.flipHorizontal();
//overlay flipped heart image with the 3 combined images image
        Image together = flippedHeart.overlay(overlay3, 1000, 200);
        together.explore();





    }

        




    }