| Homework #4 | CMSC 131 |
| Due July 8th, 3:00 pm | Object-Oriented Programming I |
| Type of Homework: Closed | Summer 2004 |
In this homework will practice implementation of picture classes.
Overview
So far you have use classes that allow you to manipulate images. In this homework you will design and implement several image processing classes, similar to the ones presented in previous homeworks. This homework will be graded as follows:
Specifications
A computer picture (sometimes called an image) is stored as a two-dimensional array of "pixels" - which are little bits of color. Computers represent these (and other) colors with a combination of "pure" red, green and blue colors. It turns out that by varying the amount of red, green and blue in any one spot, you can create what appears to the human eye as any of a wide range of colors. In normal pictures, colors are represented with many different possible values of red, green and blue. The smoothly varying levels of these color components allow for realistic-looking images. But, if you restrict the number of values of red, green and blue, you can make the pictures look unusual. If, for example, you turn off all the red and blue colors, you'll be left with a green-looking picture as you have seen in class.
The array of pixels representing an picture must be "addressable". This means there must be a way to tell the computer which pixel is where in the picture. Each picture has a width and a height. These correspond to the number of pixels wide and high the picture is. Computers count pixels in rows and columns from the top-left starting at 0. This means that the top-left pixel is in the 0th row (its y value equals 0) and 0th column (its x value equals 0). Column numbers increment by one as you move to the right all the way to the value width-1, and row numbers increment by one as you move down to height-1.
How Pixels are Managed
Let's look at how pixels are managed. Each picture is composed of three color components: red, green and blue. In our library, each color component of our pixels are represented by a floating point number in the range 0.0 (meaning black) and 1.0 (meaning bright color).
Now, let's look at the actual classes in the library and their role::
You should read the documentation for these classes. In addition, you should check out the code distribution for this homework. Several of the classes in the description below, can be found in the code distribution.
The Picture interface is key as that defines a new picture. It specifies three methods that any picture must implement. The getWidth()and getHeight()methods specify the dimensions of the picture, and getColor()specifies the color of each pixel defined for the picture, based on its dimensions. When you create a class that implements the Picture interface, you are promising that you will implement those three methods. Then, when you pass your picture into, for example, the PictureFrame.show() utility, those three methods will be called to determine the dimensions and contents of the picture. Thus, your definition of these three methods completely determine the contents of the picture.
Let's look at some of the examples. RedSquare is extremely simple. It has a fixed dimension, and every pixel is defined to be red. RedBlackSquare is a little more interesting. It has some internal variables that specify the picture dimensions as well as the size of the red inner square. Then, the getColor()method uses the (x, y) values (which specify which pixel is being asked about) to determine whether the pixel should be red or black. Finally, if you look at FlipLeftRight, you'll see that it has a constructor that stores an input picture, and uses that picture to specify it's own dimensions as well as to generate the new picture (by taking pixels from a different position from the input picture).
For this homework, you must write five classes. Each should create a picture by implementing the Picture interface. The classes must work as follows:
public UpsideDown(Picture basePicture)
public Rotate90Degrees(Picture basePicture)
public Offset(Picture basePicture, int newXOffset, int newYOffset)
public Blend(Picture basePicture1, Picture basePicture2)
Requirements
Sample Run
The following represents the result of executing the driver (DriverHw4.java). Keep in mind this is just an example and not the only scenario your classes are expected to handle.
For an extra challenge, you can use your creativity and create your own picture generation class. Please note that this challenge problem is optional, and no extra credit will be given for it. If you do work on this, you should submit a class named MyPicture along with the other files associated with this homework.
For this challenge problem, you should build on what we've learned in class and the code you've already written to create your own picture based on an input picture. Your constructor should have the signature:
public MyPicture(Picture basePicture)
You must also include a short comment that describes what this picture does and how it works. This comment should appear immediately before the class definition and should be of the form:
/**
* MyPicture implements a picture that <explain what it does here>.
* It works by <explain how it works here>.
*/
public void class MyPicture implements Picture {
// Your code here
}
Submission
Submit your project using the submit project option associated with Eclipse. Remember to complete your time log before submitting your homework. You must submit all the classes associated with your homework, including the supporting classes (e.g., PictureUtil) representing the picture infrastructure. (This is what should happen automatically when you use the 'submit' option in Eclipse.)