AP Computer Science Picture Lab Name: Michael Metz A1: Introduction to digital pictures and color Questions: 1. How many bits does it take to represent the values from 0 to 255? ➢ 8 bits 2. How many bytes does it take to represent a color in the RBG color model? ➢ 3 bytes 3. How many pixels are in a picture that is 640 pixels wide and 480 pixels high? ➢ 640 * 480 = 307200 pixels A2: Picking a color Questions: 1. How can you make pink? ➢ R: 255 G: 0 B: 255 2. How can you make yellow? ➢ R: 255 G: 255 B: 0 3. How can you make purple? ➢ R: 115 G: 0 B: 255 4. How can you make white? ➢ R: 255 G: 255 B: 255 5. How can you make dark gray? ➢ R: 75 G: 75 B: 75 A3: Exploring a picture Questions: 1. What is the row index for the top left corner of the picture? ➢0 2. What is the column index for the top left corner of the picture? ➢0 3. The width of this picture is 640. What is the right most column index? ➢639 4. The height of this picture is 480. What is the bottom most row index? ➢479 5. Does the row index increase from left to right or top to bottom? ➢top to bottom 6. Does the column index increase from left to right of top to bottom? ➢left to right 7. Set the zoom to 500%. Can you see squares of color? This is called pixelation. Pixelation means displaying a picture so magnified that the individual pixels look like small squares. ➢yes Exercises: 1. Copy the line(s) in the main() method that you changed and paste them here. Run the main method again, take a screen shot of your computer screen (press the [ctrl] and [Print Scrn] buttons at the same time) and paste the screen shot below your line(s) of code. public static void main( String args[]) { /*Picture pix = new Picture("beach.jpg"); pix.explore();*/ Picture pix = new Picture("wall.jpg"); pix.explore(); } A4: Two-dimensional arrays in Java Exercises: 1. Copy the lines of code from your getCount() method and paste them here. public int getCount(int a){ int num = a; int x = 0; for(int m = 0; m < matrix.length; m++) for(int n = 0; n < matrix[0].length; n++) if(num == matrix[m][n]) x++; return x; } After uncommenting the testGetCount() method and the call to it in the main() method of IntArrayWorkerTest, what is printed? Copy and past the output shown in your terminal window here: Count should be 6 and count is 6 2. Copy the lines of code from your getLargest() method and paste them here. public int getLargest(){ int num = 0; for(int x = 0; x < matrix.length; x++) for(int z = 0; z < matrix[0].length; z++) if(num < matrix[x][z]) num = matrix[x][z]; return num; } After uncommenting the testGetLargest() method and the call to it in the main() method of IntArrayWorkerTest, what is printed? Copy and past the output shown in your terminal window here: Largest Largest Largest Largest should should should should be be be be 6 6 6 6 and and and and is is is is 6 6 6 6 3. Copy the lines of code from your getColTotal() method and paste them here. public int getColTotal(int r){ int num = 0; for(int x = 0; x < matrix.length; x++) num += matrix[x][r]; return num; } After uncommenting the testGetColTotal() method and the call to it in the main() method of IntArrayWorkerTest, what is printed? Copy and past the output shown in your terminal window here: Total for column 0 should be 5 and is 5 Total for column 1 should be 7 and is 7 Total for column 2 should be 9 and is 9 A5: Modifying a picture Questions: 1. Open Picture.java and look for the method getPixels2D. Is it there? ➢No 2. Open SimplePicture.java and look for the method getPixels2D. Is it there? ➢Yes 3. Does the following code compile? EXPLAIN. DigitalPicture p = new DigitalPicture(); ➢NO, you cannot instantiate a object from a interface, A interface can be a pointer but you cannot instantiate it! 4. Assuming that a no-argument constructor exists for SimplePicture, would the following code compile? EXPLAIN. DigitalPicture p = new SimplePicture(); ➢Yes, like I said in the above answer, “A interface can be a pointer but you cannot instantiate it!”. The interface DigitalPicture is pointing to a SimplePicture object, and SimplePicture implements the DigitalPicture interface. 5. Assuming that a no-argument constructor exists for Picture, does the following code compile? EXPLAIN. DigitalPicture p = new Picture(); ➢Yes, like I said in the above answer, “A interface can be a pointer but you cannot instantiate it!”. The interface DigitalPicture is pointing to a Picture object, and Picture extends SimplePicture which implements the DigitalPicture interface. 6. Assuming that a no-argument constructor exists for Picture, does the following code compile? EXPLAIN. SimplePicture p = new Picture(); 5. Copy the lines of code from your grayscale() method and paste them here. public void grayscale(){ Pixel[][] pixels = this.getPixels2D(); for (Pixel[] rowArray : pixels) for (Pixel pixelObj : rowArray) { double average = ((pixelObj.getGreen()) + (pixelObj.getBlue()) + (pixelObj.getRed())) / 3.0; pixelObj.setRed((int) average); pixelObj.setGreen((int) average); pixelObj.setBlue((int) average); } } Copy the lines of code from your new static tester method in the PictureTester class and paste them here. public static void testGrayscale(){ Picture beach = new Picture("beach.jpg"); beach.explore(); beach.grayscale(); beach.explore(); } After calling this tester method in the main() method of PictureTester, take a screen shot, and paste your image here: 3. Copy the lines of code from your mirrorHorizontlBotToTop() method and paste them here. public void mirrorHorizontalBotToTop() { Pixel[][] pixels = this.getPixels2D(); Pixel topPixel = null; Pixel bottomPixel = null; int height = pixels.length; for (int row = 0; row < height / 2; row++) for (int col = 0; col < pixels[0].length; col++){ topPixel = pixels[row][col]; bottomPixel = pixels[height - 1 - row][col]; topPixel.setColor(bottomPixel.getColor()); } } Copy the lines of code from your new static tester method in the PictureTester class and paste them here. public static void testMirrorHorizontalBotToTop() { Picture caterpillar = new Picture("caterpillar.jpg"); caterpillar.explore(); caterpillar.mirrorHorizontalBotToTop(); caterpillar.explore(); } After calling this tester method in the main() method of PictureTester, take a screen shot, and paste your image here:
© Copyright 2024