Lab 11

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 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 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:
  • startX is 150
  • endX is 275
  • startY is 400
  • endY is 550
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 11

Due: Wednesday 4/4/2011 by 11:59 pm

Create a Java program that will:

  1. Contain a comment indicating

    • your Name,
    • Net-ID,
    • Course Name,
    • Assignment Name and
    • a short description of the assignment

  2. Contain the main() method that will

    • allow the user to select a file using pickAFile().
    • call the method cropImage () twice.
    • display the image returned by each call to the method cropImage ().
    • allow the user to save each image returned by the calls to the method cropImage () where the name of the file is determined by using the pickAFile() method from the FileChooser class.
    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:

    • take 5 parameters:
      1. the image to be cropped
      2. the x position were the cropping is to start
      3. the y position were the cropping is to start
      4. the x position were the cropping is to end
      5. the y position were the cropping is to end
    • verify that the starting x position is less than the ending x position
    • verify that the starting y position is less than the ending y position
    • verify that the x and y positions are within the boundaries of the original image
    • create a new empty image just large enough to hold the cropped image. You are expected to use Picture (width, height) constructor for this.
    • copy the pixels from the original images passed in as the parameter to the new image
    • return the new cropped image
    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:

    • Good variable names
    • in-line commenting
    • header block comment describing the entire program which is to include:
      • your name
      • day and time of your CS 101 lab section (i.e. Wednesday at 9:00)
      • A description of the project.
    • a method comment block that describes each method with is to include:
      • a description of the method
      • a description of each parameter the method uses
      • a description of the return value (if any) produce by the method
    • proper indentation of program statements
    • use of blank lines to separate blocks of code.

  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

Submit the Java file and the original picture via the proper assignment link in Blackboard.
Topic revision: r1 - 2012-03-27 - 18:34:21 - Main.troy
Copyright 2016 The Board of Trustees
of the University of
Helping Women Faculty Advance
Funded by NSF