<< Chapter < Page | Chapter >> Page > |
There are two commonly used ways to specify a rectangular area in programming. One way is to specify the coordinates of the upper-left andbottom right corners. This is the approach used in the cropAndFlip method in Listing 4 .
The other way is to specify the coordinates of the upper-left corner and then to specify the width and the height. This is the approach used in the copyPictureWithCrop method in Listing 7 .
The width and height parameters
The parameters named width and height in Listing 7 specify the width and height of the rectangular area of pixels that is to be preserved when the source picture is cropped.
If you compare the width and height parameter values passed in Listing 6 with the coordinate values passed in Listing 3 , you will see that the same rectangular area of the butterfly image is being preserved after cropping in both cases.
Confirm that the cropped image will fit
Listing 7 begins by confirming that the cropped rectangular area of the source picture will fit within the destination picturewhen placed at the specified location. If the conditional clause of the if statement returns true, then the code in the body of the statement will be executed.If not, control bypasses the body of the if statement and the source picture will not be copied into the destination picture.
Process using nested for loops
As was the case in Listing 4 , Listing 7 declares two working variables named pixel and color .
The variables named pixel and color are used along with various parameter values in the pair of nested for loops shown in Listing 8 to crop the source picture and to copy the cropped source picture into the destination picture at the specified location.
Listing 8 . Process using nested loops. |
---|
for(int col = 0;col<width;col++){
for(int row = 0;row<height;row++){
color = source.getPixel(col + xCoor,row + yCoor).getColor();pixel = dest.getPixel(col+xOff,row+yOff);
pixel.setColor(color);}//end inner loop
}//end outer loop}//end if}//end copyPictureWithCrop method}//end class Prob02Runner |
Not as complicated as it looks
Although the arithmetic operations involved in Listing 8 can be daunting, the code in Listing 8 is doing nothing more than replacing selected pixel colors in the destination picture with selected pixel colors from thesource picture.
Partially complete version of the output picture.
If you were to display the destination picture before returning control back to the run method in Listing 8 , you would see the image shown in Figure 6 .
Figure 6 - Partially complete version of the output picture.
At this point, only one cropped version of the butterfly image has been copied into the beach image.
Return control to the run method
The copyPictureWithCrop method terminates in Listing 8 and returns control to the run method, picking up where Listing 6 left off.
The remainder of the run method
The remainder of the run method is shown in Listing 9 .
Listing 9 . The remainder of the run method. |
---|
copyPictureWithCrop(picC,picB,161,70,0,0,77,101);
picB.explore();Picture[] output = {picA,picB,picC};return output;
}//end run |
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?