public static Picture makeBlackOrWhite (Picture p) { // access the size of the picture int width = p.getWidth(); int height = p.getHeight(); for ( int xIndex = 0 ; xIndex < width ; ++xIndex ) { // do whatever I need to column number xIndex // access each pixel in this column for ( int yIndex = 0 ; yIndex < height ; ++yIndex ) { // access the pixel at coordinate // (xIndex, yIndex) Pixel pix = p.getPixel (xIndex, yIndex); int r, g, b; // access the individual color values from the pixel r = pix.getRed(); g = pix.getGreen(); b = pix.getBlue(); // modify the individual color amounts int grayAmount = (int)(r * 0.299 + g * 0.587 + b * 0.114); // save the modified values back into the pixel if ( grayAmount < 64 ) { pix.setColor (Color.BLUE); } else if ( grayAmount < 128 ) { pix.setColor (Color.GREEN); } else if ( grayAmount < 192 ) { pix.setColor (Color.CYAN); } else { pix.setColor (Color.YELLOW); } } } return p; }Q. 22
public static Picture makeCollage (Picture p1, Picture p2) { int width1 = p1.getWidth(); int height1 = p1.getHeight(); int width2 = p2.getWidth(); int height2 = p2.getHeight(); int height; if (height1 > height2) height = height1; else height = height2; Picture result = new Picture (width1 + width2 , height); // copy the pixels fro, p1 to the result for ( int xIndex = 0 ; xIndex < width1 ; xIndex = xIndex + 1) { // access each pixel in this column for ( int yIndex = 0 ; yIndex < height1 ; yIndex = yIndex + 1 ) { // access the pixel at coordinate // (xIndex, yIndex) Pixel pix1 = p1.getPixel (xIndex, yIndex); Color c1 = pix1.getColor(); Pixel pixR; int xModified = xIndex; int yModified = yIndex; pixR = result.getPixel (xModified, yModified); pixR.setColor (c1); } // end of inner for loop } // end of outer for loop // copy the pixels from p2 to the result for ( int xIndex = 0 ; xIndex < width2 ; xIndex = xIndex + 1) { // access each pixel in this column for ( int yIndex = 0 ; yIndex < height2 ; yIndex = yIndex + 1 ) { // access the pixel at coordinate // (xIndex, yIndex) Pixel pix2 = p2.getPixel (xIndex, yIndex); Color c2 = pix2.getColor(); Pixel pixR; int xModified = xIndex + width1; int yModified = yIndex; pixR = result.getPixel (xModified, yModified); pixR.setColor (c2); } // end of inner for loop } // end of outer for loop return result; } // end of method-- Main.troy - 2014-12-02
Copyright 2016 The Board of Trustees of the University of Illinois.webmaster@cs.uic.edu |
WISEST Helping Women Faculty Advance Funded by NSF | ![]() | ![]() |