CS 101 - Intro to Computing, Spring 2010

Lab 8

For this lab, you are to write one program that will rotate an image 90 degrees in a clockwise direction and one program that will rotate an image 90 degrees in a counter-clockwise direction. To do this, you need to take the color from the pixels from the original image and store them into a diferent location in a new image.

An example of a rotated image is shown below.
Here is the original image:

Here is the same image rotated 90 degrees clockwise:

In class, we were creating a new picture that had the same height and width as our original picture. In this case the X,Y coordinate of a pixel in the original picture corresponded to the same X,Y coordinate position in the new picture. That is not the same here.

Some key issues to think about are:

We will need to determine the formulas for the clockwise rotation and the counter clockwise rotation.

Consider the following 5x3 "image":

abcde
fghij
klmno
When rotated clockwise 90 degrees, it will become:
kfa
lgb
mhc
nid
oje
We can (hopefully) notice a pattern between the original X and Y positions and the rotated X and Y positions by inspecting the following summary once it is filled in with the pixel position information. The table has been started to be filled in, but you should fill in the rest of it.
pixelOriginal XOriginal YRotated XRotated Y
a 0 0  2 0
b 1 0  2 1
c 2 0  2 2
d 3 0  2 3
e    
f    
g    
h    
i    
j    
k    
l    
m    
n    
o    

You should notice in the rotated clockwise 90 degrees result that the Rotated Y values equal the Original X values while the Rotated X values correspond to the Original Y values. The exact formula for this is left up to you.

The same will need to be done with the image rotated 90 degrees in the counter-clockwise direction. The above image would be as follows once rotated:

ejo
din
chm
bgl
afk
Below is the table that need to be completed that shows the corresponding X,Y coordinate positions. It has been started to be filled in, you should complete it. Again, hopefully we will notice a pattern between the X and Y coordinates once the table had been completed. As with the clockwise rotation, the Rotated X values will correspond with the Original Y values and the Rotated Y values will correspond with the Original X values.
pixelOriginal XOriginal YRotated XRotated Y
a 0 0  0 4
b 1 0  0 3
c 2 0  0 2
d 3 0  0 1
e    
f    
g    
h    
i    
j    
k    
l    
m    
n    
o    

Lab Assignment 8

Due: Thursday 3/11/2010 by 11:59 pm

Write two Java programs that will do the following:

  1. Program 1: Rotate 90 Clockwise

    1. The program is to be named: Lab8a.java
    2. Print out your name and your net-id
    3. Allow the user to select a picture from a file stored on the local machine
    4. Create an image that has been rotated 90 degrees in the clockwise direction.
    5. Display the modified image
    6. Save the modfied image to a file on the local machine

  2. Program 2: Rotate 90 Counter Clockwise

    1. The program is to be named: Lab8b.java
    2. Print out your name and your net-id
    3. Allow the user to select a picture from a file stored on the local machine
    4. Create an image that has been rotated 90 degrees in the counter clockwise direction.
    5. Display the modified image
    6. Save the modfied image to a file on the local machine

  3. You must write your programs using good programming style which includes:

  4. You are also to submit the Java file electronically via the following steps:

    How assignments should be submitted

    1. Go to the blackboard site for the class
    2. Select "Assignments" on the left bar
    3. Locate the correct assignment to submit; click on "View/Complete Assignment" at the bottom of that assignment
    4. Next to "Attach local file", click "Choose file", choose the file you want to submit; please submit only ONE file, properly named.
      For example for lab2, name it Lab2yournetid.java, for example if your Net ID is sfranz3, the filename would be Lab2sfranz3.java
      Please only submit source code file (the .java file, not the .class). Also, if you have any comment about your program, please write it down in the same file; please do NOT write in the "Comments" field on the submission page (this will go into a different file and will be easily missed).
    5. Hit "Submit"
    6. Go back once again to View/Complete assignment and make sure that your file was submitted; also, this page will show your grade and comments (if any) after assignments are graded.

NOTE: The code submitted for this lab must only access each pixel once for the rotation. You are not allowed to execute the code from rotate90clockwise three times for the rotate90CounterClockwise program or vise-versa.