<< Chapter < Page | Chapter >> Page > |
As with Listing 1 , the code in Listing 2 shouldn't require an explanation.
The run method of the Prob05Runner class
Listing 1 calls the run method on an object of the Prob05Runner class. The run method is shown in its entirety in Listing 3 .
Listing 3 - The run method of the Prob05Runner class. |
---|
public void run(){
Picture penguin = new Picture("Prob05a.jpg");Picture hare = new Picture("Prob05b.jpg");
merge(hare,penguin);hare = crop(hare,6,58,330,252);
hare.addMessage("Display your name here.",10,20);drawSun(hare);
hare.show();System.out.println(hare);
}//end run |
The only thing in Listing 3 that I haven't explained in earlier modules is the call to the merge method, so I will limit my discussion to that method.
The merge method
The merge method is used to merge the image in Figure 1 with the image in Figure 2 to produce the image shown in Figure 3 .
(Note, however, that the merged image was cropped to eliminate the buttons at the top of Figure 1 and Figure 2 before displaying it in Figure 3 .)
A linear merge
The merge method does a linear merge on two pictures based on the distance of each pixel from the left side of the picture.
The method assumes that both pictures have the same dimensions.
Beginning of the merge method
The merge method begins in Listing 4 .
Listing 4 - Beginning of the merge method. |
---|
private void merge(Picture left,Picture right){
int width = left.getWidth();int height = left.getHeight();
double scaleL = 0;double scaleR = 0;
int redL = 0;int greenL = 0;
int blueL = 0;int redR = 0;
int greenR = 0;int blueR = 0;
Pixel pixelL = null;Pixel pixelR = null; |
The code in Listing 4 simply declares and initializes a large number of working variables.
Do the merge
The merge is accomplished in the nested for loop in Listing 5 .
Listing 5 - Do the merge. |
---|
for(int row = 0;row<height;row++){
for(int col = 0;col<width;col++){
scaleR = (double)col/width;scaleL = 1.0 - scaleR;
pixelL = left.getPixel(col,row);pixelR = right.getPixel(col,row);
redL = pixelL.getColor().getRed();greenL = pixelL.getColor().getGreen();
blueL = pixelL.getColor().getBlue();redR = pixelR.getColor().getRed();
greenR = pixelR.getColor().getGreen();blueR = pixelR.getColor().getBlue();
redL = (int)(redL*scaleL + redR*scaleR);greenL = (int)(greenL*scaleL + greenR*scaleR);
blueL = (int)(blueL*scaleL + blueR*scaleR);pixelL.setColor(new Color(redL,greenL,blueL));
}//end inner loop}//end outer loop
}//end merge |
Difficult to explain
Although the code in Listing 5 is long, tedious, and ugly, it isn't complicated However, it is somewhat difficult to explain inwords.
Two scale factors
The body of the for loop begins by computing a pair of scale factors named scaleR and scaleL . The factor named scaleR has a maximum value of 1.0 and is directly proportional to the distance of the current pixel from the left edge of thepicture.
The factor named scaleL also has a maximum value of 1.0 and is inversely proportional to the distance of the pixel from the left edge.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?