简体   繁体   中英

Swing: detect mouse move on generalpath

I'm drawing some splines using GeneralPath as follow:

GeneralPath path = new GeneralPath();
path.moveTo(x0, y0);
path.curveTo(x0 + 100, y0, x1 - 100, y1, x1, y1);
((Graphics2D) g).draw(path);

Basically, this draws a spline that looks like a cable, or wire, between the point (x0;y0) and the point (x1;y1).

p0 --
     \
      \
       `-- p1

The drawing is okay, but now I would like to detect when the mouse moves hover this spline. The problem is that the contains(Point) method does not seems to do what I want. It returns true if the Point is inside the area between the spline and a direct line between (x0;y0) and (x1;y1).

In the example above, it will return true for the whole "stars" area:

p0 -----------
     *********\
         ******\
             ***\
                *\* 
                  \******* 
                   `------------- p1

But only want to detect when the point is exactly located on the spline.

Is there any way to achieve what I want ?

Any ideas greatly appreciated !

EDIT: I found the explanation of my problem in the documentation, but this does not help me to find a solution:

The contains and intersects methods consider the interior of a Shape to be the area it encloses as if it were filled. This means that these methods consider unclosed shapes to be implicitly closed for the purpose of determining if a shape contains or intersects a rectangle or if a shape contains a point.

Just do a curveTo back to the starting point. But it's going to be really, really hard for the user to get exactly over your curve!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM