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 8 Methods -------------------- */ // VERSION 1 // This method mirrors Delbert T. Quimby's study, left to right. // public void mirror() // { // int width = this.getWidth(); // for (int x = 0; x < width; x++) // for (int y = 0; y < 184; y++) // { // Pixel source = this.getPixel(x, y); // Pixel target = this.getPixel(width-1-x, y); // // target.setColor( source.getColor() ); // } // } // VERSION 2 // This method mirrors Delbert T. Quimby's study, right to left. // public void mirror() // { // int width = this.getWidth(); // for (int x = 0; x < width; x++) // for (int y = 0; y < 184; y++) // { // Pixel source = this.getPixel(width-1-x, y); // Pixel target = this.getPixel(x, y); // // target.setColor( source.getColor() ); // } // } // VERSION 3 // This method mirrors only the bookcase from x == 300, then... // public void mirror() // { // int width = this.getWidth(); // for (int x = 0; x < 100; x++) // for (int y = 0; y < 184; y++) // { // Pixel source = this.getPixel(width-1-x, y); // Pixel target = this.getPixel(x, y); // // target.setColor( source.getColor() ); // } // // for (int x = 100; x < 125; x++) // for (int y = 0; y < 125; y++) // { // Pixel source = this.getPixel(width-1-x, y); // Pixel target = this.getPixel(x, y); // // target.setColor( source.getColor() ); // } // // for (int x = 125; x < 160; x++) // for (int y = 0; y < 96; y++) // { // Pixel source = this.getPixel(width-1-x, y); // Pixel target = this.getPixel(x, y); // // target.setColor( source.getColor() ); // } // // for (int x = 160; x < 200; x++) // for (int y = 0; y < 82; y++) // { // Pixel source = this.getPixel(width-1-x, y); // Pixel target = this.getPixel(x, y); // // target.setColor( source.getColor() ); // } // } // VERSION 4 // This method mirrors Delbert T. Quimby's study, right to left. // It uses a helper method to mirror rectangular regions. public void mirror() { mirrorRegion( 0, 100, 184 ); mirrorRegion( 100, 125, 125 ); mirrorRegion( 125, 160, 96 ); mirrorRegion( 160, 200, 82 ); } public void mirrorRegion(int startX, int endX, int endY) { int width = this.getWidth(); for (int x = startX; x < endX; x++) for (int y = 0; y < endY; y++) { Pixel source = this.getPixel(width-1-x, y); Pixel target = this.getPixel(x, y); target.setColor( source.getColor() ); } } // This method copies the caption from Delbert T. Quimby's study // into a new Picture. It returns the picture as its answer. public Picture copyCaption() { Picture caption = new Picture( 115, 85 ); Pixel sourcePixel, targetPixel; int xOffset = 20; int yOffset = 85; for (int x = 0; x < caption.getWidth(); x++) { for (int y = 0; y < caption.getHeight(); y++) { sourcePixel = this.getPixel( x + xOffset, y + yOffset ); targetPixel = caption.getPixel( x, y ); targetPixel.setColor( sourcePixel.getColor() ); } } return caption; } } // end of class Picture, put all new methods before this