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 clearBlue() { Pixel pixelObj = null; // loop through all the pixels for (int y=0; y < this.getHeight(); y++) { for(int x= 0; x< this.getWidth(); x++) { pixelObj = this.getPixel (x,y); // set the blue on the pixel to 0 pixelObj.setBlue(0); } } } public void mirrorHorizontal() { int mirrorPoint = this.getWidth(); 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/2); } } } 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 = 73, targetX = 100; sourceX < 130; 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()); } } } /** *Method to copy part of the source picture tothe current picture. * @param sourcePicture the picture to copy from *@param startx the x at the upper left corner to start copying * from in the source picture (inclusive) *@param starty the y at the upper left corner to start copying from in the source picture (inclusive) * @param endX the x at the bottom right corner (exclustive) * @param endy the y at the botoom 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 in the target (this picture) */ public void copy(Picture sourcePicture, int targetStartX, int targetStartY) { this.copy(sourcePicture,0,0,sourcePicture.getWidth(), sourcePicture.getHeight(), targetStartX, targetStartY); } 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()); } } } public void edgeDetection(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 top and bottom pixel topObj = getPixel(x,y); bottomObj = getPixel(x,y+1); //if the dist between the colors is greater // that the passed dist set it to black if (topObj.colorDistance(bottomObj.getColor())>dist) { topObj.setColor(Color.BLACK); } else { topObj.setColor(Color.WHITE); } } } } public void chromakey (Picture newBack) { Pixel currPixel = null; Pixel newPixel = null; // loop through the columns for (int x=0; x+currPixel.getRed() +currPixel.getGreen()) { currPixel.setColor(newPixel.getColor()); } } } } public void negate() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int redValue, blueValue, greenValue = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) { // get the current pixel pixel = pixelArray[i]; // get the current red, green, and blue values redValue = pixel.getRed(); greenValue = pixel.getGreen(); blueValue = pixel.getBlue(); // set the pixel's color to the new color pixel.setColor(new Color(255 - redValue, 255 - greenValue, 255 - blueValue)); } } public void createHistoryCollage() { Picture source1Picture = new Picture(FileChooser.getMediaPath("bp.jpg")); Picture source2Picture = new Picture(FileChooser.getMediaPath("rw.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(), source2Picture.getHeight(), 100,targetBottomY - source2Picture.getHeight()); // negate the source1Picture source1Picture.negate(); // 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.clearBlue(); // 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()); } /** * Method to mirror around a vertical line in the middle * of the picture based on the width */ public void mirrorVertical() { int width = this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; // loop through all the rows for (int y = 0; y < getHeight(); y++) { // loop from 0 to the middle (mirror point) for (int x = 0; x < mirrorPoint; x++) { leftPixel = getPixel(x, y); rightPixel = getPixel(width - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); } } } public static void main(String[] args) { String file = FileChooser.getMediaPath("640x480.jpg"); Picture p = new Picture(file); p.show(); p.createHistoryCollage(); p.mirrorVertical(); p.repaint(); // Picture markP= new Picture(FileChooser.getMediaPath("blue-mark.jpg")); //Picture newBack = new Picture(FileChooser.getMediaPath("moon-surface.jpg")); //markP.chromakey(newBack); //markP.explore(); // markP.write(FileChooser.getMediaPath("wallace.jpg")); } } // this } is the end of class Picture, put all new methods before this