Question

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.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top