<< Chapter < Page Chapter >> Page >

The loop begins by getting the color value of the next pixel in the pattern array. If the color of the pattern pixel is green, the code in Listing 6 :

  • Gets the color from the corresponding pixel in the destination array.
  • Calls the method named darker , which is a method of the Color class in the standard Sun library, to produce a darker version of the pixelcolor.
  • Replaces the pixel color in the destination array with the darker version of the color.

Compare images to see the results

If you compare Figure 4 with Figure 3 , you will see that (ignoring the skater and the ellipse) , all of the pixels in Figure 4 are darker versions of the colors in Figure 3 .

The brighter method

The Color class also provides a method named brighter which has the opposite effect. In particular, it can be used to brighten the color of apixel.

These two methods are very useful for making a pixel darker or brighter without having to know anything about the actual color of the pixel.

The else clause in the processing loop

If the color of the pattern pixel (tested in Listing 6 ) is not green, the code in the else clause in Listing 7 is executed.

Listing 7 - The else clause in the processing loop.
}else{ //Apply a red tint to the corresponding pixel in// the destination. color = destPixels[cnt].getColor(); red = color.getRed();if(red*1.25<255){ red = (int)(red * 1.25);}else{ red = 255;}//end else green = (int)(color.getGreen() * 0.8);blue = (int)(color.getBlue() * 0.8); destPixels[cnt].setColor(new Color(red,green,blue)); }//end else}//end for loop}//end darkenBackground

The snow scene and the ellipse only...

At this point, only the images from Figure 2 (the ellipse) and Figure 3 (the snow scene) are being processed. The image of the skater in Figure 1 hasn't entered the picture yet.

Application of the ellipse pattern

The code in Listing 7 is executed only if the pixel from the destination picture is at a location that matches one of the non-green (black) pixels in the ellipse in Figure 2 .

(The fact that the pixel is black is of no consequence. The only thing that matters is that it is not green.)

The objective of the else clause

The objective is to modify the pixel color in the destination picture at this location to give it a red tint as shown in Figure 4 .

Get, save, and modify the red color value

Listing 7 begins by getting the color of the pixel from the current location in the destination picture. It extracts and saves thered color value of the pixel. Then, depending on the current value of the red color value, it either:

  • Multiplies the red color value by a factor of 1.25, or
  • Sets the red color value to the maximum allowable value of 255.

Decrease the color values for blue and green

Following this, it gets the green and blue color values and multiplies each of them by 0.8.

Replace the old pixel color with the new color

Finally, it replaces the pixel color with a new color using the modified values of red, green, and blue.

The texture is preserved

As you can see in Listing 4 , this process causes the pixels in locations that match the ellipse to take on a red tint, but thetexture of the image is not destroyed as it would be if the pixels had simply been replaced by pixels that all have the same color of pink.

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