<< 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

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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