<< Chapter < Page Chapter >> Page >

Darken the background of the snow scene

Listing 4 calls the method named darkenBackground to make all of the pixels darker in the snow scene except for those in thelocation of the ellipse as shown in Figure 4 . The pixels at the location of the ellipse are given a red tint.

Listing 4 - Darken the background of the snow scene.
darkenBackground(hole,snowScene);

Put the run method on temporary hold

I will put the explanation of the run method on hold at this point and explain the method named darkenBackground .

The method named darkenBackground

The method named darkenBackground receives references to two Picture objects as parameters. It uses the first picture as a pattern from which it determines which pixels in the second picture (destination) should be darkened.

The pattern and destination images

In this case, a cropped version of the image of the black ellipse shown in Figure 2 is the pattern. The cropped image of the snow scene shown in Figure 6 is the destination image whose pixels will be darkened.

Assumptions

The darkenBackground method assumes that the pattern image has a pure green background as shown in Figure 2 . It also assumes that the pattern and the destination have the same dimensions.

Behavior of the method

The darkenBackground method darkens every pixel in the destination that is at the location of a green pixel in the pattern.

The method applies a red tint to every pixel in the destination that is at the location of a non-green pixel in the pattern

Beginning of the darkenBackground method

The darkenBackground method begins in Listing 5 .

Listing 5 - Beginning of the darkenBackground method.
private void darkenBackground(Picture pattern, Picture dest){Pixel[] patternPixels = pattern.getPixels();Pixel[] destPixels = dest.getPixels();Color color = null; int red = 0;int green = 0; int blue = 0;

Get two arrays of pixel data

The darkenBackground method begins by calling the getPixels method on each of the picture objects to create a pair of array objects containing pixel data.

You learned how to use the getPixels method in an earlier module. Recall that this approach is useful when you don't need to be concerned aboutthe locations of the pixels in an x-y coordinate system.

Arrays have the same length

Because the two pictures have the same dimensions, the two arrays have the same length.

A given array index specifies pixel data from the same location in both pictures.

Beginning of the processing loop

The method uses a for loop to traverse the two arrays of pixel data in parallel, using information from the pattern picture to make the color changes to the destination picture described above. The for loop begins in Listing 6 .

Listing 6 - Beginning of the processing loop.
for(int cnt = 0;cnt<patternPixels.length;cnt++){ color = patternPixels[cnt].getColor(); if(color.equals(Color.GREEN)){//Darken corresponding pixel in the destination. color = destPixels[cnt].getColor(); destPixels[cnt].setColor(color.darker());

Behavior of the processing loop

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