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 3 of 3

Thread: Tracking mouse movements

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tracking mouse movements

    I was wondering if anyone might be able to help me track where my mouse clicks in the window.

    I know i need to use the mouselistener, but otherwise im lost as to how to get the coordinates to print out in the console.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Tracking mouse movements

    See How to write a Mouse Listener, in particular scroll down to the MouseEvent class description and its location getter functions, such as getPoint()

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Tracking mouse movements

    So you will need to create a MouseListener (I would advise a MouseAdapter) and implement the mouseClicked(MouseEvent e) method. Then inside of that method you can get the coordinates two ways. The first is using the MouseEvent.getX() method and MouseEvent.getY() method, whose values return an int. The second is by using the MouseEvent.getPoint() method, which returns a java.awt.Point.

    Based on personal experience, I wouldn't use the mouseClicked(MouseEvent e) method. I have found it to be very poor with its consistency. I think it is the way a mouseClick is defined or something compared to a mousePress. I usually use the mousePress(MouseEvent e) or mouseRelease(MouseEvent e) methods.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. [SOLVED] mouse getX and getY are off?
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2010, 01:49 AM
  3. Motion Tracking Questions
    By Brt93yoda in forum Java Theory & Questions
    Replies: 6
    Last Post: August 25th, 2010, 11:19 PM
  4. [SOLVED] Help:File Tracking Application..
    By Cross`17 in forum Java Networking
    Replies: 4
    Last Post: April 20th, 2010, 08:32 AM
  5. Libraries for User Tracking
    By jwpa in forum The Cafe
    Replies: 4
    Last Post: August 4th, 2009, 10:32 AM