Section 5.3. Concepts Summary

[Page 167 (continued)]

5.3. Concepts Summary

This chapter introduced two-dimensional arrays, nested loops, working with multiple variables in a for loop, returning a value from a method, and method overloading.

[Page 168]

5.3.1. Two-Dimensional Arrays

Pixels are stored in a two-dimensional array. A two-dimensional array is similar to seating in an auditorium. You can find your seat based on the row and chair number. You can access a location in a two-dimensional array by specifying an x and y. All indices start with 0.

5.3.2. Nested Loops

To process all of the pixels in a picture and track the x and y location of each pixel you need to use a nested loop. Nested loops are loops inside of loops. You can either loop through the rows and then the columns (y and then x) or loop through the columns and then the rows (x and then y).

`// loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++) {   // loop through the columns (x direction)   for (int x = 0; x < this.getWidth(); x++)   {     // get the current pixel at this x and y position     pixel = this.getPixel(x,y);     // do something to the color     // set the new color     pixel.setColor(aColor);   } }`

To restrict the area that you are looping through, use different values for starting and stopping the loop. To loop through a rectangular area starting with the pixel at (startX, startY) at the upper-left corner of the rectangular area and ending with the pixel at (endX,endY) as the bottom-right corner of the rectangular area, use:

`// loop through the rows (y direction) for (int y = startY; y <= endY; y++) {   // loop through the columns (x direction)   for (int x = startX; x <= endX; x++)   {     // get the current pixel at this x and y position     pixel = this.getPixel(x,y);     // do something to the color     // set the new color     pixel.setColor(aColor);   } }`

You can declare more than one variable in a loop. This is useful when you copy from one picture to another. Use variables to represent the source picture x and y values and use other variables to represent the target picture x and y values. You can change how the source and target pixel are used in order to rotate the picture.

[Page 169]

`// loop through the columns for (int sourceX = 0, targetX = 0;      sourceX < sourcePicture.getWidth();      sourceX++, targetX++) {   // loop through the rows   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());   } }`

By changing the initial and ending values of sourceX, sourceY, targetX, and targetY you can change what part of the source picture you want to copy and where you want it to go on the target picture. Using this you can clip and create collages.

If you change the amount you increment or decrement a loop variable by, you can scale a picture up or down.

5.3.3. Returning a Value from a Method

To declare a method you specify the visibility for the method, the type of thing it returns, the name of the method, and the parameter list inside parentheses. This is followed by the body of the method which is inside of an open and close curly brace.

`visibility returnType name(parameterList) {   // statements in method   // return a value   return valueToReturn; }`

Methods that do not return any value use the keyword void as the return type. Methods that do return a value use the type of that value for the return type and then have a return keyword in them that is followed by the thing to return. Remember that a type is any of the primitive types or the name of a class.

Here is an example public method declaration that doesn't return anything and the name of the method is mirrorVertical and it doesn't take any parameters.

`public void mirrorVertical()`

Here is an example public method declaration that returns an object of the class Picture.

`public Picture scaleUp(int numTimes)`

[Page 170]

Notice that it gives a return type of Picture. The body of the method must have the keyword return in it and it must return an object that is an instance of the class Picture.

A class can have more than one method with the same name as long as the parameter list is different. The methods can take a different number of parameters, or the types of the parameters can be different, or the order of the types can be different. You can't have two methods with the same name and the same number of parameters with the same types in the same order.

`public void copyPictureTo(Picture sourcePicture, int xStart) public void copyPictureTo(Picture sourcePicture, int xStart,                            int yStart)`

Notice that there are two method declarations with the same name but one takes two parameters and one takes three. The compiler will check that a method exists that takes the same number and type of parameters. If the compiler can't find a method with the same number, type, and order of parameters it will report that the method doesn't exist.

`> p.copyPictureTo(); Error: No 'copyPictureTo' method in 'Picture'`

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191