CS 140 - Lab 8 Instructions

Objectives

  1. More practice with for-loops
  2. Experiment with new picture methods
  3. Learn how to make a copy of a picture.

Method

Part I - Working with some new Picture methods

  1. Create a Java program named Lab8 that displays the picture at
           T:/Harcourt/Spring2008/CS140/gallery/bug.jpg
    					

    Hint: You need to create a Lab8 class with a main method, then build a string object with the file name above, then build a picture object, then show it. We've done this many times in previous labs; this should be straightforward by now.

  2. We can determine the height of a picture, in pixels, by calling the method getHeight() on the picture object reference. In your main method use a println statement to print the height of the bug picture. Print a nice message such as Height is _____ where the correct height of the bug picture appears where the _____ is.
  3. Do the same thing for the width of the bug picture.
  4. Write a Java statement to compute the size, in bytes, of the bug picture and store this value in a variable named size. What type should size be? Think about whether it has a decimal part or not.
  5. Print the value of your size variable.
  6. We can create a blank picture using the picture constructor that takes two parameters; a width and a height. The code below creates a blank picture that is 100 X 200 pixels.
            Picture copy = new Picture(100, 200);
    						

    Create and then show a blank picture that is 200 pixels wide and 300 pixels high.

  7. Modify how you create the blank picture above so that it creates a blank picture that is the exact same width and height as the bug picture. Hint, call getHeight() and getWidth() and pass those values to the Picture constructor.

Part II - Creating a copy of an image

  1. Below is the method copyPicture that we went over at the beginning of today's class.
            public void copyPicture(Picture p) {
              for (int i = 0; i < this.getWidth(); i++) {
                for (int j = 0; j < this.getHeight(); j++) {
                  Color c = this.getPixel(i,j).getColor();
                  p.getPixel(i,j).setColor(c);
                }
              }
            }
    					

    Copy this method into your Picture class in Picture.java.

  2. Using the blank picture object created in the previous section that is the same size as the bug, create a copy of the bug and show it. Do this by calling the copyPicture method on the original bug picture object passing the blank picture as a parameter.

Extra Credit

  1. In the lab above we created an exact copy of the bug picture. Make a blank picture that is 1024 X 768 and copy the bug picture to it and show the resulting copy. Explain what happens and why? (Write answer below)
  2. This time create a blank picture that is too small to hold the original. For example create a blank picture that is 100 X 100 and try and copy the bug to it and show it. Did it show the copy? Click on the interactions pane and see if you have an error message. Scroll to the top of the error message and explain. (Write answer below)

© Rich Sharp and Ed Harcourt