简体   繁体   English

摆动:检测鼠标在一般路径上的移动

[英]Swing: detect mouse move on generalpath

I'm drawing some splines using GeneralPath as follow: 我正在使用GeneralPath绘制一些样条线,如下所示:

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). 基本上,这将在点(x0; y0)和点(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. 问题是, contains(Point)方法似乎无法满足我的要求。 It returns true if the Point is inside the area between the spline and a direct line between (x0;y0) and (x1;y1). 如果Point在样条线与(x0; y0)和(x1; y1)之间的直线之间的区域内,则返回true

In the example above, it will return true for the whole "stars" area: 在上面的示例中,它将对整个“星星”区域返回true:

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. 包含和相交方法将Shape的内部视为其被包围的区域,就好像它被填充一样。 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! 但这对于用户来说,要完全超越您的曲线将非常非常困难!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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