EDIT: CODE REMOVED
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
EDIT: CODE REMOVED
Last edited by MXA92; April 25th, 2012 at 05:25 PM.
Please post the full text of the compiler's error messages.I get an error
On which component(s) do you want the mouse's movements detected? Add the listener to that/those component(s).
If you don't understand my answer, don't ignore it, ask a question.
EDIT: Code Removed
Last edited by MXA92; April 25th, 2012 at 05:25 PM.
After the call to setBackground you'll probably need to call repaint() to have the new color shown.
If you don't understand my answer, don't ignore it, ask a question.
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.
If you don't understand my answer, don't ignore it, ask a question.
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.
If you don't understand my answer, don't ignore it, ask a question.
EDIT: CODE REMOVED
Last edited by MXA92; April 25th, 2012 at 05:26 PM.
Why not cast the reference returned by getSource() to the component's class that created the event and call its method directly.
If you don't understand my answer, don't ignore it, ask a question.
Oh yeah, that was substantially better:
public void mouseEntered(MouseEvent e) { ((Square)e.getSource()).enter(); }
Thank you again Norm !