Change Contents of the Bubble
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

PreQuiz for 26Jan2005 Quiz

Below is the code to scale an image to an input size. For example, if I wanted to scale the picture myPicture down by 1/2 in each dimension, I would execute myPicture.scale(0.5). Notice that scale() does return a copy of the original picture.

/**
   * Method to scale the picture by a factor, and return the result
   * @param scale factor to scale by (1.0 stays the same, 0.5 decreases each side by 0.5, 2.0 doubles each side)
   * @return the scaled picture
   */
  public Picture scale(double factor)
  {
    Pixel sourcePixel, targetPixel;
    Picture canvas = new Picture((int) (factor*this.getWidth())+1,
                                 (int) (factor*this.getHeight())+1);
    // loop through the columns
    for (double sourceX = 0, targetX=0;
         sourceX < this.getWidth();
         sourceX+=(1/factor), targetX++)
    {
      // loop through the rows
      for (double sourceY=0, targetY=0;
           sourceY < this.getHeight();
           sourceY+=(1/factor), targetY++)
      {
        sourcePixel = this.getPixel((int) sourceX,(int) sourceY);
        targetPixel = canvas.getPixel((int) targetX, (int) targetY);
        targetPixel.setColor(sourcePixel.getColor());
      }
    }
    return canvas;
  }


Write the method crop which returns a new picture that is part of the original. It takes as input the (x1,y1) of the upper left hand corner of the picture and the (x2,y2) of the lower right hand corner of the picture. So myPicture.crop(10,20,100,120) would return a new Picture that is a piece of myPicture – just the pixels between x=10 and x=100 and y=20 and y=120. (So, the new Picture would be 90 pixels wide and 100 pixels tall.)

(Hint: This could be useful for your collage in HWREMOVED, so feel free to actually code this and get it to work!)

Comments? Questions?



should the crop be painted onto a canvas? and if so does the canvas have to be as small as the new cropped picture?



figured it out

public Picture crop(x1,y1,x2,y2)

I'm getting an error on my very first line that says:
Error: expected
what am I doing wrong?

by the way, in my question above it is supposed to say "identifier" before "expected"

You have to tell it the types of the input variables. Put "int" before "x1" and I think you're golden. Mark Guzdial


What does += mean?

x += 3 is shorthand for x = x + 3. We covered this in the second lecture, when we first started Java. Mark Guzdial


when i try to run scale() in out of class Picture, i get the following output:

> Picture q = new Picture(FileChooser.pickAFile())
> q.show()
> q.scale(2.0)
Picture, filename null height 721 width 961

When I do q.show(), the file will show itself. Immediately after when I input q.scale(), i get the "null" statement. Any idea what's going on?

q.scale(2.0) returns a picture. What you saw was the PRINT of the picture (it's "value"). Try q.scale(2.0).show(). q.scale() will be weird since there's no input. Mark Guzdial

i have that same "null" statement problem...

what does all this mean?

java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.IntegerInterleavedRaster.getDataElements(Unknown Source)
at java.awt.image.BufferedImage.getRGB(Unknown Source)
at SimplePicture.getBasicPixel(SimplePicture.java:259)
at Pixel.getAlpha(Pixel.java:71)
at Pixel.setColor(Pixel.java:211)
at Picture.crop(Picture.java:2331)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

I'm pretty sure that it means that one of the coordinates is wrong...that is, the program is trying to get to a pixel located at an X or Y value (or both) greater than the dimensions of the picture. In other words, it's like looking for column 5 in a picture with only 4 columns. Student36
Jaron nailed it! Yep – if you look at line 2331 of Picture.java, in your crop method, you'll see that you went beyond the bounds of the picture. (To find a particular line, type control-G, then enter "2331", then hit Enter.) You'll see that the line number is up there, in the middle. (We can talk about what all this stuff up above means, if you're interested. Ask me in class.) Mark Guzdial

public Picture crop((int)(x1,y1,x2,y2));

I'm getting errors (" illegal start of type " and " expected") for this line. What's going on, and how can i correct them?
Not (int). That's forcing a non-int value to become an int. You need the declaration form. No parentheses. Literally, type public Picture crop(int x1, y1, x2, y2); Mark Guzdial


Let me refine that a bit. The below actually compiles – it doesn't do the crop, but it does have the right heading and form. Mark Guzdial
  /**
   * Crop the picture from (x1,y1) to (x2,y2)
   **/
  public Picture crop(int x1, int y1, int x2, int y2)
  {
    return this;
  }





ewrewr



Links to this Page