import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.*; /** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } /** * Constructor that takes a buffered image * @param image the buffered image to use */ public Picture(BufferedImage image) { super(image); } ////////////////////// methods /////////////////////////////////////// /** * Method to return a string with information about this picture. * @return a string with information about the picture such as fileName, * height and width. */ public String toString() { String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth(); return output; } public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the red value value = pixelObj.getRed(); // decrease the red value by 50% (1/2) value = value/2; // set the red value of the current pixel to the new value pixelObj.setRed(value); } } public void increaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the red value value = pixelObj.getRed(); // increase the red value by 50% value = value*2; // set the red value of the current pixel to the new value pixelObj.setRed(value); } } public void changeRed(double factor) { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the red value value = pixelObj.getRed(); // modify the red value by user's factor value = (int) (value * factor); // set the red value of the current pixel to the new value pixelObj.setRed(value); } } public void clearBlue01() { Pixel[] pixelArray = this.getPixels(); // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // set the blue value to 0 pixelObj.setBlue(0); } } public void clearBlue02() { Pixel[] pixelArray = this.getPixels(); int index = 0; Pixel pixelObj = null; // loop through all the pixels in the array while (index < pixelArray.length) { pixelObj = pixelArray[index]; // set the blue value to 0 pixelObj.setBlue(0); index++; } } public void clearBlue03() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; // loop through all the pixels in the array for (int index = 0; index < pixelArray.length; index++) { pixelObj = pixelArray[index]; // set the blue value to 0 pixelObj.setBlue(0); } } public void clearBlue04() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; // loop through the columns (x direction) for (int x = 0; x < this.getWidth(); x++) { // loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++) { // get the current pixel pixelObj = this.getPixel(x, y); // set the blue value to 0 pixelObj.setBlue(0); } } } public void clearBlueTop() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; // loop through all the pixels in the array for (int index = 0; index < pixelArray.length/2; index++) { pixelObj = pixelArray[index]; // set the blue value to 0 pixelObj.setBlue(0); } } public void changeColor(double factor_r, double factor_g, double factor_b) { Pixel[] pixelArray = this.getPixels(); int value_r = 0; int value_g = 0; int value_b = 0; // loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { // get the red value value_r = pixelObj.getRed(); // get the green value value_g = pixelObj.getGreen(); // get the green value value_b = pixelObj.getBlue(); // modify the red value by user's factor value_r = (int) (value_r * factor_r); // modify the green value by user's factor value_g = (int) (value_g * factor_g); // modify the blue value by user's factor value_b = (int) (value_b * factor_b); // set the red value of the current pixel to the new value pixelObj.setRed(value_r); // set the green value of the current pixel to the new value pixelObj.setGreen(value_g); // set the blue value of the current pixel to the new value pixelObj.setBlue(value_b); } } public void mirrorVertical(){ int mirrorPoint = this.getWidth()/2; Pixel leftPixel = null; Pixel rightPixel = null; // loop through the rows for (int y = 0; y < this.getHeight(); y++) { // loop from 0 to just the mirror point for (int x = 0; x < mirrorPoint; x++) { leftPixel = this.getPixel(x, y); rightPixel = this.getPixel(this.getWidth() - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); } } } public void mirrorHorizontal(){ int mirrorPoint = this.getHeight()/2; Pixel topPixel = null; Pixel bottomPixel = null; // loop through the rows for (int x = 0; x < this.getWidth(); x++) { // loop from 0 to just the mirror point for (int y = 0; y < mirrorPoint; y++) { topPixel = this.getPixel(x, y); bottomPixel = this.getPixel(x, this.getHeight() - 1 - y); bottomPixel.setColor(topPixel.getColor()); } } } public void mirrorHthenV(){ Pixel newPixel = null; for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { newPixel = this.getPixel(this.getWidth() - 1 - x, this.getHeight() - 1 - y); newPixel.setColor(this.getPixel(x, y).getColor()); } } } public void mirrorSquareDiagonal(){ Pixel newPixel = null; for (int x = 0; x < this.getWidth(); x++) { for (int y = x; y < this.getHeight(); y++) { newPixel = this.getPixel(y, x); newPixel.setColor(this.getPixel(x, y).getColor()); } } } public void mirrorDiagonal02(){ Pixel newPixel = null; for (int x = 0; x < this.getWidth(); x++) { for (int y = x; y < this.getHeight(); y++) { newPixel = this.getPixel(this.getWidth() - 1 - x, this.getHeight() - 1 - y); newPixel.setColor(this.getPixel(x, y).getColor()); } } } public void mirrorKaleidoscope01(){ Pixel newPixel = null; for (int y = 0; y < this.getHeight(); y++) { for (int x = y; x < this.getWidth(); x++) { newPixel = this.getPixel(this.getWidth() - 1 - x, y); newPixel.setColor(this.getPixel(x, y).getColor()); } } } public void mirrorKaleidoscope02(){ Pixel newPixel = null; for (int x = 0; x < this.getWidth(); x++) { for (int y = x; y < this.getHeight(); y++) { newPixel = this.getPixel(x, this.getHeight() - 1 - y); newPixel.setColor(this.getPixel(x, y).getColor()); } } } public void makeNegative(){ Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int redValue, blueValue, greenValue = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // get the values redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); // set the pixel's color pixelObj.setColor(new Color(255-redValue, 255-greenValue, 255-blueValue)); } } public void makeGray(){ Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int redValue, blueValue, greenValue = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // get the values redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); int ave = (redValue + greenValue + blueValue)/3; // set the pixel's color pixelObj.setColor(new Color(ave, ave, ave)); } } public void makeGrayscale(){ Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int redValue, blueValue, greenValue = 0; int intensity = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixelObj = pixelArray[i]; // get the values redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); intensity = (int) (redValue*0.299 + greenValue*0.587 + blueValue*0.114); // set the pixel's color pixelObj.setColor(new Color(intensity, intensity, intensity)); } } public void copyKatie01() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } public void copyKatie02() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 73, targetX = 100; sourceX < 130; sourceX++, targetX++) { // loop through the rows for (int sourceY = 3, targetY = 100; sourceY < 78; sourceY++, targetY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } /** * Method to copy part of the source picture to the current picture * @param sourcePicture the picture to copy from * @param startX the x at the upper left corner to start copying from the source picture (inclusive) * @param starty the y at the upper left corner to start copying from the source picture (inclusive) * @param endX the x at the bottom right corner (exclusive) * @param endY the y at the bottom right corner (exclusive) * @param targetStartX the upper left corner to start copying to in the target (this picture) * @param targetStartY the upper left corner to start copying to in the target (this picture) * */ public void copy(Picture sourcePicture, int startX, int startY, int endX, int endY, int targetStartX, int targetStartY){ Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the x values for (int x = startX, tx = targetStartX; x < endX; x++, tx++) { // loop through the y values for (int y = startY, ty = targetStartY; y < endY; y++, ty++){ // copy the source color to the target color sourcePixel = sourcePicture.getPixel(x, y); targetPixel = this.getPixel(tx, ty); targetPixel.setColor(sourcePixel.getColor()); } } } // Don't forget "import java.awt.Color;" in interactions panel public void removeRedEye(int startX, int startY, int endX, int endY, Color newColor) { Pixel pixelObj = null; // loop through the pixels in the rectangle defined by the startX, startY, and endX and endY for (int x = startX; x < endX; x++) { for (int y = startY; y < endY; y++) { // get the current pixel pixelObj = getPixel(x, y); // if the color is near red then change it if (pixelObj.colorDistance(Color.RED) < 167) { pixelObj.setColor(newColor); } } } } public void removeRedEyes(int left_startX, int left_startY, int left_endX, int left_endY, int right_startX, int right_startY, int right_endX, int right_endY, Color newColor){ Pixel pixelObj = null; for (int x = left_startX; x < left_endX; x++) { for (int y = left_startY; y < left_endY; y++) { // get the current pixel pixelObj = getPixel(x, y); // if the color is near red then change it if (pixelObj.colorDistance(Color.RED) < 167) { pixelObj.setColor(newColor); } } } for (int w = right_startX; w < right_endX; w++) { for (int z = right_startY; z < right_endY; z++) { // get the current pixel pixelObj = getPixel(w, z); // if the color is near red then change it if (pixelObj.colorDistance(Color.RED) < 167) { pixelObj.setColor(newColor); } } } } public void edgeDetectBottom(int dist) { Pixel topObj = null; Pixel bottomObj = null; // loop through all x and all but last y for (int x = 0; x < this.getWidth(); x++){ for (int y = 0; y < this.getHeight() - 1; y++) { // get the top and bottom pixels topObj = getPixel(x, y); bottomObj = getPixel(x, y + 1); // if the dist between the colors is greater than the passed dist set it to black if (topObj.colorDistance(bottomObj.getColor()) > dist) { topObj.setColor(Color.BLACK); } else { topObj.setColor(Color.WHITE); } } } } public void edgeDetectRight(int dist) { Pixel leftObj = null; Pixel rightObj = null; // loop through all but last x and all y for (int y = 0; y < this.getHeight(); y++){ for (int x = 0; x < this.getWidth() - 1; x++) { // get the left and right pixels leftObj = getPixel(x, y); rightObj = getPixel(x + 1, y); // if the dist between the colors is greater than the passed dist set it to black if (leftObj.colorDistance(rightObj.getColor()) > dist) { leftObj.setColor(Color.BLACK); } else { leftObj.setColor(Color.WHITE); } } } } public void swapBackground(Picture oldBackground, Picture newBackground){ Pixel currPixel = null; Pixel oldPixel = null; Pixel newPixel = null; // loop through the columns for (int x = 0; x < this.getWidth(); x++) { // loop through the rows for (int y = 0; y < this.getHeight(); y++) { // get the current pixel and old background pixel currPixel = this.getPixel(x, y); oldPixel = oldBackground.getPixel(x, y); /* if the distance between the current pixel color * and the old backgroun pixel color is less than the 15 * then swap in the new background pixel * */ if (currPixel.colorDistance(oldPixel.getColor()) < 20.0) { newPixel = newBackground.getPixel(x, y); currPixel.setColor(newPixel.getColor()); } } } } public void chromaKey(Picture newBack){ Pixel currPixel = null; Pixel newPixel = null; // loop through columns for (int x = 0; x < this.getWidth(); x++) { // loop through rows for (int y = 0; y < this.getHeight(); y++) { currPixel = this.getPixel(x, y); // using the distance difference if ( (currPixel.colorDistance(new Color(110, 180, 20)) < 95.0) || (currPixel.colorDistance(new Color(25, 90, 10)) < 35.0)) { newPixel = newBack.getPixel(x, y); currPixel.setColor(newPixel.getColor()); } /* // using RGB comparisons if(currPixel.getGreen() > (currPixel.getRed() + currPixel.getBlue())) { newPixel = newBack.getPixel(x, y); currPixel.setColor(newPixel.getColor()); } */ } } } public void createFlowerCollage(){ Picture source1Picture = new Picture(FileChooser.getMediaPath("flower1.jpg")); Picture source2Picture = new Picture(FileChooser.getMediaPath("flower2.jpg")); int targetBottomY = this.getHeight() - 5; // copy source1Picture to 0, targetBottomY - height this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 0, targetBottomY - source1Picture.getHeight()); // copy source2Picture to 100, targetBottomY - height this.copy(source2Picture, 0, 0, source2Picture.getWidth(), source1Picture.getHeight(), 100, targetBottomY - source2Picture.getHeight()); // negate the source1Picture source1Picture.makeNegative(); // copy negated source1Picture to 200 this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 200, targetBottomY - source1Picture.getHeight()); // clear the blue from source 2 picture source2Picture.clearBlue04(); // copy source2Picture to 300 this.copy(source2Picture, 0, 0, source2Picture.getWidth(), source2Picture.getHeight(), 300, targetBottomY - source2Picture.getHeight()); // copy negated source1Picture to 400 this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 400, targetBottomY - source1Picture.getHeight()); } public void createCollage(){ // choose three pictures for collage Picture source1 = new Picture(FileChooser.getMediaPath("scb_monifire_small.jpg")); Picture source2 = new Picture(FileChooser.getMediaPath("scb_sababique_small.jpg")); Picture source3 = new Picture(FileChooser.getMediaPath("scb_moniafr_small.jpg")); // set bottom border for first set of pictures int targetBottomY = this.getHeight() - 150; // copy source1 to position using the copy method this.copy(source1, 0, 0, source1.getWidth(), source1.getHeight(), 0, targetBottomY - source1.getHeight()); // copy source2 to position using the copy method this.copy(source2, 0, 0, source2.getWidth(), source2.getHeight(), 200, targetBottomY - source2.getHeight()); // copy source3 to position using the copy method this.copy(source3, 0, 0, source3.getWidth(), source3.getHeight(), 400, targetBottomY - source3.getHeight()); // change non-edges to white source2.edgeDetectRight(20); // copy source2 to new position using the copy method this.copy(source2, 0, 0, source2.getWidth(), source2.getHeight(), 600, targetBottomY - source2.getHeight()); // make source 1 grayscale source1.makeGrayscale(); // copy source1 to new position using the copy method this.copy(source1, 0, 0, source1.getWidth(), source1.getHeight(), 800, targetBottomY - source1.getHeight()); // make a negative copy of row of pictures source1.makeNegative(); // flip pictures upside down this.mirrorHorizontal(); } public void drawSun(){ Graphics g = this.getGraphics(); g.setColor(Color.YELLOW); g.fillOval(120, 30, 30, 30); } public void drawString(){ Font labelFont = new Font("TimesRoman", Font.BOLD, 48); Font normalFont = new Font("Arial", Font.PLAIN, 12); Graphics g = this.getGraphics(); g.setFont(labelFont); g.setColor(Color.GREEN); g.drawString("Sababu Barashango", 10, this.getHeight()-10); } public static void main(String[] args) { /* // Change the red, green, blue color values Picture p = new Picture(FileChooser.getMediaPath("scb_njeri.jpg")); p.explore(); p.changeColor(0.5, 2.0, 1.0); p.explore(); // save new edited picture p1.write(FileChooser.getMediaPath("scb_logo_mirror_diagonal.jpg")); // Create a diagonal mirror Picture pict = new Picture(FileChooser.getMediaPath("scb_logo_square.jpg")); pict.explore(); pict.mirrorSquareDiagonal(); pict.explore(); pict.write(FileChooser.getMediaPath("logo_square_mirror.jpg")); // save new edited picture p1.write(FileChooser.getMediaPath("scb_color_change_final.jpg")); // Copy one picture to another Picture pict = new Picture(FileChooser.getMediaPath("7inX95in.jpg")); pict.explore(); pict.copyKatie(); pict.explore(); String picFile = FileChooser.pickAFile(); Picture p = new Picture(picFile); p.explore(); // Edge Detection String file = FileChooser.getMediaPath("logo.jpg"); Picture p = new Picture(file); p.explore(); p.edgeDetectRight(10); p.explore(); // Swap Background Picture p1 = new Picture(FileChooser.getMediaPath("kid-in-frame.jpg")); Picture oldBack = new Picture(FileChooser.getMediaPath("bgframe.jpg")); Picture newBack = new Picture(FileChooser.getMediaPath("passionFlower.jpg")); p1.swapBackground(oldBack, newBack); p1.explore(); // Chroma Key Background Picture p1 = new Picture(FileChooser.getMediaPath("scb_greenscreen.jpg")); Picture newBk = new Picture(FileChooser.getMediaPath("brickwall.jpg")); p1.explore(); p1.chromaKey(newBk); p1.explore(); // save new edited picture p1.write(FileChooser.getMediaPath("scb_greenscreen_brick_final.jpg")); // Collage String file = FileChooser.getMediaPath("1000x300.jpg"); Picture p = new Picture(file); p.explore(); p.createCollage(); p.repaint(); // save new edited picture p.write(FileChooser.getMediaPath("scb_collage_final.jpg")); // Draw a Sun on an image String file = FileChooser.getMediaPath("beach.jpg"); Picture p = new Picture(file); p.explore(); p.drawSun(); p.explore(); // save edited image p.write(FileChooser.getMediaPath("beach_sun.jpg")); */ // Draw Text String file = FileChooser.getMediaPath("kitten.jpg"); Picture p = new Picture(file); p.explore(); p.drawString(); p.explore(); // save edited image p.write(FileChooser.getMediaPath("kitten_text.jpg")); } } // this } is the end of class Picture, put all new methods before this