|
Outcomes: Upon
completion of the lab, the student will know how to |
|
|
|
· Use nested loops to
manipulate the pixels in a picture |
|
|
· Use loops to draw figures
in pictures using Turtles |
|
1 |
Login to the lab network using the username and
password assigned to you by the Computer Lab staff. |
|
2 |
Navigate
to Mr. Clark’s website. From there go to the Computer Science I page, then to
the Lecture notes and assignments page. The url is: http://www.wvutech.edu/mclark/CSCI121-2006/cs1_lectures.htm Click
on the link to MyPicture.java and save it to your J: drive. This is not the same version of MyPicture.java
that we used in Lab Assignment 3.
Also click on the links to the files MyTurtle.java and caterpillar.jpg
and save them to your J: drive. |
|
3 |
Start
the DrJava application, and then click the Open button on the toolbar. Navigate to your J: drive and open the
files MyPicture.java and MyTurtle.java. |
|
4 |
In the files
panel, click on MyTurtle.java so that its code appears in the editor
panel. Before the main method of MyTurtle,
you will see the following skeleton of a method definition for drawing a
spiral:
public void
drawSpiral(int initialSize, int numSquares, Color color) this.setPenColor(color); draw numSquares squares as follows: tell turtle to draw a square using
current value of size increment size by 3 */ Add
code to this method to accomplish the tasks specified in the comment. Compile the file and then execute the main
method of MyTurtle. Show the result
of the execution and the code to the lab assistant. Have him/her sign your lab assignment form. |
|
5 |
In the files
panel, click on MyPicture.java so that its code appears in the edit
panel. Before the main method of
MyPicture you will the following skeleton of a method definition: /* */
To draw a circle, let an integer variable range from centerX – radius to centerX + radius and calculate the corresponding values for y. The diagram below will give you an idea about doing the calculations:
From the diagram we see that y = Math.sqrt(r*r – x*x). Associated with each value of x are two values: centerY – y and centerY + y. Use this idea to plot the points using a single for statement.
Once you have added code to this method to accomplish the tasks specified in the comment, compile the file and then execute the main method of MyPicture. Show the result of the execution and the code to the lab assistant. Have him/her sign your lab assignment form. |
|
6 |
In
the MyPicture class, define a new method with header public void flipHorizontal() that
causes a MyPicture object to be flipped horizontally as discussed in the
lecture of September 15 (using nested for
statements and getPixel(x,y).
Change the main method of MyPicture to this: public static void main(String[] args) { String fileName =
FileChooser.pickAFile(); MyPicture p = new MyPicture(fileName); /* put code to test lab assignment here
*/ p.flipHorizontal(); p.show(); } Compile MyPicture and execute the main method. Choose the file caterpillar.jpg. Show your code to the lab assistant and have her/him sign your lab sheet in the appropriate place. |
|
7 |
Optional (for fun!): Add another version of flipHorizontal to your MyPicture
class that flips a rectangular region of a picture horizontally. The header for this function should be: public void flipHorizontal (int x1, int y1, int
x2, int y2) The
region to be flipped is a rectangle with upper left corner at coordinates
(x1, y1) and lower right corner at (x2, y2). Change
the main method of MyPicture to this: public
static void main(String[] args) { String
fileName = FileChooser.pickAFile();
MyPicture p = new MyPicture(fileName); /* put
code to test lab assignment here */
p.flipHorizontal(40, 20, 100, 50);
p.show(); } Compile
MyPicture and execute the main method.
Choose the file caterpillar.jpg. Show your code to the lab instructor and
have her/him sign your lab sheet in the appropriate place. |
|
8 |
Print a copy of your code in MyPicture.java. Give the listing of MyPicture.java and
your completed lab worksheet to the lab instructor. |