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 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 color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } public void copyKatie2() { Picture katiePict = new Picture(FileChooser.getMediaPath("KatieFancy.jpg")); this.copy(katiePict,0,0,katiePict.getWidth(),katiePict.getHeight(),100,100); } public void copyKatieFace() { Picture katiePict = new Picture(FileChooser.getMediaPath("KatieFancy.jpg")); this.copy(katiePict,70,3,135,80,100,100); } 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()); } } } /** * Method to clear the blue from the picture (set * the blue to 0 for all pixels) */ public void clearBlue() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int index = 0; // loop through all the pixels while (index < pixelArray.length) { // get the current pixel pixel = pixelArray[index]; // set the blue on the pixel to 0 pixel.setBlue(0); // increment index index++; } } public void clearRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int index1 = 0; // loop through all the pixels while (index1 < pixelArray.length) { // get the current pixel pixel = pixelArray[index1]; // set the blue on the pixel to 0 pixel.setRed(0); // increment index index1++; } } public void mirrorHorizontal() { int mirrorPoint = this.getHeight() / 2; Pixel topPixel = null; Pixel bottomPixel = null; // loop through the rows for (int y = 0; y < mirrorPoint; y++) { // loop from 0 to just before the mirror point for (int x = 0; x < this.getWidth(); x++) { topPixel = this.getPixel(x, y); bottomPixel = this.getPixel( x, this.getHeight() - 1 -y); bottomPixel.setColor(topPixel.getColor()); } } } public void createCollage() { Picture p1 = new Picture (FileChooser.getMediaPath("JB.jpg")); this.copy(p1,0,0,p1.getWidth(),p1.getHeight(),0,0); p1.clearBlue(); this.copy(p1,0,0,p1.getWidth(),p1.getHeight(),0,150); this.mirrorHorizontal(); Picture p2 = new Picture (FileChooser.getMediaPath("JB and Jaz.jpg")); this.copy(p2,0,0,p2.getWidth(),p2.getHeight(), 400,0); p2.grayscale(); this.copy(p2,0,0,p2.getWidth(),p2.getHeight(),1300,0); Picture p3 = new Picture (FileChooser.getMediaPath("Jordy004.jpg")); this.copy(p3,0,0,p3.getWidth(),p3.getHeight(),750,0); p3.grayscale(); this.copy(p3,0,0,p3.getWidth(),p3.getHeight(),750,500); Picture p4 = new Picture (FileChooser.getMediaPath("JB.jpg")); this.copy(p4,0,0,p4.getWidth(),p4.getHeight(),1025,0); p4.grayscale(); this.copy(p4,0,0,p4.getWidth(),p4.getHeight(),1025,500); } /** * Method to change the picture to gray scale with luminance */ public void grayscaleWithLuminance() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int luminance = 0; double redValue = 0; double greenValue = 0; double blueValue = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixel = pixelArray[i]; // get the corrected red, green, and blue values redValue = pixel.getRed() * 0.299; greenValue = pixel.getGreen() * 0.587; blueValue = pixel.getBlue() * 0.114; // compute the intensity of the pixel (average value) luminance = (int) (redValue + greenValue + blueValue); // set the pixel color to the new color pixel.setColor(new Color(luminance,luminance,luminance)); } } public void sepiaTint() { Pixel pixelObj = null; double redValue = 0; double greenValue = 0; double blueValue = 0; this.grayscaleWithLuminance(); // 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 * 0.9; blueValue = blueValue * 0.9; } // 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 posterize() { Pixel pixelObj = null; double redValue = 0; double greenValue = 0; double blueValue = 0; this.grayscaleWithLuminance(); // 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 < 64) { redValue = 31; } // tint the midtones a light brown by reducing the blue else if (redValue < 128) { redValue = 95; } else if (redValue < 128) { redValue = 95; } else if (redValue < 192) { redValue = 159; } else { redValue = 223; } if (redValue < 64) { redValue = 31; } // tint the midtones a light brown by reducing the blue else if (blueValue < 128) { blueValue = 95; } else if (redValue < 128) { blueValue = 95; } else if (blueValue < 192) { blueValue = 159; } else { blueValue = 223; } if (redValue < 64) { redValue = 31; } // tint the midtones a light brown by reducing the blue else if (greenValue < 128) { greenValue = 95; } else if (greenValue < 128) { greenValue = 95; } else if (greenValue < 192) { greenValue = 159; } else { greenValue = 223; } // tint the highlights a light yellow // by reducing the blue // 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