<< Chapter < Page | Chapter >> Page > |
Revised: Sun Apr 03 11:21:18 CDT 2016
This page is included in the following Books:
This module contains review questions and answers keyed to the module titled Java3012: Using Nested Loops to Process Pixels .
The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.
True or False? The code shown in Listing 1 will transform the image shown in Figure 1 into the image shown in Figure 2 .
Listing 1 . Question 1. |
---|
/*File Java3012ra Copyright 2008 R.G.Baldwin
Revised 02/14/13*********************************************************/
public class Java3012ra{public static void main(String[] args){new Java3012raRunner().run();
}//end main method}//end class Java3012ra
//======================================================//class Java3012raRunner{
public Java3012raRunner(){System.out.println("Display your name here.");
}//end constructor//----------------------------------------------------//
public void run(){Picture pix = new Picture("Java3012ra.jpg");
//Call the mirrorUpperQuads method to modify the top// half of the picture.
pix = mirrorUpperQuads(pix);//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();
}//end run//----------------------------------------------------////This method mirrors the upper-left quadrant of a
// picture into the upper-right quadrant.private Picture mirrorUpperQuads(Picture pix){
Pixel leftPixel = null;Pixel rightPixel = null;
int midpoint = pix.getWidth()/2;int width = pix.getWidth();
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 loop
return pix;}//end mirrorUpperQuads
//----------------------------------------------------////This method mirrors the top half of a picture into
// the bottom half.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 Java3012raRunner |
Figure 1 - Image from the file named Java3012ra.jpg.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?