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.

Results 1 to 11 of 11

Thread: Adding a mouse listener to my Draughts game

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Adding a mouse listener to my Draughts game

    EDIT: CODE REMOVED
    Last edited by MXA92; April 25th, 2012 at 05:25 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Adding a mouse listener to my Draughts game

    I get an error
    Please post the full text of the compiler's error messages.

    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.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a mouse listener to my Draughts game

    EDIT: Code Removed
    Last edited by MXA92; April 25th, 2012 at 05:25 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Adding a mouse listener to my Draughts game

    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.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a mouse listener to my Draughts game

    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Adding a mouse listener to my Draughts game

    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.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a mouse listener to my Draughts game

    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 :/

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Adding a mouse listener to my Draughts game

    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.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a mouse listener to my Draughts game

    EDIT: CODE REMOVED
    Last edited by MXA92; April 25th, 2012 at 05:26 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Adding a mouse listener to my Draughts game

    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.

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a mouse listener to my Draughts game

    Oh yeah, that was substantially better:

    public void mouseEntered(MouseEvent e)
        {
             ((Square)e.getSource()).enter();
        }

    Thank you again Norm !

Similar Threads

  1. implement mouse listener class
    By vnsgbt in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2011, 06:06 PM
  2. Adding sound to a game application
    By Shaybay92 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 07:56 AM
  3. [SOLVED] Is it possible for a mouse listener to exist without a GUI interface?
    By techwiz24 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 3rd, 2011, 02:32 PM
  4. Listener Configurations in DD
    By tcstcs in forum Java Servlet
    Replies: 0
    Last Post: August 3rd, 2011, 01:47 AM
  5. Key listener help
    By airsim15 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 13th, 2009, 02:15 PM