我一直在尝试创建的效果是,只要鼠标进入 JPanel 上的某个区域,鼠标光标图标就会发生变化,而只要它离开该区域,就会切换到默认值。我在 MouseMotionListener 类中使用 MouseMoved 功能,每当鼠标在 JPanel 上移动时,它都会验证坐标是否对应于特殊区域。
但是,这种方法对计算机处理器的压力非常大,所以我想问一下是否有更有效的方法。任何帮助表示赞赏。
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.
这些图形绘制在 JPanel 上,并存储了它们各自的区域边界,因此当鼠标移动时,它会检查当前 X 和 Y 坐标是否在这些区域之一内,如果是,则更改光标。checkValidConnectionRegion 检查当前 X 和 Y 变量是否在图形区域内。这是处理程序的代码:
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));
}
}