<< Chapter < Page Chapter >> Page >

Call the cropAndFlip method

Then Listing 3 calls the cropAndFlip method passing the reference to the butterfly image of Figure 1 , along with some other information as parameters. The return value is stored in a new localreference variable of type Picture named picC .

Put discussion of the run method on hold

I will put the discussion of the run method on temporary hold at this point and explain the method named cropAndFlip , which begins in Listing 4 .

Beginning of the cropAndFlip method

The cropAndFlip method crops a picture to the specified coordinate values and flips it around a vertical line at its center.

Listing 4 . Beginning of the cropAndFlip method.
private Picture cropAndFlip(Picture pic, int x1,int y1,int x2,int y2){ Picture output = new Picture(x2-x1+1,y2-y1+1);int width = output.getWidth();Pixel pixel = null; Color color = null;

Incoming parameters

In addition to a reference to the picture to be processed, the method receives four incoming integer values as parameters. The parameters named x1 and y1 specify the coordinates of the upper-left corner of a rectangular area of the picture that is to be retained in the output.

The parameters named x2 and y2 specify the coordinates of the lower-right corner of the rectangular area of the picture that is to be retainedin the output.

An empty Picture object

Listing 4 begins by creating an empty Picture object of the correct size to hold the cropped image. A reference to the empty picture is savedin the local reference variable named output .

Then Listing 4 gets and saves the width of the output picture.

Following this, Listing 4 declares two local working variables named pixel (of type Pixel ) and color (of type Color ) .

Process using nested loops

Listing 5 uses a pair of nested for loops to cause the output picture to be a cropped version of the picture received as an incoming parameter.The cropped image is flipped around its center.

Listing 5 . Process using nested loops.
for(int col = x1;col<(x2+1);col++){ for(int row = y1;row<(y2+1);row++){ color = pic.getPixel(col,row).getColor();pixel = output.getPixel(width-col+x1-1,row-y1); pixel.setColor(color);}//end inner loop }//end outer loopreturn output; }//end cropAndFlip method

The code in Listing 5 copies the pixel colors of the selected pixels of the incoming image to the pixels of the output image,flipping the image around its center line in the process.

Cropped and flipped version of the butterfly image

If you display the picture referred to by output in Listing 5 , you will get the image shown in Figure 5 .

Figure 5 - Cropped and flipped version of the butterfly image.

This is an image of the butterfly that has been cropped to eliminate extraneous material and flipped horizontally.

Compare with the original butterfly picture

If you compare Figure 5 with Figure 1 , you will see that pixels on the outer edges of Figure 1 have been discarded and the resulting image has been flipped around its centerline.

End of the cropAndFlip method

Figure 5 returns a reference to the new image and ends the method named cropAndFlip . The returned value is stored in the variable named picC in Listing 3 .

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