图像映射,如 Java swing 中的功能、Points 与 GeneralPath
我的目标是在 PaintComponent() 方法中使用 2D 图形绘制多个图像。但是,我不确定如何添加 MouseListener 以便每个图像都知道它是否被选中。
到目前为止,我的解决方案过于简单地记录鼠标单击的坐标,然后查看它们是否包含在每个图像的边界内。然而,对于具有更复杂边界的图像来说,这将很困难。
另一种选择是创建简单的形状并将它们放置在图像上,但同样具有更复杂边界的图像将很困难。在另一个关于 SO 的讨论中发现 这里,有人提到使用 GeneralPath 来绘制更复杂的图像形状。我从来没有玩过 is,但这似乎令人鼓舞。
在这两个选项中,什么似乎是最好的解决方案,或者还有其他建议
My gaol is to draw several images using 2D graphics in a paintComponent() method. However, I'm not sure how I could add a MouseListener such that each image would know if it was selected.
My solution thus far is too simply record the coordinates of the mouse click, and see if they are contained within the boundaries of each of the images. However this will be difficult with images that have more complex boundaries.
Another option would be too create simple shapes and place them over the images, but again images with more complex boundaries will be difficult. In another discussion on SO found
here, someone mentioned using GeneralPath to draw more complex shapes. I have never played with is, but this seems encouraging.
Of these 2 options, what seems to be the best solution, or are there other recommendations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你们的图像是一个一个地绘制的还是单独绘制的。
如果它们是单独绘制的,那么您应该在 JComponent 上进行自定义绘制。然后就可以使用GeneralPath进行绘图了。您还需要通过检查鼠标单击是否包含在 GeneralPath 中来实现 contains(...) 方法。如果 contains() 方法正确实现,那么 MouseListener 将正确响应。
这是一个更简单的例子:
Are you images painted on top of one another or are they drawn separately.
If they are drawn separately, then you should do the custom painting on a JComponent. Then you can do the drawing by using the GeneralPath. You will also need to implement the contains(...) method by checking if the mouseclick is contained in the GeneralPath. If the contains() method is implemented properly then the MouseListener will respond properly.
Here is a simpler example: