通过Path2D检测鼠标指针 [英] Mouse pointer detection over a Path2D

查看:213
本文介绍了通过Path2D检测鼠标指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个方法不起作用,因为算法将未闭合的形状视为隐式闭合(即通过在起点和终点之间画一条直线)。



有没有人知道实现这个的另一种机制?

解决方案


  1. 创建一个BasicStroke(宽度控制你的像素距离容差)

  2. 不用它绘制,只使用其createStrokedShape方法从你的形状创建第二个形状。如果您使用BasicStroke绘制第一个形状,则第二个形状描述将填充的形状轮廓。

  3. 使用第二种形状的contains方法

来自Stroke.createStrokedShape API文档:


返回一个轮廓Shape,它包含根据$ b定义的规则描绘Shape时应该为
绘制的区域$ b实现Stroke接口的对象。



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. 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.

这篇关于通过Path2D检测鼠标指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆