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

2014/11/21

Click transparent image condition?

Dalvengyr Dalvengyr

2014/11/21

#
Guys, I have a situation here. I want to create an interactive menu. Each button is circular in shape. Let me give you an image: Pretend the black area is button, the white area is transparent. I want a condition that returns false when I click at white (transparent) area but returns true if I click at the black (button) area. Can somebody help me with this? Thanks for any response ;)
danpost danpost

2014/11/21

#
What you really are trying to say is: if the mouse is clicked within radius distance of the location of the circle (or actor), then do whatever. I hope putting it that way helps.
Dalvengyr Dalvengyr

2014/11/22

#
Well, in that case you are right? But how if the shape is neither circle or square? Isn't there really a condition whether an image part is transparent or not? Something like getColor() == Color.TRANSPARENT?
danpost danpost

2014/11/22

#
Dalvengyr wrote...
Well, in that case you are right? But how if the shape is neither circle or square? Isn't there really a condition whether an image part is transparent or not? Something like getColor() == Color.TRANSPARENT?
I do not think there is a TRANSPARENT field in the Color class; so, you will have to check the alpha color part itself. And to clarify, there is a 'getColorAt' method in the GreenfootImage class to return the color at a specific pixel in the image.
Dalvengyr Dalvengyr

2014/11/23

#
When I checked the color at transparent part it returns (in string) "java.awt.color = R G B". Not exactly like that actually. Now I have found a way to trick it out but the condition still returns true if the color is pretty close to black e.g. R G B Yeah, I'm comparing the returned string length. Hope you get it ;)
danpost danpost

2014/11/23

#
To see if a Color object is transparent, use the object method 'getAlpha' of the Color class on it and check for a zero value.
Dalvengyr Dalvengyr

2014/11/24

#
danpost wrote...
To see if a Color object is transparent, use the object method 'getAlpha' of the Color class on it and check for a zero value.
how to use it? Thanks for replying anyway :)
danpost danpost

2014/11/24

#
Dalvengyr wrote...
how to use it?
int transparencyValue = /* a  Color object */.getAlpha();
The value returned by 'getAlpha' will be in the range between 0 (inclusive) and 256 (exclusive).
Dalvengyr Dalvengyr

2014/11/24

#
color object like
Color color = someImage.getColor()
?
danpost danpost

2014/11/24

#
Dalvengyr wrote...
color object like
Color color = someImage.getColor()
?
An image is an array of colors; so, you cannot just 'getColor' from an image. You need to specify which pixel in the image you want to get the color from:
Color color = someImage.getColorAt(/* x and y coordinates within image */);
You need to login to post a reply.