I am trying to make it so that when a user hovers over an image on my JPanel, the image turns into a different image. I have 2 classes, one for the JPanel and the other is for the gameplay (snake game). How do I implement the mouseListener to make it so that the image changes on hover?
snake.java (main):
public class snake {
public static int mouseX;
public static int mouseY;
public static void main(String[] args) {
// JFrame
JFrame obj = new JFrame("Snake");
gameplay Gameplay = new gameplay();
obj.setBounds(10, 10, 905, 700);
obj.setBackground(Color.DARK_GRAY);
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(Gameplay);
obj.setVisible(true);
obj.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
});
}
}
Image I want to change:
settingsImage = new ImageIcon(getClass().getResource("/assets/settings.png"));
settingsImage.paintIcon(this, g, 700, 23);
If you need to see any of my other code please tell me!