 |  |

 |
 |  |  |
 | Free Iran!
THE CAKE IS A LIE
THE CAKE IS A LIE
THE CAKE IS A LIE
|  |
 |  |  |
|
|
|
        |
FinalExam Review Sp2005: From Decrease to Gray
Post Answers, Questions, Comments, etc. here.
(Back to Final Exam Review Sp2005)
public void decreaseRed()
{
Pixel pixel = null; // the current pixel
int redValue; // the amount of red
int greenValue; //amount of green
int blueValue; //amount of blue
// get the array of pixels for this picture object
Pixel[] pixels = this.getPixels();
// start the index at 0
int index = 0;
// loop while the index is less than the length of the pixels array
while (index < pixels.length)
{
// get the current pixel at this index
pixel = pixels[index];
// get the all color values at the pixel
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();
// set the values to their average
redValue = (int) ((redValue + greenValue + blueValue)/3);
greenValue = (int) ((redValue + greenValue + blueValue)/3);
blueValue = (int) ((redValue + greenValue + blueValue)/3);
// set the colors to their new value
pixel.setRed(redValue);
pixel.setGreen(greenValue);
pixel.setBlue(blueValue);
// increment the index
index++;
}
}
This seems like the round about way to do this, but it how I would do it based on the code given on the review.
| The code looks pretty good, but I don't think it should be called decreaseRed anymore, do you? Did you try it? How well does it work? Mark Guzdial |
public void grayscale()
{
Pixel pixel = null; // the current pixel
int redValue; // the amount of red
int greenValue; //amount of green
int blueValue; //amount of blue
// get the array of pixels for this picture object
Pixel[] pixels = this.getPixels();
// start the index at 0
int index = 0;
// loop while the index is less than the length of the pixels array
while (index < pixels.length)
{
// get the current pixel at this index
pixel = pixels[index];
// get the all color values at the pixel
redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();
// set the values to their average
redValue = (int) ((redValue + greenValue + blueValue)/3);
greenValue = (int) ((redValue + greenValue + blueValue)/3);
blueValue = (int) ((redValue + greenValue + blueValue)/3);
// set the colors to their new value
pixel.setRed(redValue);
pixel.setGreen(greenValue);
pixel.setBlue(blueValue);
// increment the index
index++;
}
}
;)
or do a for loop:
for (int i=0; i<pixels.length; i++)
{
pixel = pixels[i];
grayMe = (int) ((pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3);
pixel.setColor(new Color(grayMe,grayMe,grayMe));
}
Link to this Page