Download - Example Two-Dimensional Arrays
Georgia Institute of Technology
Example Two-Dimensional Arrays
• Maps– That street is in D-5
• Battleship– Try I-5
• Hit or miss
• Chairs at a theater or game– Row C seat 20
Georgia Institute of Technology
What is a two-dimensional array?
• The pixels in a picture are really stored in a two-dimensional array– Each pixel has a x value
(horizontal location)– Each pixel has a y value
(vertical location)– pictureObj.getPixel(x,y)
returns the pixel at that location
xy
Pixels
Pixel
• A pixel (abbr. for picture element) is the smallest unit of an image.
• Therefore, a 640x480 image is a matrix of 640 columns and 480 rows, each element of this matrix is called an image pixel.
RGB and Grayscale• In RGB format, each Pixel has 3 color components: Red,
Green, and Blue.
• A grayscale pictures has values from 0 to 255 that produce a black and white image in those ranges of gray. So it has one color ranging from 0 to 255 creating different scales of white to black (grayscale).
RGB Gray
GrayImage FreeResponse
Instance Variables to use: public static final int BLACK = 0;
public static final int WHITE = 255;
2D Array to use private int [][] pixelValues
private int pixelValues[][] =
{{255,184,178,84,135},
{84, 255, 255, 130, 84},
{78, 255, 0, 0, 78},
{84, 130, 255, 130, 84}};
Georgia Institute of Technology
0 1 2 3 4 0 255 184 178 84 129 1 84 255 255 130 84 2 78 255 0 0 78 3 84 130 255 130 84
Grayscale picture with 4 rows and 5 columns
Methods to create
public int countWhitePixels() (return method)
{
This method will loop through the 2D array of pixels in row-major order (row first then column)
And return the number of pixels in the image that contain the value WHITE. Return an int
}
Georgia Institute of Technology
0 1 2 3 4 0 255 184 178 84 129 1 84 255 255 130 84 2 78 255 0 0 78 3 84 130 255 130 84
It should return 5 since there are 5 pixel values with WHITE (255)
processImage()
public void processImage() (void method performs an action)
{
It will modify the image by chaing in the values in the instance variable pixelValues [][] by the following:
(1)Pixel value at position row col is decreased by the value at position [row+2] [col +2] if the row+2 and col + 2 position exist
(2)If the subtraction results in < 0 change the pixel value at the position to BLACK.
}Georgia Institute of Technology
• Work on Free Response today. (Let’s get finished)
• Work on multiple choice questions. These should be finished and turned in first thing Friday. I will grade these.
• If you need help come see me Thursday during IF once you have looked over them and tried them.
Georgia Institute of Technology