Mouse pointer detection over a Path2D

北慕城南 提交于 2020-01-01 09:44:14

问题


I have constructed a Path2D that represents an unclosed shape consisting of straight lines:

I want to be able to detect when the mouse is clicked and the mouse pointer is near to (within a few pixels of) the path. Using the contains method does not work because the algorithm treats the unclosed shape as implicitly closed (i.e. by drawing a straight line between the start and end points).

Does anyone know of another mechanism for achieving this?


回答1:


  1. Create a BasicStroke (the width controls your pixel-distance-tolerance)
  2. Don't draw with it, only use its createStrokedShape method to create a second shape from your shape. This second shape describes the outline of the shape that would be filled if you would draw your first shape with the BasicStroke.
  3. Use the contains method of this second shape

From Stroke.createStrokedShape API documentation:

Returns an outline Shape which encloses the area that should be painted when the Shape is stroked according to the rules defined by the object implementing the Stroke interface.



来源:https://stackoverflow.com/questions/12068726/mouse-pointer-detection-over-a-path2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!