<< Chapter < Page Chapter >> Page >

Original image not modified

Note that the code in the cropAndFlip method does not modify the original image of the butterfly. Instead, it extracts pixel data from theoriginal image to produce a new image. When control returns to the run method in Listing 3 , a reference to the new image is stored in the variable named picC .

Call the copyPictureWithCrop method from the run method

Control has now returned to the run method, picking up where Listing 3 left off. The next statement in the run method is shown in Listing 6 .

Listing 6 . Call the copyPictureWithCrop method from the run method.
copyPictureWithCrop(picA,picB,82,70,4,5,77,101);

Put the run method on hold again

Once again, I will put the run method on hold while I explain the method named copyPictureWithCrop , which begins in Listing 7 .

Beginning of the method named copyPictureWithCrop

The first two incoming parameters named source and dest are references to a source picture and a destination picture.

When the method is called in Listing 6 , the source picture is the original butterfly picture shown in Figure 1 and the destination picture is the beach picture shown in Figure 2 .

Listing 7 . Beginning of the method named copyPictureWithCrop.
private void copyPictureWithCrop( Picture source,Picture dest, int xOff,int yOff, int xCoor,int yCoor, int width,int height){ //Confirm that source will fit in destinationif(((width+xOff)<= dest.getWidth())&&((height+yOff)<= dest.getHeight())){Pixel pixel = null; Color color = null;

Copy source to destination

The method named copyPictureWithCrop copies part of the source picture into the destination picture with an offset on both axes after first confirmingthat the part will fit. The method does nothing if the part won't fit.

The copy process causes selected pixel colors in the destination picture to be replaced by pixel colors from the source picture.

The offset values

The next two parameters named xOff and yOff in Listing 7 specify the location in the destination picture where the upper-left corner of the cropped source picture is to be located.

The statement in Listing 6 passes the values (82,70) for these two values. This is the location of the upper left corner of theleft-most butterfly image in Figure 3 .

Not really cropped

For clarity, I will refer to this as a cropped source picture even though the program doesn't actually save a cropped versionof the picture as was the case with the cropAndFlip method.

The program simply copies a rectangular portion of the source picture into the destination picture.

Upper-left cropping corner

The parameters named xCoor and yCoor in Listing 7 specify the upper-left corner of the rectangular area of pixels that is to be preserved when the source image is cropped.

Coordinate values of (4,5) are passed for these two values when the method is called in Listing 6 .

Same values as Listing 3

Note that these are the same two values that were passed for this purpose when the cropAndFlip method was called in Listing 3 .

Two ways to specify a rectangle

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