EDIT: CODE REMOVED
Printable View
EDIT: CODE REMOVED
Please post the full text of the compiler's error messages.Quote:
I get an error
On which component(s) do you want the mouse's movements detected? Add the listener to that/those component(s).
EDIT: Code Removed
After the call to setBackground you'll probably need to call repaint() to have the new color shown.
I've worked out I need the row and column of the square the mouse is on (using e.getX() and e.getY() somehow?) and then I can just call the enter() method.
ie. squares[1][2].enter() changes the dark square on row 1 and column 2 to yellow whenever the mouse enters any dark square..... I don't know how to change the specific dark square the mouse is on though as opposed to hard coding a square to change color... thank you
If the squares are components, the event object passed to the listener method could have a reference to it.
Look at the getSource() method.
Hmm, I'm not quite sure what you mean.
I need the mouse listener to call the enter() method for the specific Square object that has called it :/
Look at the API doc for the event object that is passed to the mouse listener methods. If you can get a reference to the component that created the mouse event, you could call its methods.
For a visual look, add a call to println in the mouse listener that prints out the event object that is passed to the listener method.
EDIT: CODE REMOVED
Why not cast the reference returned by getSource() to the component's class that created the event and call its method directly.
Oh yeah, that was substantially better:
Code :public void mouseEntered(MouseEvent e) { ((Square)e.getSource()).enter(); }
Thank you again Norm :)!