0

我正在用java开发一些可视化。我有一个问题。我正在我的程序中绘制一个 Arc2D,我想在我的 Arc2D 中添加一个 MouseInputListener。问题是,Arc2D 对象在一个矩形上延伸,而不仅仅是正在绘制的线。因此,当我想获得鼠标悬停效果时,它适用于整个矩形,而不仅仅是 Arc2D 的线。你们有没有解决这个问题的方法?

我的代码如下所示:

final Arc2D arc =
   new Arc2D.Double(x_pos, 10, x2_pos-x_pos, 190, 0, 360, Arc2D.OPEN);
JPanel jp = new JPanel();
addMouseInputListener(new MouseInputAdapter() {
   @Override
   public void mouseMoved(MouseEvent e) {
      System.out.println( "X: " + e.getX() + " Y: " + e.getY() +
         " Does it touch the arc?: " + arc.contains(e.getX(), e.getY()));
   }
   // More MouseInputListener methods... 
});
ga.draw(arc);
4

1 回答 1

0

You need to calculate the distance of the point from the curve. Then see if it's within an appropriate distance from the curve. You wouldn't want a hotspot that's only 1 pixel thick?!

To do this is not as easy as it might seem. you'll need to take

rsquared = pow((e.getX()-circle_centre.x),2) + pow(e.getY()-circle_centre.y,2);

and then see if it lies in an interval

rsquared<circle_radius+threshold && rsquared>circle_radius-threshold

And then check the angle

angle = Math.atan2(e.getX()-circle_centre.y, e.getY()-circle_centre.y);

which must lie in the interval you want as well.

于 2013-02-16T14:56:07.380 回答