1

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!

asked Mar 27, 2015 at 18:57
1
  • 1
    question in this form isn't answerable without an SSCCE/MCVE, short, runnable, compilable, basics mouse events with Color works for me, issue could be with JLabel Commented Mar 27, 2015 at 19:08

2 Answers 2

2

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);
 }
 });
answered Mar 27, 2015 at 19:31
Sign up to request clarification or add additional context in comments.

Comments

1

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);
 }
 });
 }
}
answered Mar 27, 2015 at 19:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.