<< Chapter < Page Chapter >> Page >

I will discuss this program in fragments. A complete listing of the program is provided in Listing 8 near the end of the module.

The driver class named Prob01

The driver class containing the main method is shown in Listing 1 .

Listing 1 . The driver class named Prob01.
public class Prob01{ public static void main(String[]args){ Picture pic = new Prob01Runner().run();System.out.println(pic); }//end main method}//end class Prob01

There is nothing in Listing 1 that I haven't explained in earlier modules.

The println statement in Listing 1 causes the second line of text to be displayed in Figure 3 .

The constructor for the class named Prob01Runner

The constructor for the class named Prob01Runner is shown in Listing 2 .

Listing 2 . The constructor for the class named Prob01Runner .
class Prob01Runner{ public Prob01Runner(){System.out.println("Display your name here."); }//end constructor

The code in Listing 2 simply causes the first line of text in Figure 3 to be displayed on the command line screen.

Beginning of the method named run

The code in the driver class in Listing 1 instantiates a new object of the Prob01Runner class and immediately calls the run method belonging to that object. The run method begins in Listing 3 .

Listing 3 . Beginning of the method named run.
public Picture run(){ Picture pix = new Picture("Prob01.jpg");//Display the input picture.pix.explore(); //Call the mirrorUpperQuads method to modify the top// half of the picture. pix = mirrorUpperQuads(pix);

A new Picture object

Listing 3 instantiates a new Picture object from an image file and saves a reference to that object in the local variablenamed pix .

Display the Picture object

Then Listing 3 calls the explore method on the reference producing the output image shown in Figure 1 .

Modify top half of the picture

Finally, Listing 3 calls the method named mirrorUpperQuads to mirror the upper-left quadrant of the picture into the upper-right quadrant. A copy of a reference to the picture object is passed tothe method and the value returned by the method is saved in the variable named pix . ( I will have more to say about this later.)

Put the explanation of the run method on hold

I will put the explanation of the run method on hold temporarily and explain the method named mirrorUpperQuads .

Beginning of the mirrorUpperQuads method

The beginning of the mirrorUpperQuads method is shown in Listing 4 .

Listing 4 . Beginning of the mirrorUpperQuads method.
private Picture mirrorUpperQuads(Picture pix){ Pixel leftPixel = null;Pixel rightPixel = null;int midpoint = pix.getWidth()/2; int width = pix.getWidth();

Note that the method receives a copy of a reference to the picture.

Declare working variables

The code in Listing 4 begins by declaring a pair of local working variables of type Pixel . These variables will be used to hold information about individual pixels.

Compute width and midpoint of the image

Then Listing 4 computes and saves the width and the horizontal midpoint of the image.

Mirror pixel colors around the midpoint

Listing 5 uses a pair of nested for loops to copy the pixel colors on the left of the midpoint to corresponding mirror-image pixels on the right sideof the midpoint.

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