<< Chapter < Page Chapter >> Page >
Figure 3 . Output text on the command line screen.
java version "1.6.0_14" Java(TM) SE Runtime Environment (build 1.6.0_14-b08)Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)javac 1.6.0_14 Dick BaldwinPicture, filename Prob02.jpg height 274 width 365

The algorithm

The algorithm required to transform the image from Figure 1 to Figure 2 is:

  • Set the blue color value for every pixel to zero.
  • Invert the red and green color values for every pixel.

A color value is inverted by subtracting the value from 255 and using the difference as the new color value.

Obvious that the blue color value is reduced to zero

It should be obvious to the student when comparing the two images in the PictureExplorer objects that the blue pixel value has been set to zero for every pixel in the modified image.

Color inversion is not quite so obvious

Deducing that the red and green colors in the output pixels are the inverse of the red and green colors in the input image isn't quite as straightforward.However, using algebra to compare several corresponding pixels in the two images should make it obvious fairly quickly to the curious student.

The implementation of the algorithm will be explained below.

Discussion and sample code

Will explain in fragments

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

I will begin with the driver class named Prob02 , which is shown in its entirety in Listing 1 .

Listing 1 . The driver class.
public class Prob02{//the driver class public static void main(String[]args){ Prob02Runner obj = new Prob02Runner();obj.run();System.out.println(obj.getPicture());}//end main }//end class Prob02

You should already be familiar with everything in Listing 1 . The most important aspect of Listing 1 for purposes of this discussion is the call to the run method belonging to the object instantiated from the Prob02Runner class. I will explain the run method shortly.

Beginning of the class named Prob02Runner

The class definition for the class named Prob02Runner begins in Listing 2 .

Listing 2 . Beginning of the class named Prob02Runner.
class Prob02Runner{ private Picture pic = new Picture("Prob02.jpg");public Prob02Runner(){//constructor System.out.println("Dick Baldwin");}//end constructor //----------------------------------------------------////Accessor method public Picture getPicture(){return pic;}

Again, you should be familiar with everything in Listing 2 . I will simply highlight the instantiation of a new Picture object using an image file as input and the saving of a reference to that object in the private instancevariable named pic .

The beginning of the run method

The run method begins in Listing 3 . This is where the action is, so to speak.

Listing 3 . The beginning of the run method.
public void run(){ pic.addMessage("Dick Baldwin",10,20);pic.explore();

You are already familiar with the call to the addMessage method to add my name as text to the image encapsulated in the Picture object. ( See Figure 1 .)

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