<< Chapter < Page Chapter >> Page >

Complete program listing

A complete listing of the program discussed in this module is shown in Listing 6 below.

Listing 6 . Complete program listing.
/*File Prob03 Copyright 2008 R.G.Baldwin Revised 12/17/08*********************************************************/ import java.awt.Color;public class Prob03{ public static void main(String[]args){ Prob03Runner obj = new Prob03Runner();obj.run(); }//end main}//end class Prob03 //======================================================//class Prob03Runner{ public Prob03Runner(){//constructorSystem.out.println("Display your name here."); }//end constructor//----------------------------------------------------// public void run(){//Instantiate, display and crop the four input // images. They must be cropped to remove the Alice// runtime window material. The three skater images // are also cropped to remove excess blank green// background. //Note that the three views of the skater are bmp// images instead of jpg images in order to preserve // the pure green background color. Storing the// images as jpg files would corrupt the background // color in the low order bits.//A view facing the front of the skater. Picture front = new Picture("Prob03a.bmp");front.explore(); front = crop(front,123,59,110,256);//A view showing the right side of the skater. Picture right = new Picture("Prob03b.bmp");right.explore(); right = crop(right,123,59,110,256);//A view showing the left side of the skater. Picture left = new Picture("Prob03c.bmp");left.explore(); left = crop(left,123,59,110,256);//This will be the background for the new picture. Picture snowScene = new Picture("Prob03d.jpg");snowScene.explore(); snowScene = crop(snowScene,6,59,344,256);//Draw the front view of the skater on the snowScene // at full size.greenScreenDraw(front,snowScene,117,0); //Draw the left side view of the skater on the// snowScene at half size. left = left.getPictureWithHeight(256/2);greenScreenDraw(left,snowScene,55,64); //Draw the right side view of the skater on the// snowScene at one-third size. right = right.getPictureWithHeight(256/3);greenScreenDraw(right,snowScene,260,96); //Display students name on the final output and// display it. snowScene.addMessage("Display your name here.",10,15);snowScene.explore(); System.out.println(snowScene);}//end run method //----------------------------------------------------////Assumes a source image with a pure green background. // Copies all non-green pixels from the source image to// the destination image at the location explained // below. Note that jpg images typically won't have// a pure green background even if they had a pure // green background before being compressed into the// jpg format. bmp images work well for this. private void greenScreenDraw(Picture source, Picture dest,//Place the upper-left corner // of the source image at the// following location in the // destination image.int destX, int destY){int width = source.getWidth(); int height = source.getHeight();Pixel pixel = null; Color color = null;for(int row = 0;row<height;row++){ for(int col = 0;col<width;col++){ color = source.getPixel(col,row).getColor();if(!(color.equals(Color.GREEN))){ pixel = dest.getPixel(destX + col,destY + row);pixel.setColor(color); }//end if}//end inner loop }//end outer loop}//end greenScreenDraw //----------------------------------------------------////Crops a Picture object to the given width and height // with the upper-left corner located at startCol and// startRow. private Picture crop(Picture pic,int startCol,int startRow, int width,int height){ Picture output = new Picture(width,height);int colOut = 0; int rowOut = 0;int col = 0; int row = 0;Pixel pixel = null; Color color = null;for(col = startCol;col<startCol+width;col++){ for(row = startRow;row<startRow+height;row++){ color = pic.getPixel(col,row).getColor();pixel = output.getPixel(colOut,rowOut); pixel.setColor(color);rowOut++; }//end inner looprowOut = 0; colOut++;}//end outer loop return output;}//end crop }//end class Prob03Runner

-end-

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