有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Swing:在generalpath上检测鼠标移动

我正在使用GeneralPath绘制一些样条曲线,如下所示:

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

基本上,这会在点(x0;y0)和点(x1;y1)之间绘制一条看起来像电缆或导线的样条曲线

p0 --
     \
      \
       `-- p1

绘图没有问题,但现在我想检测鼠标何时移动并悬停此样条线。问题是contains(Point)方法似乎没有达到我想要的效果。如果Point在样条曲线和(x0;y0)和(x1;y1)之间的直线之间的区域内,则返回true

在上面的示例中,对于整个“stars”区域,它将返回true:

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

但只希望检测点何时精确位于样条曲线上

有什么方法可以实现我想要的吗

非常感谢您的任何想法

编辑: 我在文档中找到了问题的解释,但这无助于我找到解决方案:

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.


共 (1) 个答案

  1. # 1 楼答案

    做一个弯回到起点。但是对于用户来说,让准确地越过你的曲线将是非常非常困难的