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. * * @author Barbara Ericson */ 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() 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 for copy-constructor */ public Picture(Picture p) { super(p); } /** * 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); } ////////////////////// 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() + " width " + getWidth() + " height " + getHeight(); return output; } /** * Matt Kretchmar * April 1, 2006 * This method reduces the number of available colors by performing * a posterization of the image. The original picture is unchanged, the * posterized image is returned. The numColors parameter specifies the * number of color values. */ public Picture posterize ( int numColors ) { // get dimensions int width = getWidth(); int height = getHeight(); // create new picture Picture ret = new Picture(width,height); // get array of pixels for original Pixel originalArray[] = getPixels(); // get array of pixels for new picture Pixel retArray[] = ret.getPixels(); int binSize = (int)(256 / (double)numColors + 0.5); // loop to process color in each picture for ( int i = 0; i < retArray.length; i++ ) { int red = originalArray[i].getRed(); red = red / binSize * binSize + binSize/2; red = bound(red,0,255); retArray[i].setRed(red); int green = originalArray[i].getGreen(); green = green / binSize * binSize + binSize/2; green = bound(green,0,255); retArray[i].setGreen(green); int blue = originalArray[i].getBlue(); blue = blue / binSize * binSize + binSize/2; blue = bound(blue,0,255); retArray[i].setBlue(blue); } return ret; } /** * Matt Kretchmar * April 1, 2006 * A method to create and return a new picture in which all the colors * have been swapped. * * New Image Calling Image * red <- blue * green <- red * blue <- green */ public Picture rotateColors ( ) { // get dimensions int width = getWidth(); int height = getHeight(); // create new picture Picture ret = new Picture(width,height); // get array of pixels for original Pixel originalArray[] = getPixels(); // get array of pixels for new picture Pixel retArray[] = ret.getPixels(); // loop to process color in each picture for ( int i = 0; i < retArray.length; i++ ) { int red = originalArray[i].getRed(); int green = originalArray[i].getGreen(); int blue = originalArray[i].getBlue(); retArray[i].setRed(blue); retArray[i].setGreen(red); retArray[i].setBlue(green); } return ret; } /** * Matt Kretchmar * April 1, 2006 * A method to ensure an input number falls within a range specified * by the lower and upper bounds. */ public int bound ( int input, int lower, int upper ) { if ( input < lower ) input = lower; if ( input > upper ) input = upper; return input; } } // end of class Picture put all new methods before this last parenthesis