Computer Science 1026 Topic 9.docx

3 Pages
152 Views
Unlock Document

Department
Computer Science
Course
Computer Science 1026A/B
Professor
Brian Langen
Semester
Fall

Description
Topic 9: More on For Loops Copying and Transforming Pictures  Previously, images were altered by changing the color intensities of pixels in an image.  Images can be altered differently, by copying the color intensity of pixels in a ‘source image’ to those of a ‘target image’  To do this, both the source picture’s and target picture’s ‘x’ and ‘y’ values are used.  Target Picture: o An existing picture or a blank picture can be used as the target picture. o Creating a new blank picture: Picture targetPic = new Picture(x,y); For Loop - Review  General Syntax: o For (start; check; change) { Body of Loop }  Start: More than one loop variable can be initialized, separated by commas  Check: Only one test can exist  Change: More than one loop variable can be changed, separated by commas Copy Picture Method  The source picture will be passed as a parameter, and the target picture will invoke the method.  Algorithm: o Loop through the pixels of the source picture o Get the source and target pixels (the two do not necessary have to have the same initial value) o Set the color intensities of the target pixel to the color intensity of the source pixel public void copyPicture (Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } Copy Picture To Method  Changes: o Two new parameters, xStart and yStart, are added o Respective x and y starting position at the target pixel is changed to xStart and yStart o For loop is changed in the case that the source picture does not fit into the target picture public void copyPictureTo (Picture sourcePicture, int xStart, int yStart) { Pixel sourcePixel = null; Pixel targetPixel = null; for (int sourceX = 0, targetX = xStart; (sourceX < sourcePicture.getWidth()) && (targetX < this.getWidth()); sourceX++, targetX++) { for (int sourceY = 0, targetY = yStart; ((sourceY < sourcePicture.getHeight()) && (targetY < this.getHeight()); sourceY++, targetY++)
More Less

Related notes for Computer Science 1026A/B

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit