In this lab you will create new Picture objects to perform more advanced image manipulations. You will also use Scanner to take input from the keyboard.
Open the file for the Picture class, Picture.java and SAVEAS Picture.java in YOUR LAB09 FOLD ER! For this first part you will create a method in the Picture class called scaleUp(int num Times). This method will enlarge the photo to numTimes times its original size. This should be fairly easy, since this method is given in the book. Refer to section 5.2.5, specifically program 32 on pages 166167 for help.
Note that this method is different from the ones you have created before in that it has a return value. Instead of modifying the current picture, this method creates a new one and returns it. Because of this the way you use the method is also different. Refer to the code on page 167. Try creating anoth er Picture object and saving the result of the method to that. Then you can show both the original and the scaled version to compare. Try your method on other images as well. Be sure you under stand the behavior of this method before moving forward.
BE MODERN AND SMART!!! Make it return a blank picture of the right length and width, TEST IT, and THEN add the scaling code: DON'T TRY TO GET IT RIGHT ALL AT ONCE!!!
public Picture scaleUp(int nTimes)
{ Picture target = new Picture( this.getWidth()*nTimes,
this.getHeight()*nTimes);
return target;
}
Create a main method just as you did last lab, only this time we’ll put it in a different class. Instead of adding that method to Picture.java, click the “New” button in DrJava and write it in the empty window. You’ll have to declare a class around that method by starting to write: class SomeName {, where YOU make up the name of the class. When you save that file, DrJava will fill in the class name you did choose with ".java" appended to the name.
Let’s make your program more interactive by using a Scanner. A Scanner scans input from people. It al lows you to parse input from the user via the keyboard. We will use it to input first the filename , and then, the number of times to scale. First off, you need to include Scanner from the appropriate Java library pack age. Put the following line at the very top of your file with your main method:
import java.util.Scanner;
Now you can use Scanners in your program. First you need to create a new Scanner object. Put the fol lowing line at the beginning of your main method:
Scanner sc = new Scanner(System.in);
Most of this line should look familiar to you. (Everything but System.in) From left to right:
Now let’s take the input. First off we will get the filename. Since all images are stored in the same folder and with the same extension, you can just take the short name. Try this:
System.out.print("File: ");
String fileShortName = sc.next();
String fileName = "/usr/local/depts/cs/geintro/mediasources/" +
fileShortName + ".jpg";
// + applied to Strings means concatenate or glue them together.
Scanner uses whitespace as the default delimiter. This will take in everything typed, up to some whitespace (space or return), and put it in the String fileShortName.
Next let’s get the number of times to scale up (numTimes). Since this needs to be an integer, a String won’t do. Fortunately, Scanner helps us out with its method nextInt(). Try this:
System.out.print("Scale: ");
int numTimes = sc.nextInt();
Now just plug these values for fileName and numTimes into the code you already have in your main method and you’re all set. When you run your program, an example session might look like this:
> java CLASSNAME
File: rose
Scale: 3
>