简体   繁体   English

使用 mouseentered 事件突出显示多边形

[英]Highlight polygons with mouseentered event

I am very new to Java Swing, and I am working on an assignment.我对 Java Swing 非常陌生,我正在做一个任务。 I have some polygons on my component.我的组件上有一些多边形。 When I entered in to a polygon it has to highlight (ie filled with some color).当我进入一个多边形时,它必须突出显示(即填充一些颜色)。

When I go to next polygon it has to highlight and previous one should be erased (ie normal state).当我 go 到下一个多边形时,它必须突出显示并且前一个应该被擦除(即正常状态)。 I found some examples but those are using "mousepressed" events, but mine is different.我找到了一些示例,但这些示例使用的是“mousepressed”事件,但我的不同。

Have you gone through the MouseListener/MouseMotionListener sections of the Swing tutorials?您是否浏览过 Swing 教程的 MouseListener/MouseMotionListener 部分? If not, and if you have nothing written yet, I suggest that you review the tutorials and look at using the MouseMotionListener.如果没有,并且您还没有写任何东西,我建议您查看教程并查看使用 MouseMotionListener。 You don't want to listen for mouseEntered but more likely mouseMoved.您不想听 mouseEntered 但更有可能是 mouseMoved。 A pseudocode example could be:伪代码示例可能是:

in MouseMotionListener or MouseAdapter
   mouseMoved method
      get position of mouse pointer via the MouseEvent parameter.
      For loop through list of Polygons 
         If mouse inside of polygon, highlight it.
         Else, un-highlight it.
      End for loop
   End of mouseMoved method.
end MouseMotionListener or MouseAdapter

java.awt.Polygon has a contains(double x, double y) method that returns true if the x,y mouse coordinates are inside the polygon. java.awt.Polygon 有一个contains(double x, double y)方法,如果 x,y 鼠标坐标在多边形内,则返回 true。

The (x,y) coordinates come from implementing a MouseMotionListener on the Container where you're drawing your shapes and in the implemented public void mouseMoved(MouseEvent e) method you have e.getX() and e.getY() to get the coordinates and check if they're in your polygon(s). (x,y) 坐标来自在要绘制形状的容器上实现MouseMotionListener并在实现的public void mouseMoved(MouseEvent e)方法中使用e.getX()e.getY()来获取坐标并检查它们是否在您的多边形中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM