Chek Mouse pointer INSIDE a image...

hi all, somebody can tell me how I can check if the mouse pointer is inside a image (Gif), not rectangle, suppose I have a GIF with World Map and I have to stand out a specific country when mouse is over that country…

Of course countries not are rectangles, not circles…

so, how can I check if mouse pointer is in country boundaries???..

THANKS A LOT…

Well, you know the location of the upper-left hand corner of the image and it’s width and height, you can use these to build a rectangle.


if(mouse.getX() >= image.x && mouse.getX() <= (image.x + image.getWidth() &&
   mouse.getY() >= image.y && mouse.getY() <= (image.y + image.getHeight())
{
    // The mouse is inside the image
}

cborders suggestions is good! Also you could define a point (x, y) for each country and then check if the mouse is within range (Pytagoras):


int mouseX = ...
int mouseY = ...
int countryX = ...
int countryY = ...
int MAX_DISTANCE_SQUARED = 50 * 50;
if ((mouseX - countryX) * (mouseX - countryX) + (mouseY - countryY) * (mouseY-countryY) <= MAX_DISTANCE_SQUARED) {
   // mouse within 50 pixel radius of country (x, y)
}

but I don’t want country to be painted when pointer is in a rectangle, you know, country shape form can be no a rectabgle, suposse countre USA, and I put my mouse on alaska, I want alaska resalted, but if I make a rectangle, it will be painted when mouse is on sea, too… I dont want it…

Maybe I miss understood your answer…

how about having an image containing different colors for each country, then just check the color in the pixel under the mouse. you might want to have a different image used to actaully draw onto the screen tho, so that each country doesnt have to have all the same color on screen.

just see this image… so, I want each time mouse is over a countre, it show painted in blue for example… but only that country…

bad gif file… this is the one…

You are talking about a flood fill algorithm? I got one at: http://www.aevumobscurum.com/public/ (Open Source)

that’s good for painting, but how to detect when Mouse Pointer enters to country boundaries?, that’s what I do not know how to do…

there is no perfect solution. please read cborders and my post again …

Reread g666’s post.

You have one image as you’ve shown, but you also have a second image that has each country a different color. You never display the second image, you just use it to test country areas. Then you can test pixel colors.

You can describe the countries as geometric shapes using, for example, java.awt.geom.Area (basically as Shapes, or GeneralPaths) classes, and use hit testing of java.awt.geom.Shape interface.

Dmitri

The typical solution to this problem is a collision map.

This is simply additional information that is stored along with each pixel, to uniquely identify each pixel in an application specifiable manner.

I wrote this more than 3 years ago, for the exact same question on forums.java.sun.com.

http://www.pkl.net/~rsc/Riskthingy/RiskTypeMap.htm (source is :- http://www.pkl.net/~rsc/Riskthingy/RiskTypeMap.java )

It isn’t big, and it isn’t clever, but it demonstrates exactly what you want to do in a simple fashion.

As anonand others say, you go into your paint program, make a copy of your map and fill each region with a different solid color.

When yo uwant to know whether x,y is in a country, you get the pixel in that secdn image at that point and check the color.

[quote]As anon and others say, you go into your paint program, make a copy of your map and fill each region with a different solid color.
[/quote]
Just to say the obvious: You don’t have to display the colored map, just keep it in memory and use it to find the “country color” with the coordinates you get from the mouse listener, when the real image is clicked.