TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.

Slides:



Advertisements
Similar presentations
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Advertisements

ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Manipulating Pictures CS1316: Representing Structure and Behavior.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
How to use the Java class libraries Brief documentation of how to do this all with Java.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
ManipulatingPictures-Mod6-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan,
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 10 THE IF STATEMENT 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CSC 112Introduction to Media Computation 1 Rotating Images.
CS1315: Introduction to Media Computation Transforming pictures by index number.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Barbara Ericson Georgia Tech Sept 2005
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Barb Ericson Georgia Institute of Technology Dec 2009
Topic 10 The if statement Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Two-Dimensional Arrays and Nested Loops – part 4
Two-Dimensional Arrays and Nested Loops – part 3
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Nested Loops Circles inside Circles
Manipulating Pictures, Arrays, and Loops
Nested Loops Circles inside Circles
Manipulating Pictures, Arrays, and Loops part 6
Presentation transcript:

TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson.

Outline 2  How to return a value from a method  How to transform a picture into one of a different size Rotation (Scaling up and scaling down)  Returning a Picture object from a method

Return Values from Methods 3  Recall that methods can return values  We have invoked some methods that returned something, for example:  getWidth() returns an int for (int x = 0; x < pictureObj.getWidth(); x ++) …  getPixel() returns a reference to a Pixel object Pixel pixelObj = pictureObj.getPixel(x,y);  getPixels() returns a reference to an array of pixels Pixel[] pixelArray = pictureObj.getPixels() ;

Return Values: Count White Pixels 4 // this is an object method public int countWhitePixels() { int counter = 0; // loop through the columns (x direction) for (int x = 0; x < this.getWidth(); x++) { // loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++) {

Counting White Pixels 5 // get the pixel at the x and y location Pixel pixelObj = this.getPixel(x,y); // if the pixel is white increment the counter if (pixelObj.getRed()==255 && pixelObj.getGreen()==255 && pixelObj.getBlue() == 255) counter = counter + 1; } return counter; }

Rules 6  The return type must be specified in the method header, for example public int countWhitePixels()  The return statement sends a value back to where the method was invoked  The returned value can be stored in a variable: int numWhitePixels = pictureObj.countWhitePixels();  Or it can be used directly: System.out.println(pictureObj.countWhitePixels());

Warning 7 The method must always return a value of the correct type public int badMethod1(){ return 1.1; } public int badMethod2(int x){ if (x < 10) return 1; }

Warning 8 The method must always return a value of the correct type public int badMethod3(int x){ if (x < 10) return 1; if (x >= 10) return 2; }

Example 9  A return value can be a boolean value, i.e. true or false  Exercise: Write a method for the Picture class that checks whether two pictures are of the same size public boolean equalSize(Picture otherPic)  It will be invoked on a Picture object  If this Picture object is of the same size as the parameter picture, the method returns true, otherwise it returns false

Returning a Picture Object 10  So far, we have invoked our picture methods on a target Picture object  We will now write a new version of decreaseRed  It will create a new target Picture object inside the method  It will return this Picture object as the result of the method

Decrease Red Method 11  The new decreaseRed method will  be invoked on the source picture  return a target picture that has the same dimensions as the source picture  So we don’t need to pass the source picture as a parameter  Example of a call to the new decreaseRed() : Picture sourcePic = new Picture(…); Picture targetPic = sourcePic.decreaseRed(); targetPic.show();

Decrease Red Method 12 public Picture decreaseRed() { Picture targetPicture = new Picture(this.getWidth(), this.getHeight()); // loop through the columns for (int x = 0; x < this.getWidth(); x++) { // loop through the rows for (int y = 0; y < this.getHeight(); y++) { sourcePixel = this.getPixel(x, y); int redValue = sourcePixel.getRed(); int greenValue = sourcePixel.getGreen(); int blueValue = sourcePixel.getBlue(); // decrease the red value redValue = redValue / 2;

Decrease Red Method (continued) 13 // assign target picture values Pixel targetPixel = targetPicture.getPixel(x, y); targetPixel.setGreen(greenValue); targetPixel.setBlue(greenValue); targetPixel.setRed(redValue); } return targetPicture; }

Rotating Pictures 14

Rotating Pictures

Left Rotation 16 (0,0)(1,0)(2,0) (0,1)(1,1)(2,1)  To rotate an image 90 degrees to the left, we copy all the pixels, but they go to different locations in the target  (0,0) goes to (0,2) (1,0) goes to (0,1) (2,0) goes to (0,0) (0,1) goes to (1,2) (1,1) goes to (1,1) (2,1) goes to (1,0)  What happens to the source row (y) cordinates?  They go to the target column (x) coordinates : target x = source y (2,0) goes here (2,1) goes here (1,0) goes here (1,1) goes here (0,0) goes here (0,1) goes here

Left Rotation  What happens to the source column (x) coordinates? (0,0) goes to (0,2) (1,0) goes to (0,1) (2,0) goes to (0,0) (0,1) goes to (1,2) (1,1) goes to (1,1) (2,1) goes to (1,0)  They go to the target row (y) coordinates that are calculated by: target y = (source width –1) – source x (0,0)(1,0)(2,0) (0,1)(1,1)(2,1) (2,0) goes here (2,1) goes here (1,0) goes here (1,1) goes here (0,0) goes here (0,1) goes here

Left Rotation Method 18  The copyLeftRotation method will  be invoked on the source picture  return a target picture that is the source picture rotated left  So we don’t need to pass the source picture as a parameter  Example of a call to the new copyLeftRotation() : Picture sourcePic = new Picture(…); Picture targetPic = sourcePic.copyLeftRotation(); targetPic.show();

Left Rotation Method 19 public Picture copyLeftRotation(){ Picture targetPicture = new Picture(this.getHeight(), this.getWidth()); for (int sourceX = 0; sourceX < this.getWidth(); sourceX++){ for (int sourceY = 0; sourceY < this.getHeight(); sourceY++){ int targetX = sourceY; int targetY = (this.getWidth() – 1) – sourceX; Pixel sourcePixel = this.getPixel(sourceX, sourceY); Pixel targetPixel = targetPicture.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } return targetPicture; }

Summary 20  Returning values from Methods  Returning Pictures  Picture Algorithms that Return Pictures