javaswingawtutilities

Highlight polygons with mouseentered event


I am very new to Java Swing, and I am working on an assignment. I have some polygons on my component. When I entered in to a polygon it has to highlight (i.e. filled with some color).

When I go to next polygon it has to highlight and previous one should be erased (i.e normal state). I found some examples but those are using "mousepressed" events, but mine is different.


Solution

  • Have you gone through the MouseListener/MouseMotionListener sections of the Swing tutorials? If not, and if you have nothing written yet, I suggest that you review the tutorials and look at using the MouseMotionListener. You don't want to listen for mouseEntered but more likely 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