Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Making a tri-effect picture with Java

I am in a Java course that features a lot of manipulation of images. An assignment that I recently could not complete involved making a picture where the effect changed from the top third to the middle third and the bottom third. So the grayscale effect I would have in the top third would become a negative effect in the middle and a color third in the bottom. The due date has come and gone, so I was hoping someone could suggest how this is done for my reference in the future. I am including my code for the negative and the grayscale below...thanks. The points are a lost cause but I'd still like to know.

Public void greyscale(int start, int end)
  {
    //creates pixel array
  Pixel[] pixelArray = this.getPixels();
  //iterates through array
    for (Pixel currPixel : pixelArray)
    {//accessor
      int green = currPixel.getGreen();
      int red = currPixel.getRed();
      int blue= currPixel.getBlue();
      double average = 0.0;

      int i = (int)start;
      //while loop to average values to create grays
      while (i < end) {
        average = ((green + blue + red)/3);
      currPixel.setGreen((int)average);
      currPixel.setRed((int)average);
      currPixel.setBlue((int)average);
      i++;
    }
     //end loop markers
  }System.out.println("Splunge.");
  } 

  public void negative(int start, int end)
  {
    //creates pixel array
  Pixel[] pixelArray = this.getPixels();
  //iterates through pixel array
    for (Pixel currPixel : pixelArray)
    {
      //accessor
      int green = currPixel.getGreen();
      int red = currPixel.getRed();
      int blue= currPixel.getBlue();

      int i = (int)start;
       //while loop to make negative pixel values
      while (i < end) {
      currPixel.setGreen(255-green);
      currPixel.setRed(255-red);
      currPixel.setBlue(255-blue);
      i++;
    }
     //end loop marker
  }System.out.println("Splunge.");
  }//end method

What are the parameters start and end supposed to mean? Why are you iterating through the whole array of pixels? Did you divide a larger array into thirds elsewhere? Why have the nested iterations to set the pixel to the same value n times, repeatedly?

1 Answer

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

What are the parameters start and end supposed to mean?

That was not made clear to us.

Why are you iterating through the whole array of pixels?

I was showing that I could make a single effect for one picture to show that I could at least do that.

Did you divide a larger array into thirds elsewhere?

I have no idea how to do that. I would like to know how.

Why have the nested iterations to set the pixel to the same value n times, repeatedly?

If I don't have to, I won't. I have a decent grasp of Java fundamentals. I am not used to manipulating images. This class, so far, is almost exclusively about that use of the language. I had no idea going in. Now I am stuck and I would like not to fail.

thanks.

Nancy M.