TWiki> CS111 Web>CS111Fall2015>AssignmentsF15>Lab8f15 (2015-10-26, Main.troy)

## CS 111 - Program Design I, Fall 2015

### Lab 8

In class, we discussed an example (Lect1022b.java) that created the mirror image of a picture. This example shows the basic template for most position modification algorithms.

Also recall the code from the example (Lect1022b.java) that calculated the modified x and y coordinates based from the original x and y coordinates is as follows:

`      // determine the location in the result of the mirrored pixel`
`       int modX;`
`       int modY;`
`       modX = width - 1 - x;`
`       modY = y;`

The above formulas are very close to the formulas needed for rotating a picture by 90 degrees, 180 degrees, and 270 degrees.

For this lab, you are to write one program that will prompt the user to enter if an integer value from 1 to 3. When the use enters:

1. - rotate an image 90 degrees in a clockwise direction.
2. - rotate an image 180 degrees in a clockwise direction.
3. - rotate an image 270 degrees in the clockwise direction ( which is the same as rotating the image 90 degrees in a counter-clockwise direction).
An example of a rotated image is shown below.
Here is the original image: Here is the same image rotated 90 degrees clockwise: Here is the same picture rotated 180 degrees: Here is the same picture rotated 270 degrees clockwise: Some key issues to think about are

• What is the size of the new rotated image based on the width and height of the original image? If you get a coordinate out of bounds errors, most likely, you did NOT do the following:
• for the 90 degree rotation and the 270 degree rotation: the original width becomes the rotated height, and the original height becomes the rotated width
• for the 180 degree rotation: the rotated width and height remain the same as the original width and height.
• What are the X and Y coordinates of a pixel in the new rotated images based on the X and Y coordinates of the pixel in the original image?
• for the 90 degree rotation and the 270 degree rotation: the rotated/modified X coordinate is calcutated from the original Y coordinate, and the rotated/modified Y coordinate is calculated from the original X coordinate.
• sometimes the calculation will need to "mirror" (or reverse) the original location (see the code above for the "mirror image" algorithm).
We will need to determine the formulas for the clockwise rotation and the counter clockwise rotation.

Consider the following 5x3 "image", which has width of 5 and height of 3:

 a b c d e f g h i j k l m n o
When rotated clockwise 90 degrees, it will become an picture of width 3 and of height 5:
 k f a l g b m h c n i d o j e
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.
pixel Original X Original Y Rotated X Rotated Y
a
b
c
d
e
f
g
h
i
...
m
n
o
When we fill in this table for the rotated 90 degree clockwise result we get:

pixel Original X Original Y Rotated X Rotated Y
a 0
0
2
0
b 1
0
2
1
c 2
0
2
2
d 3
0
2
3
e 4
0
2
4
f 0
1
1
0
g 1
1
1
1
h 2
1
1
2
i 3
1
1
3
...
m
2
2
0
2
n
3
2
0
3
o 4
2
0
4
We should notice that the Rotated Y values take on the values of the Original X. We should also notice that the Rotated X value take on the "mirrorred location" from the Original Y. Thus the code to modify the positions of the X and Y values in an picture that have been rotated 90 degrees clockwise becomes: (refer back to the original example from lecture to see how this code fits into the overall program)
`      // determine the location for the result of picture rotated 90 degrees clockwise`
`       int modX;`
`       int modY;`
`       modX = height - 1 - y;`
`       modY = x;`

The same approach will need to be done with the image rotated 180 degrees. You should fill in the table yourself to see the relationship of the original X and Y coordinates compared to the rotated X and Y coordinates. The above image would be as follows once rotated 180 degrees:
 o n m l k j i h g f e d c b a

The same approach will also need to be done with the image rotated 270 degrees clockwise. The above image would be as follows once rotated:

 e j o d i n c h m b g l a f k

### Prompting the User for Which Rotation to Do

To get the user's input, we will use the getIntNumber() method of the SimpleInput class. The SimpleInput class is one of the classes in the bookClasses directory. This method will display a "pop-up" window that will prompt the user to enter an integer value.

The getIntNumber() has two ways it can be used. In either way, it takes a String parameter will be displayed as a message when the user is prompted for the input. That is the only parameter for the first way that getIntNumber() can be used. The other way also takes two more parameter which specify the range the number enter by the user must be in. The following code shows both of these ways.

 `// declare in integer variable to store the number entered by the user. int value; // prompt the user for the number value = SimpleInput.getIntNumber ("Please enter an integer value."); // display the value entered by the user System.out.println ("The user entered: " + value); // prompt the user for a number within a range of values value = SimpleInput.getIntNumber ("Please enter an integer value between 1 and 10.", 1, 10); // display the value entered by the user System.out.println ("The user entered: " + value);`
For this lab, you can use either form of the getIntNumber() method that you want. It probably makes a lot more sense to use second version of the method. The lower number in the range should be one and the upper number in the range should be 3. Thus the user can only enter values of 1, 2 or 3. This will save us from having to verify the number entered by the user.

### Lab Assignment 8

Due: Wednesday, 10/28/2015 by 11:59 pm

Write a Java program named NetIDLab8.java that will do the following:

2. Allow the user to select a picture from a file stored on the local machine using pickAFile ().
3. Prompt the user for an integer value of 1, 2 or 3 using getIntNumber ().
4. If the user enters the value of 1, call a method that will create and return an image that has been rotated 90 degrees clockwise
5. If the user enters the value of 2, call a method that will create and return an image that has been rotated 180 degrees
6. If the user enters the value of 3, call a method that will create and return an image that has been rotated 270 degrees clockwise
7. Display the rotated image
8. Save the rotated image to a file on the local machine

9. The code that does the rotation must be written in 3 different methods. One method for each different rotation.These methods are to take the original picture as a parameter and return the rotated picture. I.E. you are to name these three methods:
1. rotate90
2. rotate180
3. rotate270
10. You must write your programs using good programming style which includes:

• Good variable names
• in-line commenting
• header block commenting for the program and each method written
Be sure to include the following with the header block comment for the program.
• day and time of your CS 111 lab section (i.e. Monday at 2:00)
• A description of the project.
• proper indentation of program statements
• use of blank lines to separate blocks of code.
NOTE: The code submitted for this lab must only access each pixel once for the rotation. I.E. You are not allowed to execute the code from rotate90 three times for the rotate270 code.

### How assignments should be submitted

You are to submit your program electronically using the link for Lab 8 on the Assignments Page in Blackboard.

-- Main.troy - 2015-10-26

Topic revision: r1 - 2015-10-26 - 02:23:42 - Main.troy

 Copyright 2016 The Board of Trustees of the University of Illinois.webmaster@cs.uic.edu WISESTHelping Women Faculty AdvanceFunded by NSF  