0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ This question is not really specific to game development. You usually get faster and better answers to such general programming questions on stackoverflow.com \$\endgroup\$ – Philipp Oct 10 '17 at 10:34
0
\$\begingroup\$

I assume the gameplay class holds the object(s) that should change the image. You don't mention if only one object should listen to the mouse or more of them.

From the given code you will have to pass the mouseX and mouseY to the towards the object that should change.

e.g.

obj.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseMoved(MouseEvent e) {
            gameplay.mouseMoved(e.getX(),  e.getY())
        }
    });

Within gameplay you could either pass it to every object, to only the objects that should listen to the mouse or just the one that listens.

All objects (which react to the mouse) then would have a MouseMoved(int x, int y) method. And all of theses objects need to know their bounds (position x, position y, width, height) and their current state if the mouse is currently pointing on them.

Then you have in each object these methods and members:

private boolean isMouseInside = false;
private int posX, posY, width, height;

public void mouseMoved(int x, int y) {
    boolean isInside =
        x > posX
      && x < posX + width
      && y > posY
      && y < posY + height;

    if (isInside && !isMouseInside) {
       //mouse just entered
       isMouseInside = true;
       OnMouseEnter();
    } else if (!isInside && isMouseInside) {
       isMouseInside = false;
       OnMouseExit();
    }
}

public void OnMouseEnter() {
     settingsImage = new ImageIcon(getClass().getResource("/assets/settings.png"));
settingsImage.paintIcon(this, g, 700, 23);
}

public void OnMouseExit() {

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.