<< 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

Questions & Answers

I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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