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; } // ***** Methods for Session 10 ------------------------------------- public Picture scaleUp() { Picture p = new Picture( this.getWidth()*2, this.getHeight()*2 ); for (int x = 0; x < this.getWidth()*2; x++) for (int y = 0; y < this.getHeight()*2; y++) p.getPixel(x,y).setColor( this.getPixel( x/2, y/2 ).getColor() ); return p; } public void mysteryMethod() { int red, green, blue; for ( Pixel p : this.getPixels() ) { red = p.getRed() / 2; blue = p.getBlue() / 2; green = p.getGreen(); green = green - (green - 102)/2; p.setColor( new Color( red, green, blue ) ); } } public void changePurpleToKellyGreen() { Color target = new Color( 163, 124, 143 ); int red, green, blue; for ( Pixel p : this.getPixels() ) { if ( p.colorDistance( target ) < 50 ) { red = p.getRed() / 2; blue = p.getBlue() / 2; green = p.getGreen(); green = green - (green - 124)/5; p.setColor( new Color( red, green, blue ) ); } } } public void eliminateGlareInGlasses() { Color glare = new Color( 200, 179, 158 ); Color flesh = new Color( 182, 122, 70 ); int red, green, blue; Pixel p; for ( int x = 92; x < 104; x++ ) { for ( int y = 87; y < 97; y++ ) { p = this.getPixel( x, y ); if ( p.colorDistance( glare ) < 50 ) p.setColor( flesh ); } } } public void removeRedeye( int startX, int startY, int endX, int endY, Color newColor ) { int red, green, blue; Pixel p; for ( int x = startX; x < endX; x++ ) { for ( int y = startY; y < endY; y++ ) { p = this.getPixel( x, y ); if ( p.colorDistance( Color.red ) < 167 ) p.setColor( newColor ); } } } // public void removeRedeye( int startX, int startY, // int endX, int endY, // int distance, Color newColor ) // { // int red, green, blue; // Pixel p; // // for ( int x = startX; x < endX; x++ ) // { // for ( int y = startY; y < endY; y++ ) // { // p = this.getPixel( x, y ); // if ( p.colorDistance( Color.red ) < distance ) // p.setColor( newColor ); // } // } // } public void detectEdges( double threshold ) { Pixel leftPixel, rightPixel; double leftAverage, rightAverage; double difference; 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 ); leftAverage = leftPixel.getAverage (); rightAverage = rightPixel.getAverage(); difference = Math.abs( leftAverage - rightAverage ); if ( difference < threshold ) leftPixel.setColor( Color.white ); else leftPixel.setColor( Color.black ); } } } // ***** Methods for Session 9 -------------------------------------- public Picture reduce() { Picture p = new Picture( this.getWidth()/2, this.getHeight()/2 ); for (int x = 0; x < this.getWidth()/2; x++) for (int y = 0; y < this.getHeight()/2; y++) p.getPixel(x,y).setColor( this.getPixel( x*2, y*2 ).getColor() ); return p; } public Picture copyRightTriangle( int startX, int startY, int width ) { int lineLength = 0; Picture p = new Picture( this.getWidth(), this.getHeight() ); for (int y = startY; y < startY + width; y++) { lineLength++; for (int x = startX; x < startX + lineLength; x++) p.getPixel(x,y).setColor( this.getPixel( x, y ).getColor() ); } return p; } } // end of class Picture, put all new methods before this