Jake Norris
Jake Norris

Reputation: 11

Java MouseMotionListener

I am working on a Reversi game in java and one thing I am doing is making it so the background color of a space turns green if the move is valid. I want to do this by having it turn green when the player puts his mouse over the space, but I am having trouble figuring out how to make the color go back to the default when the mouse is removed from the space. Here is my code, it changes the color to red for now:

gameSpacePanel.addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                gameSpacePanel.setBackground(Color.RED);
            }
            public void mouseExited(MouseEvent e) {
                gameSpacePanel.setBackground(Color.GRAY);
            }
        });

I tried the mouseExited method, but apparently that doesn't do what I thought it would do. Any suggestons? the mouseMoved method works fine, I just don't know how to make the color go back to normal when the mouse is removed. Thanks!

Upvotes: 1

Views: 1595

Answers (2)

BK Batchelor
BK Batchelor

Reputation: 457

A MouseMove event is being fired on every mouse movement. Correct me if I am wrong, you want the mouse to change color on entering and change back to default color on exit? First MouseMotionListener does not have mouseExited method, instead use MouseListener, then replace

void mouseMoved(MouseEvent e) 

with

void mouseEntered(MouseEvent e) 

It should look something like this:

gameSpacePanel.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                gameSpacePanel.setBackground(Color.RED);
            }
            public void mouseExited(MouseEvent e) {
                gameSpacePanel.setBackground(Color.GRAY);
            }
        });

Upvotes: 2

Andrei Vajna II
Andrei Vajna II

Reputation: 4842

I just tried this and got the same result as you. But then I realised you only added the MouseAdapter as a MouseMotionListener. You also have to add it as a MouseListener too, because mouseExited() is part of that interface, while mouseMoved() is part of MouseMotionListener.

Here is a short program which works:

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                final JPanel panel = new JPanel();
                MouseAdapter mouseAdapter = new MouseAdapter() {
                    public void mouseMoved(MouseEvent e) {
                        panel.setBackground(Color.RED);
                    }
                    public void mouseExited(MouseEvent e) {
                        panel.setBackground(Color.GRAY);
                        panel.repaint();
                    }
                };
                panel.addMouseListener(mouseAdapter);
                panel.addMouseMotionListener(mouseAdapter);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 1

Related Questions