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

2015/4/5

Can't find a color at...

-nic- -nic-

2015/4/5

#
image.setColor(Color.RED);
        image.fill();
        for(int i=0; i<10000; i++)
        {
            //int ix = Greenfoot.getRandomNumber(600);
            //int iy = Greenfoot.getRandomNumber(600);
            
            Color color = getColorAt(10,10);
            color.darker();
            image.setColorAt(10,10,color);
        }
When compiling the line
            Color color = getColorAt(10,10);
throws a NullPointerException why? Should it not return RED as the colour?
danpost danpost

2015/4/6

#
You would probably be alright if you used:
Color color = image.getColorAt(10, 10);
since you want the color from the image. The 'for' loop will execute within one execution of the method it is in and the color will just instantaneously show as the final color, however (in other words, if you are attempting to animate the darkening of the color, this will fail). For another thing, fifteen iterations through the loop would be sufficient to reach to final color (10'000 seems quite extreme).
davmac davmac

2015/4/6

#
When compiling the line ... throws a NullPointerException why?
You don't get exceptions during compiling; this problem must happen at runtime. Post the complete stack trace. The line that you point out could not throw a NullPointerException.
-nic- -nic-

2015/4/7

#
Thanks danpost, no error now. I'm trying to make a system whereby random coordinates are chosen the colour at the coordinates is then made darker, 10,000 times Davmac, all these are done from the prepare method in the world i was getting these errors before pressing run.
danpost danpost

2015/4/7

#
-nic- wrote...
Thanks danpost, no error now. I'm trying to make a system whereby random coordinates are chosen the colour at the coordinates is then made darker, 10,000 times
There is another problem with the code given above. It is setting the same color that was found at the location back to that location. The color referenced by 'color' is never changed. Although line 9 appears to change it (and actually does produce a new darker version of the color referenced in the 'color' variable), nothing is done with that darker color. You are not assigning it to the 'color' variable or using it in any way. Change line 9 to:
color = color.darker();
to darken the color referenced by the variable.
-nic- -nic-

2015/4/7

#
Thanks got it working now
You need to login to post a reply.