PICTURE LAB
(Last Q3 Objective)
In this challenge you will modify the pixels of a digital photo.
Take a look at this zeroBlue method:
This method uses nested loops to visit each pixel in a photo (which has red, green, and blue values) and sets all of the blue values to 0.
Here is the code that you need in order to try this for yourself!
Make sure to "fork" the program so that you can edit and run the code.
There are several pictures already uploaded into this program that you can experiment with (see the pics folder).
Line 8 in the Main class (pic.write("output.jpg")) creates output in the output.jpg file that you can open and see the effect your code had on the image you choose.
Be patient!
Even with a strong connection, it takes approximately 20 seconds for the output.jpg image to update.
We can loop through 2D arrays using nested for loops or nested enhanced for each loops.
The outer loop for a 2D array usually traverses the rows, while the inner loop traverses the columns in a single row.
The 2D array’s length gives the number of rows. A row’s length array[0].length gives the number of columns.
Nested iteration statements can be written to traverse the 2D array in “row-major order” or “column-major order.”
In an enhanced for each loop, the variable of the outer loop must be the type of each row, which is a 1D array. The inner enhanced for loop variable must be the same type as the elements stored in the array.
All standard 1D array algorithms can be applied to 2D array objects.
When applying sequential/linear search algorithms to 2D arrays, each row must be accessed then sequential/linear search applied to each row of a 2D array.
You can find the zeroBlue( ) method in the Picture.java file.
1) Write a keepOnlyBlue( ) method that sets the red and green values to zero, but doesn't change any of the blue values.
2) Write a switchRedGreen( ) method that swaps the red pixels with green pixels.
3) Write a switchRedBlue( ) method that swaps the red pixels with blue pixels.
Hint: for tasks 2) and 3) you will need to use the getRed, getGreen, and getBlue methods to get the current RGB values and then swap them using the setRed, setGreen, and setBlue methods.
4) Write a negate( ) method to negate all the pixels in a picture. To do this, set the red value to 255 minus the current red value, the green value to 255 minus the current green value, and the blue value to 255 minus the current blue value.
5) Write a grayscale( ) method to turn the picture into shades of gray. Set the red, green, and blue values to the average of the current red, green, and blue values (add all three values and divide by 3).
Make sure to meet with Mr. C to show (and explain) all of the new methods you created (tasks 1 - 5).
6) OPTIONAL: If you would like an additional challenge you can find some additional exercises on Runestone!