<< Chapter < Page Chapter >> Page >

The method named crop

Listing 3 calls the crop method four times in succession, once for each of the four Picture objects instantiated from the image files.

I explained image cropping in an earlier module. The crop method used in this program is very similar to the methods that I explained in theearlier module, so I won't explain it again in this module. You can view the crop method in detail in Listing 6 near the end of the module.

Five incoming parameters

The crop method requires five incoming parameters. The first parameter is a reference to the Picture object that is to be cropped. The remaining four integer parameters specify the rectangular area that is to bepreserved after the picture is cropped.

The rectangular area to be preserved

The first two integers specify the column and row coordinates for the upper-left corner of the rectangular area that is to be preserved. Thelast two integers specify the width and the height of the rectangle that is to be preserved.

Note that in all four cases, the height of the rectangular area that is to be preserved is 256 pixels. This will be important later on with respect toscaling two of the images.

Returns a reference to a cropped picture

The crop method returns a reference to a Picture object that is a cropped version of the picture whose reference is passed to the method.In each case, the code in Listing 3 replaces the reference to the original Picture object with the reference to the cropped Picture object.

Front view of the skater after cropping

If you were to display the Picture object referred to by the variable front after cropping, you would see the image shown in Figure 7 .

Figure 7 - Front view of the skater after cropping.

View of the ice skater facing the camera croppoed to exclude all but the ice skater on a green background.

Transparent pixels

This is the image that appears as the center ice skater in Figure 5 after green-screen processing. Note that all of the green pixels in Figure 7 appear to be transparent in Figure 5 .

Remainder of the run method

Continuing with the run method, Listing 4 calls the method named greenScreenDraw three times in succession to draw the three skater images at specific locations on the snow scene withgreen-screen processing.

Listing 4 . Remainder of the run method.
//Draw the front view of the skater on the snowScene // at full size.greenScreenDraw(front,snowScene,117,0); //Draw the left side view of the skater on the// snowScene at half size. left = left.getPictureWithHeight(256/2);greenScreenDraw(left,snowScene,55,64); //Draw the right side view of the skater on the// snowScene at one-third size. right = right.getPictureWithHeight(256/3);greenScreenDraw(right,snowScene,260,96); //Display students name on the final output and// display it. snowScene.addMessage("Display your name here.",10,15);snowScene.explore(); System.out.println(snowScene);}//end run method

Two skater images are scaled

In two cases in Listing 4 , the method named getPictureWithHeight is called before calling the greenScreenDraw method. The getPictureWithHeight method is used to scale two of the images to a smaller size as shown in Figure 5 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask