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; } /* -------------------- Session 5 Methods -------------------- */ public void clearBlue() { Pixel[] pixelArray = this.getPixels(); for (int i = 0; i < pixelArray.length; i++ ) pixelArray[i].setBlue( 0 ); } public void negate() { int intensity = 0; int red, green, blue; Pixel[] pixelArray = this.getPixels(); for (int i = 0; i < pixelArray.length; i++ ) { red = pixelArray[i].getRed(); green = pixelArray[i].getGreen(); blue = pixelArray[i].getBlue(); pixelArray[i].setColor( new Color( 255-red, 255-green, 255-blue ) ); } } public void makeGrayscale() { int intensity = 0; int red, green, blue; Pixel[] pixelArray = this.getPixels(); for (int i = 0; i < pixelArray.length; i++ ) { red = pixelArray[i].getRed(); green = pixelArray[i].getGreen(); blue = pixelArray[i].getBlue(); intensity = (red + blue + green) / 3; pixelArray[i].setColor( new Color( intensity, intensity, intensity ) ); } } public void blackAndWhiteNegative() { this.makeGrayscale(); this.negate(); } public void addBlueBack() { Pixel[] pixels = this.getPixels(); for (int i = 0; i < pixels.length; i++ ) { int red = pixels[i].getRed(); int green = pixels[i].getGreen(); pixels[i].setBlue( (red + green) / 2 ); } } public void allDimensionsAsRed() { for ( Pixel p : this.getPixels() ) { int red = p.getRed(); p.setGreen( red ); p.setBlue( red ); } } // This version of allDimensionsAsRed() uses a standard // for loop in place of the "for each pixel" loop above. // // public void allDimensionsAsRed() // { // Pixel[] pixels = this.getPixels(); // for (int i = 0; i < pixels.length; i++) // { // Pixel p = pixels[i]; // int red = p.getRed(); // p.setGreen( red ); // p.setBlue( red ); // } // } public void lighter() { Pixel[] pixels = this.getPixels(); for ( int i = 0; i < pixels.length; i++ ) { Pixel p = pixels[i]; p.setRed ( (int) (p.getRed () * 1.1) ); p.setGreen( (int) (p.getGreen() * 1.1) ); p.setBlue ( (int) (p.getBlue () * 1.1) ); } } // This version of lighter() lets the sender of the message // control the factor used to brighten the image. // // public void lighter( double amount ) // { // Pixel[] pixels = this.getPixels();; // double factor = 1 + amount; // // for ( int i = 0; i < pixels.length; i++ ) // { // Pixel p = pixels[i]; // p.setRed ( (int) (p.getRed () * factor) ); // p.setGreen( (int) (p.getGreen() * factor) ); // p.setBlue ( (int) (p.getBlue () * factor) ); // } // } } // end of class Picture, put all new methods before this