This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mach
Mach wrote ...

2015/2/14

What seems to be the problem? Double can not be converted to boolean?

Mach Mach

2015/2/14

#
public void detectEdges()
    {
        GreenfootImage bg = getBackground();
        for (int x =0; x< bg.getWidth()-1; x++)
        {
            for (int y = 0; y< bg.getHeight(); y++)
            {
               Color c = bg.getColorAt(x,y);
               Color d = bg.getColorAt(x+1,y);
               if ( Math.sqrt(Math.pow(c.getRed() - d.getRed(),2))+(Math.sqrt(Math.pow(c.getBlue() - d.getBlue(),2)))+(Math.sqrt(Math.pow(c.getGreen()-d.getGreen(),2))));
               bg.setColorAt(x,y,c.WHITE);
            }
        }
    }
It is supposed to only show the edges of pictures when the method is run, but I can not seem to get it to work. I get th error: incompatible types: double cannot be converted to boolean. Thanks!
danpost danpost

2015/2/14

#
In line 10, your 'if' condition is just a numerical expression. It is like saying
if (128.37)
'128.37' is not a true or false value -- it is just a number. Line 11 uses 'c.WHITE'. 'c' is already declared as a Color object. The code may execute properly as is; but, it is poor programming to use an object to get the value of a class field. Better is to just use 'Color.WHITE'. If the code is later changed and the value of 'c' is ever found to be 'null', 'c.WHITE' will throw a NullPointerException while 'Color.WHITE' will not.
You need to login to post a reply.