[Quick Reference for Turtle methods] [Quick Reference for Picture and Pixel methods]
Use the interactions pane to create a Picture object and show it on the screen. In order to create a Picture object, you need a String that represents the full path of a picture file on the system.
To get such a string, we first need to create a variable to store it. Create a variable whose type is String and whose name is picName.
Next, assign the value returned from the method call FileChooser.pickAFile() to the variable picName. When you execute this line, a file dialog will come up. You should navigate to a file that contains a picture on the system. If you are working on your own machine, any image file will do (smaller is better). If you are working on the computers in the lab (340 or 101), you can find image files here: /eng/class/notes/cse113/intro-prog-java/mediasources.
Finally, create a variable whose type is Picture, whose name is whatever you'd like and whose value is assigned a new Picture object. You need to pass in the string picName as an argument to the picture's constructor.
Now you have a Picture object, but you can't see it yet. You need to call the explore() method on the picture object you've just created in order to see the picture. The explore method opens the picture in a picture explorer that will allow you to zoom and inspect individual pixels of an image for their color values. You can close the explorer once you have seen the picture.
Now, let's change the color value of a particular pixel. Create a variable whose type is Pixel and whose name is whatever you'd like.
Assign it the value that is returned from calling the method getPixel(4,5) on the picture object you've just created.
Now, call the method setColor() on the pixel object passing in java.awt.Color.RED as the argument to the method call.
Explore the picture again to see that the pixel at the coordinate (4,5) has been changed to the color red.
Save the results of this interactions session into a history file named Lab2.hist, and place this file in your Lab2 directory.
You will use some of this code again when you complete the work for weeks 2 & 3 of this assignment.
Open a new file in DrJava and copy the contents of this page into it. Save the file and name it CSE113Turtle.java in the Lab2 directory you created for Lab 1.
Scroll through the file and look for the line that says:
/////////////////// methods ///////////////////////
You will write the following methods after this line in the file.
Write a method named drawMyInitials that has a void return type and takes no parameters.
Write a method named drawSquare that has a void return type and takes a parameter of type int for the size of the side of the square. Use this parameter to draw a square of that size whenever this method is called.
You can test out the functionality of your methods by creating a CSE113Turtle at the prompt of the interactions pane. CSE113Turtles work the same as the Turtles you used last time, but they have the added two methods you just wrote.
Using DrJava, create a new file named CSE113Picture.java. Inside it, put a class definition for a class named CSE113Picture. Define no other parts of the class at first. Save the file and Compile it to ensure that you have no compiler errors.
Add a comment to the beginning of the file that puts your full name indicating that you are the author of this file. Save and compile again to make sure you wrote the comment block correctly.
Create a constructor for this class that when called, creates a file dialog to enable the user to select a file to create a Picture object from. Once the user selects a file, create a Picture object and store it as an instance variable of the class. You will use this instance variable in the rest of the methods in the class. (This repeats work we have done in class already).
Once you have done this, save, compile, and go to the interactions pane and create an instance of your CSE113Picture object. You should be able to select a file and then nothing will happen. We need to create a method in our class to show the picture you just created.
Create a method inside CSE113Picture named show that calls the method show on the picture instance variable. Now, if you save and compile this file and go to the interactions pane, you can create a CSE113Picture object, select a file for the picture, and then call the show() method from the prompt to see the picture you just selected.
Now, you have an object that you can manipulate and use to test the methods you are about to write. (Note that calling some of the methods in a row on the same picture will not allow you to see the results of the methods properly, when testing create a new CSE113Picture object for each method.)
Write a method in the class CSE113Picture named setGreen. This method will take as a parameter an x and y coordinate that represents a pixel location and changes the pixel at that location to green. After you have written this method, you should test it. Note that on pictures of significant size, it will be hard to see the pixel.
Write a method in the class CSE113Picture named makePictureOneColor. This method will take as a parameter a java.awt.Color and change the color of every pixel in the picture to the color passed in as a parameter. This method should also be tested using the interactions pane. Its results will be very easy to see.
Write a method in the class CSE113Picture named blueOnly. This method will change the picture by removing the red and green component of each pixel in the picture and leaving on the blue component, effectively creating a "blue-scale-like" image of the picture.
Write a method in the class CSE113Picture named lighten or darken (you choose). This method will lighten or darken the color in each of the pixels in the picture. There are two methods on Color objects that will help you, they are named brighten and darken. Once you get the current color of the pixel, you can call these methods on the color and then reset the pixel's color to the lighter or darker version.
When you have implemented all the methods described here, you can insert the following main method in your class. Then, you will be able to run the code as its own program without the use of the interactions pane.
public static void main(String[] args) {
CSE113Picture picture = new CSE113Picture();
picture.show();
picture.setGreen(20, 20); //Make sure the picture you choose above is at least 20x20 pixels
picture.show();
picture = new CSE113Picture(); //You'll need to pick a file again, you can use the same one
picture.makePictureOneColor(new java.awt.Color(153, 51, 0));
picture.show();
picture = new CSE113Picture();
picture.blueOnly();
picture.show();
picture = new CSE113Picture();
//picture.lighten(); //Remove comment at beginning of this line if implemented lighten
//picture.darken(); //Remove comment at beginning of this line if implemented darken
picture.show();
}
Submission for this assignment will be different that Lab 1. We will use an entirely web-based system for submission. This means, you do not need to transfer or email your files to yourself if you are working on your home machine. You only need to be able to access the Internet.
You should have received an email indicating a password to a system called Web-CAT. This is the system that you will use to submit your assignments. To log onto webcat, go to this page:
http://web-cat.cse.buffalo.edu:8180//Web-CAT/WebObjects/Web-CAT.woa
Then, type in the Username and Password that were sent to you in the email. You can change your password by selecting the MyProfile link. Strongly consider changing your password into something easy for you to remember as opposed to the randomly-generated passwords that the system provides.
You will see a set of assignments that are currently accepting submissions for our course on the first page after you log in. If you would like to submit, simply hover over the icons to the right of the assignment name and select the icon for uploading the submission. The icon looks like a piece of paper with a world next to it and an up arrow. Once you select this, you will be taken to an upload screen. Select the browse button and find the file you are interested in submitting. Select the file and then click Next. You will need to confirm your submission on the next screen by clicking Next again. You will see a results page indicating if your submission was successful.
You will need to submit each of the three files in this assignment to its appropriate assignment in Web-CAT. You can submit as many times as you would like. We will only grade the last submission you make. You can use Web-CAT to view what submissions you have made to ensure you have sent the correct file.
Your lab submission is due no later than 11:59:59 pm on February 27th. Remember, no late labs will be accepted.
Lab authored by Adrienne Decker
Page maintained by Adrienne Decker
Contact: adrienne@cse.buffalo.edu | 130 Bell Hall | (716)645-3180 x 161