-2

I am so lost for this code I am working on. I have to make a picture have a grayscale using only java. I have the base code, but I do not know what to put in that won't make the entire screen gray. I am avidly working on it, but I am lost and have no clue what to do next. The way it currently is, it just takes an image goes through a process then makes a new version of it completely the same, but I need to make the new version the grayed version of it. Visit: https://ask.extension.org/uploads/question/images/attachments/000/037/087/image_300x300%2523.jpg?1406470060 For the tree picture I am using.

import java.awt.*;                                       //import the awt graphics package
class TrueColors                                                //start of the class
{
 TrueColors()                              //start of the main method
 {       
    Picture pictureObj = new Picture("trunk.jpg");     
    pictureObj.explore();                                           
    int redValue = 0; int greenValue = 0; int blueValue = 0;        

    Pixel targetPixel = new Pixel(pictureObj, 0,0);               
    Color pixelColor = null;                                                

    for(int y=0; y < pictureObj.getHeight(); y++)                   
    {
        for(int x = 0; x < pictureObj.getWidth(); x++)              
        {
            targetPixel = pictureObj.getPixel(x,y);                 
            pixelColor = targetPixel.getColor();                    

            redValue = pixelColor.getRed();                         
            greenValue = pixelColor.getGreen();                     
            blueValue = pixelColor.getBlue();                       
            pixelColor = new Color(redValue, greenValue, blueValue);
            targetPixel.setColor(pixelColor);                       
        }//end of the inner for loop
    }//end of the outer for loop

    pictureObj.explore();                                           
    pictureObj.write("NewTrunk.jpg");                  
    pictureObj.show();                                              
 }//end of main method
}//end of class
BWR
  • 47
  • 11

2 Answers2

1

You need to change the RGB values to be all equal to make it grayscale. There are many possible algorithms for this; here is one:

int gray = (0.2989 * red) + (0.5870 * green) + (0.1140 * blue);
pixelColor = new Color(gray, gray, gray); 
FredK
  • 4,094
  • 1
  • 9
  • 11
  • When I tried to use the code you provided, it won't compile because it says that the int gray values are incompatible. it says can't convert from double to int – BWR Jul 29 '16 at 16:58
  • Also, when I try to run it after making it to int from double, it says that the new color should be red, green, and blue where you put gray. – BWR Jul 29 '16 at 17:03
0

I suggest you check out this question: convert a RGB image to grayscale Image reducing the memory in java.

While your intention is not to reduce memory, it should serve the same purpose.

--edit-- A simple greyscale algorithm is simply to take the average of the red, green, and blue. Similar to the other post you could do:

redValue = pixelColor.getRed();                         
greenValue = pixelColor.getGreen();                     
blueValue = pixelColor.getBlue();
greyValue = (int)((redValue + greenValue + blueValue)/3)                  
pixelColor = new Color(greyValue, greeyValue, greyValue);
targetPixel.setColor(pixelColor);
Community
  • 1
  • 1
  • The problem with that is, I have not learned how to use BufferedImages. I am working on a course and this is one of the assignments I am stuck on. – BWR Jul 29 '16 at 16:57
  • Thank you so much, that fixed my issue. – BWR Jul 29 '16 at 17:17
  • (R + G + B) / 3 is a really poor way to calculate colors for a grayscale image. – eldo Aug 01 '16 at 13:22
  • @eldo What would you recommend instead? – BWR Aug 02 '16 at 00:21
  • 1
    @Ben Rocco Luminance-preserving conversion, use weighted avarage of colors 0.2126 r 0.7152 g 0.0722 b see explanation and result of different approaches of grayscale conversion: http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/ – eldo Aug 02 '16 at 05:57