The effect I've been trying to create is that the mouse cursor icon changes whenever the mouse enters a certain zone on a JPanel, and switches to default whenever it leaves the zone. I'm using the MouseMoved feature in the MouseMotionListener class, which whenever the mouse moves over the JPanel verifies if the coordinates correspond to the special area.
However, the strain on the computer's processor is very high with this method, so I wanted to ask if there's a more efficient way of doing it. Any help is appreciated.
What the program does is it draws figures on a JPanel, and when the Connection button is selected then it connects those figures with a line if the user clicks on one figure, and then on another.
The figures are drawn on the JPanel, and have their respective region boundaries stored, so when the mouse moves it checks if the current X and Y coordinates are inside one of those regions, and if so changes cursor. The checkValidConnectionRegion checks to see if the current X and Y variables are inside a figure's region. Here's the code for the handler:
public void mouseMoved(MouseEvent e)
{
if(GUI.Connectionbutton.isSelected())
{
x = e.getX();
y = e.getY();
checkValidConnectionRegion();
if(validConnectionRegion)
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
if(!validConnectionRegion)
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}