CS 101 - Introduction to Computing, Spring 2010

Lab 9

This lab assignment will have you write at least two methods in java that will create a new image that will show a portion from another image. This technique is called cropping.

Cropping of an image is to take a certain part of image and discard the rest of the image. Once the user has picked an image, we shall create an empty image which will store the resulting cropped image. Then we shall copy the pixels from the part of the original to the new image. "Copying the pixels" is nothing but copying the colors of pixels from original image to the new image. In lecture, we wrote code that copied an entire image to another in Lect39a.java. The important part of that code is something like the code below:

  public static Picture copyImage (Picture p1) 
  {
    // set up my return variable
    Picture p2;
    
    p2 = new Picture (p1.getWidth(), p1.getHeight() );
    
    // access each pixel to modify it.
    Pixel pixelArray[] = p1.getPixels();

    for (int count = 0 ; count < pixelArray.length ; count++)
    {
      // access the original pixel and information from that pixel
      Pixel pix1 = pixelArray [ count];
      int redAmount = pix1.getRed ();
      int greenAmount = pix1.getGreen ();
      int blueAmount = pix1.getBlue ();
      int xPos = pix1.getX();
      int yPos = pix1.getY();

      // determine the position the pixel in the new picture
      int newX = xPos;
      int newY = yPos;
      Pixel pix2 = p2.getPixel (newX, newY);
      
      // set the proper information for the pixel in the new picture
      pix2.setRed   (redAmount);
      pix2.setGreen (greenAmount);
      pix2.setBlue  (blueAmount);
    }
    
    return p2;
  }

We have also showed how to access only a portion if an images in Lect34e.java. The important part of that code is shown below:
    // accessing a portion of an image
    Picture p = new Picture (400, 600);
    
    Pixel arr[] = p.getPixels();
    
    for (int count = 0; count < arr.length; count++)
    {
      Pixel pix = arr[count];
      int xPos = pix.getX();
      int yPos = pix.getY();
      
      // access the 125x150 pixel portion of the picture
      if ( ((xPos >= 150) && (xPos < 275))  && ((yPos >= 400) && (yPos < 550)) )
      {
        pix.setRed (0);
      }
    }
The above code accesses the 125x150 pixel portion of the picture from the X coordinates from 150 to 275 and the Y coordinates from 400 to 550. These pixel position are called the startX, endX, startY and endY values:

The above code just changes the color value at those pixels. It does NOT do any cropping. In order to do cropping, those pixels must be copied from that portion of the picture to a new blank picture.

In this example, this new blank picture would need to be 125 pixels wide and 150 pixels high. These values are determined by subtracting the endX value from the startX value and subtracting the endY value from the startY value.

When copying the pixels from the original picture to the new picture, we will also need to move the pixels to there proper X and Y coordinates in the new picture. In the above example, the original pixel at position (150, 400) needs to be placed at position (0, 0). The original pixel at position (200, 500) needs to be placed at potition (50, 100). This translation is easy to do since we just have to subtract the startX value from the original X coordinate and subtract the startYvalue from the original Y coordinate.

Lab Assignment 9

Due: Thursday 3/18/2010 by 11:59 pm

Create a Java program that will:

  1. Contain a comment indicating

  2. Contain the main() method that will

    Each call will send the image selected but must crop a different section of the image and crop a different sized section image. You are to crop an interesting item from the original image and not just some random location. You will also be required to submit your image with your JES program file. The BlendIn.jpg image offers a few interesting spots that could be cropped. You may use this image or any other image of your choosing. Here is another image that offers a few good spots for cropping, apollo-11-astronauts.jpg. You may wish to use the picture explore() method to help find the x and y coordinates of different interesting sections of an image. Make sure you submit the image with your code when you turn in this program!

  3. Contain a method called cropImage () that will:

    This method may call other methods that are in a library or that you have written. Note that the code given in lecture is close to what is needed here but it is not exact. If any of the verify checks fail, print out a good error message and return from the function.

  4. You must write your programs using good programming style which includes:

  5. You are also to submit the Java file electronically via the steps specified below. You are also required to submit the image that you selected the x-start, y-start, x-end and y-end values.

    How assignments should be submitted

    1. Go to the blackboard site for the class
    2. Select "Assignments" on the left bar
    3. Locate the correct assignment to submit; click on "View/Complete Assignment" at the bottom of that assignment
    4. Next to "Attach local file", click "Choose file", choose the file you want to submit; please submit only ONE source code file, properly named.
      For example for lab2, name it Lab2yournetid.java, for example if your Net ID is sfranz3, the filename would be Lab2sfranz3.java
      Please only submit source code file (the .java file, not the .class). Also, if you have any comment about your program, please write it down in the same file; please do NOT write in the "Comments" field on the submission page (this will go into a different file and will be easily missed).
    5. Hit "Submit"
    6. Go back once again to View/Complete assignment and make sure that your file was submitted; also, this page will show your grade and comments (if any) after assignments are graded.