

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Topics To replace a range of colors with one color in a picture: posterizing. To average nearby pixels: blur To combine Boolean expressions with and and or.
Type in the following code in the Program window (changing the comments): import java.awt.Color; /* Program that transforms pictures using loops and conditionals
for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // get the current pixel and color values pix = sourcePic.getPixel(x,y); // if the distance from white or black is less than passed amount // use the replacement color if (pix.colorDistance(Color.white) < amount || pix.colorDistance(Color.black) < amount) pix.setColor(replacementColor); } } } ii) Type in the following code in the Program window in the main method and before the pic.show(); highlightLightAndDark(sourcePicture, 50.0, Color.yellow); iii) Test your program with different pictures, values for the distance and colors (e.g. butterfly1.jpg, 50, yellow).
6. 5 Combining Pixels: Blurring iv) Type in the following code in the Program window below the main method and before the last }: /** * Method to blur the pixels of the picture * @param pict - picture Object to be modified * @param numPixels - number of pixels to average in all directions */ public static void blur(Picture sourcePic, int numPixels) { Pixel pix = null; Pixel samplePix = null; int width = sourcePic.getWidth(); int height = sourcePic.getHeight(); int redValue = 0; int greenValue = 0; int blueValue = 0; int count = 0; // loop through the pixels for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // get the current pixel and color values pix = sourcePic.getPixel(x,y); // reset the count and red, green, and blue values count = 0; redValue = greenValue = blueValue = 0; // loop through pixel numPixels before x and after x and sum values of colors