CS 123 - Programming Assignment 6

In this assignment you will write a method for the Picture class that mirrors a picture across a diagonal. Download this zip file and un-zip it. In the zip file there is a copy of this web page, which you can read in your browser. This assignment is due Wednesday, October 28.

Add a method called mirrorDiagonal() to the Picture class. The full declaration of this method is

  public Picture mirrorDiagonal()
Notice that this method is declared to return a Picture. In the textbook, this idea is described starting in the last paragraph on page 165 up to the bottom of page 167 (in particular, Program 32).

Here is how you would use this method to mirror a picture and capture the returned image.

> Picture p = new Picture("C:/cs123/mediasources/pictures/butterfly1.jpg")
> Picture p2 = p.mirrorDiagonal()
> p2.show()
The first line creates a Picture object. The second line calls the mirrorDiagonal() method on that picture object. Since the method returns another object (which holds the mirrored image; the original object should be unchanged), we use a new reference variable p2 to refer to the new object returned by the method. The third line has this new object display itself. Here is what the new object should look like.
mirrored butterfly

Here is a more detailed explanation of what the mirrorDiagonal() method should do. If a picture is not square we will chop off part of the picture to make it square (which makes it a lot easier to find the diagonal of the picture). If height and width are the dimensions of our picture, then we will create a new picture that is a square with the dimension computed by

  int size = Math.min(height,width);
We create the new (empty) square Picture object with a line like
  Picture p = new Picture(size, size);
This new Picture object will (eventually) hold the mirrored image that this method should return. The method should copy the upper right triangle of the original picture into the upper right triangle of this new picture. Suppose we start with the butterfly1.jpg image.
butterfly
Here is what the upper triangle of this image looks like.
half butterfly
To mirror this part of the image into the empty part of the picture, we need to copy the pixel data from every vertical line starting at a diagonal point to a horizontal line going out of the same diagonal point.
half butterfly
Here is what the mirrored image will look like after all of the copying is done.
mirrored butterfly

Write a program called TestDiagonal.java that tests your mirrorDiagonal() method. Your test program's main() method should open the picture file C:\cs123\mediasources\pictures\butterfly1.jpg, send the mirrorDiagonal() message to the picture object and save the result as the file testresult1.jpg. Then have your test program create the following image and save the resulting picture in the file testresult2.jpg. You can use any of the other mirroring methods that we did in class to help you create this image.

kaleidoscope

Turn in a zip file called CS123Hw6Surname.zip containing your Picture.java file, your TestDiagonal.java file, a file called mirrorDiagonal.java that contains just your mirrorDiagonal() method, and your two resulting image files testresult1.jpg and testresult2.jpg. This assignment is due Wednesday, October 28.


Return to the main homework page.
Return to the CS 123 home page.


compliments and criticisms