PIC - Modifying a Picture (Lesson)
Modifying a Picture
Introduction
Pictures are 2-dimensional arrays of pixels. We will be using what we learned about 2-dimensional arrays and how to process them using nested loops. You may want to refer to the array lessons while working with pictures.
Even though digital pictures have millions of pixels, modern computers are so fast that they can process all of them quickly. You will write methods in the Picture class that modify digital pictures. The Picture class inherits from the SimplePicture class and the SimplePicture class implements the DigitalPicture interface.
Below is a UML (Unified Modeling Language) diagram that shows the relationship between the different classes. Each class is shown in a box with the class name at the top. The middle area shows attributes and the bottom area shows methods. The open triangle points to the class that the connected class inherits from. The straight line links show associations between classes. Association is also called a “has-a” relationship. The numbers at the end of the association links give the number of objects associated with an object at the other end.
Interfaces
Click below to learn more about interfaces.
Inheritance Practice
Click below to begin a practice activity.
Color Modification Lab
Add the following methods to the Picture class. They can be added directly after the zeroBlue method. You will need these methods for the final lab project.
1. Write the zeroRed and zeroGreen methods for the Picture class.
2. Write a keepOnlyBlue method that will keep only the blue values, that is, it will set the red and green values to zero.
3. Write keepOnlyRed and keepOnlyGreen methods for the Picture class.
4. Write the negate method to negate all the pixels in a picture. To negate a picture, 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 the 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).
- Be sure to test all the new methods to make sure they work as intended.
IMAGES CREATED BY GAVS