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

2016/8/10

Color Detection

Wolfpack9 Wolfpack9

2016/8/10

#
I am trying to detect the color of another object. public void stopSun (Body other) { double fds = other.getExactX(); int dfg = (int) fds; double zxc = other.getExactY(); int tyu = (int) zxc; int Color color = getImage().getColorAt(int tyu,int dfg); if ( color == Color (255, 216, 0)) { //next step } } Why isn't this working?
Super_Hippo Super_Hippo

2016/8/10

#
'int Color color' won't compile. A variable is either an 'int' or a 'Color' but not both. You should not use random letters for your variables. You probably mixed up the x and y coordinates. Btw, instead of casting the two 'getExact' methods to an int, you could use the 'getX' and 'getY' methods instead. And before telling you that the if-condition is also not correct like this, I think it makes sense to ask what you want to achieve. You said you are trying to detect the color of another object, but it looks like you want to compare a pixel of an actors image (the one x-y-coordinates like the actor in the world which probably doesn't do what ever you are trying to do). So please tell us what exactly you are trying to compare.
danpost danpost

2016/8/10

#
Another set of issues resides with this line:
1
if ( color ==  Color (255, 216, 0))
'color' represents a Color type object, so the expression on the right must represent the same exact object (which, obviously, it does not). Also, the compiler will not like the expression on the right; it will look for a method called 'Color' as written (using 'new Color(255, 216, 0)' is what would be needed there; but, still, it will never be the same object as 'color'). To compare different Color object for the same internal color values, use the 'equals' method:
1
if (color.equals(new Color(255, 216, 0)))
Wolfpack9 Wolfpack9

2016/8/10

#
Thank you so much! I am new to this. Although this line won't compile due to the variables. int color = getImage().getColorAt(int fds,int zxc); Also I am trying to see if the body is a certain body so I can apply different things to that. I was going to use color because I realised that its different among the Bodies. also when I call the method it doesn't let me because of the parameters. public void stopSun (Body other)
danpost danpost

2016/8/10

#
You do not, and should not, declare the types of parameters when calling a method (your variables are already declared; you do not want to create new, uninitialized, ones). Also, the return type of the 'getColorAt' method is not an 'int' value, but an object of type Color. The line should be:
1
Color color = getImage().getColorAt(fds, zxc);
This declares a variable (object reference) of type 'Color' named 'color' and assigns it a Color object representing the color at (fds, zxc) in the current image of 'this' object (an object of the class the code is located in). Now, I get the impression that it is in the Sun class and it is 'other' (a Body type object brought in by the parameter of the 'stopSun' method that you want the color of. As an example, below is shown how to get the color at the center of the image of the 'other' object:
1
2
3
4
GreenfootImage bodyImg = other.getImage();
int x = bodyImg.getWidth()/2;
int y = bodyImg.getHeight()/2;
Color color = bodyImg.getColorAt(x, y);
The location of an actor in a world is irrelevant as far as where a color is within the image of that actor.
You need to login to post a reply.