<< Chapter < Page Chapter >> Page >
Listing 5 . Mirror pixel colors around the midpoint.
for(int row = 0;row<pix.getHeight()/2;row++){ for(int col = 0;col<midpoint;col++){ leftPixel = pix.getPixel(col,row);rightPixel = pix.getPixel(width-1-col,row); rightPixel.setColor(leftPixel.getColor());}//end inner loop }//end outer loopreturn pix; }//end mirrorUpperQuads

Iterate on rows and columns

The outer loop in Listing 5 iterates down through each of the rows in the top half of the image.

The inner loop iterates across the left half of each row, copying the color of the pixels from the left half to the corresponding mirror-image pixels on theright half.

Note that the getPixel method that is called in Listing 5 is different from the getPixels method that was explained in several earlier pages in this book. You can easilyfind those references by entering the term getPixels in the search box at the top of this page that reads "Search this book." I will leave it as an exercise for the student to go to the documentation and learn thedifference between these two methods.

Return a reference to the modified object

Finally, Listing 5 returns a reference to the modified Picture object. The reference is assigned to the variable named pix in Listing 3 .

Superfluous but self-documenting code

Returning and storing a reference to the modified picture is superfluous and unnecessary. The code in Listing 3 already has a reference to the picture and that reference doesn't change just because theobject to which it refers is modified.

However, I prefer this programming style because I consider it to be more self-documenting.

Remainder of the run method

Returning now to the run method, Listing 6 calls the method named mirrorHoriz to mirror the top half of the image into the bottom half. (I will explain the mirrorHoriz method shortly.)

Listing 6 . Remainder of the run method.
//Mirror the top half into the bottom half. pix = mirrorHoriz(pix);//Add your name and display the output picture.pix.addMessage("Display your name here.",10,20);pix.explore();return pix; }//end run

Display text on the image

Then Listing 6 calls the addMessage method on the reference to the picture to place the text near the upper-left corner as shown in Figure 2 .

Display the modified image

After that, Listing 6 calls the explore method to display the modified image as shown in Figure 2 .

Return a reference to the modified picture

Finally, Listing 6 returns the reference to the modified picture, which is saved in the variable named pic in Listing 1 .

As mentioned earlier, the variable named pic is passed to the println method in Listing 1 , causing the second line of text shown in Figure 3 to be displayed on the command line screen.

The method named mirrorHoriz

Listing 7 shows the method named mirrorHoriz in its entirety. This method mirrors the top half of the picture into the bottom half.

Listing 7 . The method named mirrorHoriz.
private Picture mirrorHoriz(Picture pix){ Pixel topPixel = null;Pixel bottomPixel = null;int midpoint = pix.getHeight()/2; int height = pix.getHeight();for(int col = 0;col<pix.getWidth();col++){ for(int row = 0;row<midpoint;row++){ topPixel = pix.getPixel(col,row);bottomPixel = pix.getPixel(col,height-1-row);bottomPixel.setColor(topPixel.getColor()); }//end inner loop}//end outer loop return pix;}//end mirrorHoriz //----------------------------------------------------//}//end class Prob01Runner

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