import java.awt.*; import java.awt.font.*; import java.awt.geom.*; 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); } ////////////////////// 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 changeRed(double percentChange) { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int value = 0; int index = 0; //loop through all the pixels while(index < pixelArray.length) { // get the currrent pixel pixelObj = pixelArray[index]; //get the red value value = pixelObj.getRed(); // change the red value value =(int)( value* percentChange); // set the pixel red pixelObj.setRed(value); // increment the index index = index + 1; } } public void clearBlue() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int index = 0; //loop through all the pixels while(index < pixelArray.length) { // get the currrent pixel pixelObj = pixelArray[index]; // set the pixel blue pixelObj.setBlue(0); // increment the index index = index + 1; } } public void makeSunset() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int blue = 0; int green = 0; int index = 0; //loop through all the pixels while(index < pixelArray.length) { // get the currrent pixel pixelObj = pixelArray[index]; blue=pixelObj.getBlue(); pixelObj.setBlue ((int)(0.6 * blue)); green=pixelObj.getGreen(); pixelObj.setGreen ((int)(0.6 * green)); // increment the index index = index + 1; } } public void clearBlueWithForLoop() { Pixel pixelObj = null; //get the array of pixels Pixel[] pixelArray = this.getPixels(); // loop through all the pixels for (int i = 0; i< pixelArray.length; i++) { System.out.println(i); //get the current pixel pixelObj = pixelArray[i]; //set the blue on the pixel to ) pixelObj.setBlue(0); } } public void makeSunsetWithForLoop() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int blue = 0; int green = 0; //loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { System.out.println(i); // get the currrent pixel pixelObj = pixelArray[i]; blue=pixelObj.getBlue(); pixelObj.setBlue ((int)(0.6 * blue)); green=pixelObj.getGreen(); pixelObj.setGreen ((int)(0.6 * green)); // increment the index i = i ++; } } //end of class Picture, put all new methods before this public void negatePic() { 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 grayscalePic() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int intensity = 0; //loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { //get the current pixel pixelObj = pixelArray[i]; // commute the average intensity intensity = (int) ((pixelObj.getRed() + pixelObj.getGreen() + pixelObj.getBlue())/3); //set the pixel color pixelObj.setColor(new Color(intensity, intensity, intensity));} } public void grayscaleWithLuminance() { Pixel[] pixelArray = this.getPixels(); Pixel pixelObj = null; int intensity = 0; //loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { //get the current pixel pixelObj = pixelArray[i]; // commute the average intensity intensity = (int) ((pixelObj.getGreen() * 0.587 + pixelObj.getRed() * 0.2999 + pixelObj.getBlue() * 0.114)); //set the pixel color pixelObj.setColor(new Color(intensity, intensity, intensity));}} public void lighten() { Pixel pixelObj = null; Color colorObj = 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 pixel at the x and y location pixelObj = this.getPixel(x,y); //get the current color colorObj = pixelObj.getColor(); // get a light color colorObj = colorObj.brighter(); ///set the pixel color to the ligher color pixelObj.setColor(colorObj); }} public void clearBlueNested() { 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 pixel at the x and y location pixelObj = this.getPixel(x,y); // set the pixel blue pixelObj.setBlue(0); } }} public void mirrorVertical() { Pixel fromPixel = null; Pixel toPixel = null; int midPoint = this.getWidth ()/2; for ( int y = 0; y < this.getHeight(); y++) { for ( int x = 0; x < midPoint; x++) { fromPixel = this.getPixel (x,y); toPixel = this.getPixel (this.getWidth() - 1 - x, y); toPixel.setColor (fromPixel.getColor()); } } } public void mirrorHorizontal() { Pixel fromPixel = null; Pixel toPixel = null; int midPoint = this.getHeight ()/2; for ( int x = 0; x < this.getWidth(); x++) { for ( int y = 0; y < midPoint; y++) { fromPixel = this.getPixel (x,y); toPixel = this.getPixel (x, this.getHeight() - 1 - y); toPixel.setColor (fromPixel.getColor()); } }} public void copyKatie() { 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 colr sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } public void copy(Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; //loop through the columns for (int sourceX = 0, targetX = 100; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { //loop through the rows for (int sourceY = 0, targetY =100; sourceY < sourcePicture.getHeight(); sourceY++, targetY ++) { //set the target pixel color to the source pixel colr sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } public void copyKatiesFace1() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; //loop through the columns for (int sourceX = 70, targetX = 100; sourceX < 135; sourceX++, targetX++) { // loop through the rows for (int sourceY = 3, targetY = 100; sourceY < 80; 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 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 < sourcePicture.getWidth() && tx < this.getWidth(); x++, tx++) { // loop through the y values for (int y = startY, ty = targetStartY; y < endY && y < sourcePicture.getHeight() && ty < this.getHeight(); y++, ty++) { //copy the soucre color to the color sourcePixel = sourcePicture.getPixel(x,y); targetPixel = this.getPixel(tx,ty); targetPixel.setColor(sourcePixel.getColor()); }}} } public void copyKatiesFace() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; //loop through the columns for (int sourceX = 70, targetX = 100; sourceX < 135; sourceX++, targetX++) { // loop through the rows for (int sourceY = 3, targetY = 100; sourceY < 80; 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 copyKatie2() { String sourceFile= FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); this.copy(sourcePicture, 0, 0, sourcePicture.getWidth(), sourcePicture.getHeight(), 0, 0); } public void copyLeftRotation(Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++) { //loop through the rows for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++) { //set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(sourceY, sourcePicture.getWidth() - 1 - sourceX); targetPixel.setColor(sourcePixel.getColor()); }} } public void copyrightRotation() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX, targetY; // loop through the columns for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++) { //loop through the rows for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++) { //set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX, sourceY); targetX = sourcePicture.getHeight() - 1 - sourceY; targetY = sourceX; targetPixel = this.getPixel (targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } public void copyrightRotation2(Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; int targetX, targetY; // loop through the columns for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++) { //loop through the rows for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++) { //set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX, sourceY); targetX = sourcePicture.getHeight() - 1 - sourceY; targetY = sourceX; targetPixel = this.getPixel (targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); }}} public void copyFlowerSmaller() {Picture flowerPicture = new Picture( FileChooser.getMediaPath("passionFlower.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < flowerPicture.getWidth(); sourceX +=2, targetX++) { //loop through the rows for (int sourceY = 0, targetY = 0; sourceY < flowerPicture.getHeight(); sourceY += 2, targetY++) { sourcePixel = flowerPicture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } public void copyscaleUp() {Picture flowerPicture = new Picture( FileChooser.getMediaPath("flower1.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (double sourceX = 0, targetX = 0; sourceX < flowerPicture.getWidth(); sourceX += 0.5, targetX += 1) { //loop through the rows for (double sourceY = 0, targetY = 0; sourceY < flowerPicture.getHeight(); sourceY +=0.5 , targetY+= 1) { sourcePixel = flowerPicture.getPixel((int)sourceX, (int) sourceY); targetPixel = this.getPixel((int)targetX, (int)targetY); targetPixel.setColor(sourcePixel.getColor()); }}} public void copyScaleUp() { Picture flowerPicture = new Picture( FileChooser.getMediaPath("flower1.jpg")); Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (double sourceX = 0, targetX = 0; sourceX < flowerPicture.getWidth(); sourceX += 0.5, targetX += 1) { //loop through the rows for (double sourceY = 0, targetY = 0; sourceY < flowerPicture.getHeight(); sourceY +=0.5 , targetY+= 1) { sourcePixel = flowerPicture.getPixel((int)sourceX, (int) sourceY);}}} public void copyScaleUp1(Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (double sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX += 0.5, targetX += 1) { //loop through the rows for (double sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY +=0.5 , targetY+= 1) { sourcePixel = sourcePicture.getPixel((int)sourceX, (int) sourceY);} } } public void createCollage() { Picture source1Picture = new Picture (FileChooser.getMediaPath("flower1.jpg")); Picture source2Picture = new Picture (FileChooser.getMediaPath("flower2.jpg")); int targetBottomY = this.getHeight() - 5; int targetX = 0; //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(), source2Picture.getHeight(), 100, targetBottomY - source2Picture.getHeight()); //negate the picture source1Picture.negatePic(); //copy negated source1Picture to 200 this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 200, targetBottomY - source1Picture.getHeight()); //copy sourcePicture2 to 300 this.copy(source2Picture, 0, 0, source2Picture.getWidth(), source2Picture.getHeight(), 300, targetBottomY - source2Picture.getHeight()); //copy source1Picture to 0, targetBottomY - height this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 400, targetBottomY - source1Picture.getHeight());} public void createCollage2() { Picture source1Picture = new Picture (FileChooser.getMediaPath("butterfly2.jpg")); Picture source2Picture = new Picture (FileChooser.getMediaPath("eiffel.jpg")); Picture source3Picture = new Picture (FileChooser.getMediaPath("waikiki.jpg")); int targetBottomY = this.getHeight() - 5; int targetX = 0; //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(), source2Picture.getHeight(), 480, targetBottomY - source2Picture.getHeight()); //negate the picture source3Picture.negatePic(); //copy negated source3Picture to 200 this.copy(source3Picture, 0, 0, source3Picture.getWidth(), source3Picture.getHeight(), 600, targetBottomY - source3Picture.getHeight()); } public void createCollage3() { Picture source1Picture = new Picture ("h:\\southbeach.jpg"); Picture source2Picture = new Picture ("h:\\southbeach.jpg"); int targetBottomY = this.getHeight(); int targetX = 0; //copy source1Picture to 0, targetBottomY - height this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 0, targetBottomY - source1Picture.getHeight()); //negate the picture source1Picture.negatePic(); //copy source2Picture to 100 targetBottomY - height this.copy(source2Picture, 0, 0, source2Picture.getWidth(), source2Picture.getHeight(), 100, targetBottomY - source2Picture.getHeight() - 5); //negate the picture source1Picture.negatePic(); //copy source1Picture to 100 targetBottomY - height this.copy(source1Picture, 0, 0, source1Picture.getWidth(), source1Picture.getHeight(), 200, targetBottomY - source1Picture.getHeight()); //copy source2Picture to 100 targetBottomY - height this.copy(source2Picture, 0, 0, source2Picture.getWidth(), source2Picture.getHeight(), 400, targetBottomY - source2Picture.getHeight()); } 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 edgeDetection(double limit) { Pixel bottomPixel = null; Pixel topPixel = null; double distance; for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight() - 1; y++) { topPixel = this.getPixel (x,y); bottomPixel = this.getPixel (x, y + 1); Color bottomColor=bottomPixel.getColor(); distance = topPixel.colorDistance(bottomColor); if (distance <= limit) { topPixel.setColor(Color.white); } else { topPixel.setColor(Color.black); } }}} public void edgeDetection1(double limit) { Pixel rightPixel = null; Pixel leftPixel = null; double distance; for (int x = 0; x < this.getWidth() - 1; x++) { for (int y = 0; y < this.getHeight(); y++) { leftPixel = this.getPixel (x,y); rightPixel = this.getPixel (x + 1 , y); Color bottomColor=rightPixel.getColor(); distance = leftPixel.colorDistance(bottomColor); if (distance <= limit) { leftPixel.setColor(Color.white); } else { leftPixel.setColor(Color.black); } }}} public void sepiaTint() { Pixel pixelObj = null; double redValue = 0; double greenValue = 0; double blueValue = 0; //first change the current picture to grayscale this.grayscalePic(); //loop through the pixels for (int x = 0; x < this.getWidth(); x++) { for(int y = 0; y < this.getHeight(); y++) { //get the current pixel and color values pixelObj = this.getPixel(x,y); redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); //tint the shadows darker if (redValue < 60) { redValue = redValue * 0.9; greenValue = greenValue * .09; blueValue = blueValue * .09; } //tint the midtones a light brown by reducing the blue else if (redValue < 190) { blueValue = blueValue *0.8; } //tint the highlights a light yellow //by reducing the blue else { blueValue = blueValue * 0.9; } //set the colors pixelObj.setRed((int) redValue); pixelObj.setGreen((int) greenValue); pixelObj.setBlue((int) blueValue); } } } public void posterise() { Pixel pixelObj = null; double redValue = 0; double greenValue = 0; double blueValue = 0; //first change the current picture to grayscale this.grayscale(); //loop through the pixels for (int x = 0; x < this.getWidth(); x++) { for(int y = 0; y < this.getHeight(); y++) { //get the current pixel and color values pixelObj = this.getPixel(x,y); redValue = pixelObj.getRed(); greenValue = pixelObj.getGreen(); blueValue = pixelObj.getBlue(); if (redValue < 64) { redValue = 31; } else if (redValue < 128) { redValue = 95; } else if (redValue < 192) { redValue = 159; } else { redValue = 223; } if (blueValue < 64) { blueValue = 31; } else if (blueValue < 128) { blueValue = 95; } else if (blueValue < 192) { blueValue = 159; } else { blueValue = 223; } if (greenValue < 64) { greenValue = 31; } else if (greenValue < 128) { greenValue = 95; } else if (greenValue < 192) { greenValue = 159; } else { greenValue = 223; } //set the colors pixelObj.setRed((int) redValue); pixelObj.setGreen((int) greenValue); pixelObj.setBlue((int) blueValue); } } } 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