Add a method to the Picture class to give the image a vignette effect. See the samples at the end.


Lecture notes on loops, problem solving and pixels; Chapter 4 of the textbook and necessary material from previous chapters.


Create a new method, named vignette, taking no parameters, at the bottom of (just before the final closing curly bracket). The new method must be programmed to do the computations described mostly in English below whose effect is to convert the image into one with the vignette effect applied. Since your method must be an "instance" method (like the draw letter methods you gave to Turtles of Project 1), your code uses the keyword this to get at the Picture to be modified.

The new method MUST be named vignette and take no parameters.

Besides the vignette method, you must re-program the main method so that when the TA runs the program, it (1) brings up the FileChooser and, if an image file is successfully chosen, it (2) makes a Picture from the file and (3) shows the Picture after it was modified by the computer calling your vignette method upon it.

To make a vignette--What to program the computer to compute:

Before the loop

  • The computer must know, more exactly, write down, the width and the height of the Picture
  • From the width and height, she or he can calculate the x and y coordinates of the center point. Just divide the width and the height by 2.0 Let's use variables xC and yC for those coordinates. They should be of type double because dividing an integer by 2 can leave a fractional part.
  • From the coordinates of the center point, he or she must calculate the distance of the center to each corner. Since the center is in the middle, it doesn't matter which corner. So the easiest corner is the one with coordinates (0.0, 0.0), the upper left corner.
  • The distance to that corner is calculated by the Pythagorean Theorem applied to the right triangle shown below. See image.
  • So the calculation is (1) compute the square of each center point coordinate (xC*xC and yC*yC) (2) add those squares and (3) thinking that the sum is S, take the square root returned by Math.sqrt(S) That square root is the maximum distance distMax of all Pixels in the Picture to the center point. All Pixels are within that distance to the center.

The above common computation is done ONLY ONCE, to make a single vignette. You have to arrange that the remaining computations be controlled by a loop. We now describe the computation done once for each Pixel. See lecture notes and Sec. 4.3 for several kinds and applications of loops to Pictures and arrays of Pixels. The only result of the above (common) computation that will be used over and over for each Pixel is distMax's value. Sec. 4.3 also has many examples of taking int values, converting them to double, doing the arithmetic possibly with fractions, and casting the resulting doubles to int with (int) so certain G&E methods accept those parameters.

Inside the loop body

  • The x and y coordinates of the Pixel are needed. A G&E Pixel object has the methods you can use to return these coordinates: Supposing variable pixel refers to the Pixel, calls to those methods are coded by pixel.getX( ) and pixel.getY( ) (Or you can use nested loops to set the two variables x and y. Both ways are explained and applied in the textbook.)
  • The original Color's three intensities (red, green and blue) are needed too. Use method calls pixel.getRed( ), pixel.getGreen( ) and pixel.getBlue( ) to return those three numbers. See image.
  • How the color is changed depends on the distance of the pixel to the center point. Your computer will have to compute that distance using the Pythagorean theorem, again. The sides of the relevant triangle have lengths (x ­ xC) and (y ­ yC). So program the computer to (1) square each of these side lengths (Just multiply each number by itself), (2) add the two squares and (3) compute the square root of the sum. Let's say the he or she wrote that square root onto ticket distPix
  • The key quantity that determines the color change is the ratio of distPix to distMax (actually, their values.) The ratio will range from 0.0 for the center pixel to 1.0 for a corner pixel. So, your computer must use the Java double type of number. For the reason explained below, let's store this ratio as whiteColorWeight = distPix/distMax
  • Now think about how your weighted GPA (grade point average) is computed. Suppose you took only two courses: Course P with say 9 credits and another course Q with say 13 credits. Course P has weight of 9.0/21 or about 0.43 and Course Q's weight is (1.00­0.43) or 0.57 So, if you got a 4.0 (A) in course P and a 3.0 (B) in course Q, your weighted GPA would be (0.43*4.0) + (0.57*3.0). You do not divide this sum by anything because the weights already add up to 1. The familiar non-weighted average of two numbers N1 and N2 is the weighted average with equal weights 0.5: (0.5*N1 + 0.5*N2). Here is the answer: The new red intensity is the weighted average of 255 with weight whiteColorWeight averaged with the original red intensity with weight (1.0­whiteColorWeight) Of course, 255 is the red intensity for color white. The new blue intensity and the new green intensity are separately computed in similar fashion.
  • Remember that after the new red, green and blue intensities are computed, the G&E Pixel methods that change those three intensities must be called. Call those methods like pixel.setRed( ?? ); etc., with the correct variable or expression in place of the ??. These three method calls are of course inside the loop body.
  • And just in case you forget to add 1 to the correct variable or variables for each run of the loop body, it's very helpful to know how to stop DrJava when it's running a program with an infinite loop: Click on "Reset" (just right of the "Compile" button.)

See result. See image.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.