You are on page 1of 3

# U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

## COMS2003: Application and Analysis of Algorithms II

A3 Laboratory 7 2012

## Two dimensional arrays

Today, were going to play with some graphics. Note that your screen is fundamentally a two dimensional array of colours. Every item in this array is called a pixel, and denes the colour of a single point on your screen. In order to work with graphics, were going to do an exercise working with 2 Dimensional Arrays. Declaring a two dimensional array is very similar to creating a single dimensional array, and is done as follows: int[][] myArray = new int[width][height]; So in order to create a two dimensional array with width 640 and height 480, we could say: int[][] pixels = new int[640][480]; Then, remember that we index our array using these dimensions. It is useful to think of them as x and y, with x going from 0 to width and y going from 0 to height. You can query them as follows: int value = myArray[x][y]; For instance, if we wanted to look at coordinate (600,300), we could do the following: int myPixel = pixels[600][300]; If you want to iterate over this array, youd need a nested for loop structure. For instance, if I wanted to set all the values in my pixel array to 150, I could do the following: for (int x=0; x<640; x++){ for (int y=0; y<480; y++){ pixels[x][y]=150; } }

1.1

Averaging exercise

## 1. In Netbeans, create a project called Lab7

1.2

Main

1. In your Main class, netbeans has created a main method for you. 2. Create a two dimensional array called p, which has a width of 50 and a height of 40. 3. Next, you must read in 50 integers, all stored on a seperate line. You must then store these integers in the bottom row of your 2D array. Note that this will be where the y coordinate is 39. 4. Next, create a method called average which takes in a two dimensional array of integers and returns nothing. 5. This average method does the following: For every item in the array except the ones on the borders (Note that this will need to be a nested for loop that excludes x=0, x=49, y=0 and y=39), set the value of the item in the array to be equal to the average of the three items below it and its current value. For example, the pixel in position (10, 15) must be set to the average of the values in (9, 16), (10, 16), (11, 16)and(10, 15). 6. Now, in your main method, call the average method on the p array 30 times after having read in the input. 7. Lastly, print out your array. To do this, use the deepToString() method in the Arrays class. As an example, if you wanted to print out the pixel array used earlier, you would type: System.out.println(Arrays.deepToString(pixel)); Note that you have to import java.util.Arrays 8. Submit the jar le containing your program on moodle

1.3

Smoke

The last exercise must have seemed kind of random. The purpose of it is going to become clear as we use it to make a smoke effect on the screen. In order to do this, we must make some modications to our code, including the idea of randomness. Note that in java, in order to generate a random integer, we create an object that generates numbers, and then we tell it to generate a number. To generate a number between 0 and 10, you could use the following code:

Random myGenerator = new Random(); int r = myGenerator.nextInt(10); Now lets look at the modications we need to make. 1. First, change your array so that it has a width of 500, and a height of 100. 2. Then, create a method called doRandom, which takes in a two dimensional array, and lls the row with y value 99 (The last row) with randomly generated numbers between 0 and 255. 3. Change your average method so that it works on an array with width 500 and height 100. 4. Strangely, using these functions simulates a smoke effect. To see this, download the Smoke.zip le from moodle. Unzip it somewhere and open it in netbeans. 5. Note that in this project, the average and doRandom functions must be replaced with the ones that you have written. Try running the code to see what happens. How does it work? Basically, the Draw2D class takes the value in the array as the brightness of the dot that is shown on the screen. By calculating the average the way we have, we are creating a model (Admittedly a not very good model) of how smoke disperses. When we generate the random numbers at the bottom, were saying that there is a random amount and intensity of smoke at the bottom. As the smoke rises, it disperses, hencing becoming averaged. If you look inside the Draw2D class, youll see a method called paint. In this method, there is the following line: g.setColor(new Color(toDraw[i][j], toDraw[i][j], toDraw[i][j])); The format of the Color contructor is new Color(int red, int green, int blue). The line sets the drawing colour to be a shade of grey by setting the red, green and blue components to be equal. Try changing the line to the following: g.setColor(new Color(toDraw[i][j], 0, 0)); Note that the green and blue components have been set to 0. Feel free to experiment with the code. You can always redownload the project if you break something.