[Bluej-discuss] exercise 11.37 page 331

Michael Kölling M.Kolling at kent.ac.uk
Mon May 8 20:57:23 BST 2006


Your code looks very good until the very last line. That's where the  
bug creeps in.

When you use

	image.setPixel(x, y, value);

(that is, the setPixel method with one int as the color value) the  
system takes all three colors out of selected bits of that int  
variable. You should use the setPixel method that uses a Color  
argument instead.

You 'value' variable now holds the value you want to use for each of  
the color channels, so you should create a Color using

	new Color(value, value, value)

and pass that one to setPixel. Since the three color channels have  
the same value, the color will be a shade of grey.

Michael


On 8 May 2006, at 17:51, Luis Moreira wrote:

> Hi Michael,
> this is what I have done, based on what I got from your email, but  
> the problem is that all pixels go blue. Is this what you mean or am  
> I missing something?
>
> public void apply(OFImage image)
>    {
>        int height = image.getHeight();
>        int width = image.getWidth();
>        for(int y = 0; y < height; y++) {
>            for(int x = 0; x < width; x++) {
>                Color pixel = image.getPixel(x, y);
>                int red = pixel.getRed();
>                int green = pixel.getGreen();
>                int blue = pixel.getBlue();
>                int value = (red + green + blue)/ 3;
>                image.setPixel(x, y, value);
>            }
>        }
>    }
>
>
>> From: Michael Kölling <M.Kolling at kent.ac.uk>
>> Reply-To: General discussion for users of BlueJ <bluej- 
>> discuss at bluej.org>
>> To: General discussion for users of BlueJ <bluej-discuss at bluej.org>
>> Subject: Re: [Bluej-discuss] exercise 11.37 page 331
>> Date: Mon, 8 May 2006 16:17:20 +0100
>>
>> On 8 May 2006, at 16:12, Luis Moreira wrote:
>>
>> > Hi Guys,
>> > on exercise 11.37, I manage to add the filter no problem, but the
>> > functionality I could not work out.
>> > the exercise states that to make a pixel any shade of gray we give
>> > the same
>> > value to red,green and blue. that is fine but what value do I need
>> > to give
>> > the three components?
>>
>> You can preserve the brightness (roughly) by leaving the sum of  
>> the R/
>> G/B channels the same.
>>
>> That is, compute the average of the R, G and B values, and use that
>> average for all three channels.
>>
>> Regards,
>>
>> Michael
>>
>> _______________________________________________
>> mailing list bluej-discuss at bluej.org
>> To unsubscribe or change your preferences, go to
>> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>
> _________________________________________________________________
> The new MSN Search Toolbar now includes Desktop search! http:// 
> join.msn.com/toolbar/overview
>
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss



More information about the bluej-discuss mailing list