How to get the cursor position in JAVA? (X,Y)

Discussion in 'Programming General' started by dirtycommando, Dec 16, 2008.

How to get the cursor position in JAVA? (X,Y)
  1. Unread #1 - Dec 16, 2008 at 11:31 AM
  2. dirtycommando
    Joined:
    Feb 9, 2007
    Posts:
    8
    Referrals:
    0
    Sythe Gold:
    0

    dirtycommando Newcomer

    How to get the cursor position in JAVA? (X,Y)

    Hi all,

    I have searched all over and simply couldn't find it. I am looking for a way to get the cursor position in X and Y coordinates relative to the screen. I have searched google and sythe for some sort of API but to no avail. I was expecting to find something as simple as getCursorPos(int,int); But i was mistaking...

    So what I was wondering is if someone could possibly point me in the right direction or show me some sort of example implementation on how to obtain X and Y screen coordinates.

    But I am also wondering at this point, Does an API exist? Or must this be done through JNI?

    Thank you =).


    -Dirty
     
  3. Unread #2 - Dec 16, 2008 at 11:38 AM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    How to get the cursor position in JAVA? (X,Y)

    First you need:

    Code:
    import java.awt.*
    Then use:

    Code:
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b  = a.getLocation();
    int x = (int)b.getX();
    int y = (int)b.getY();

    I think that works, but i have not done any mouse stuff in awhile so i may be mistaken.
     
  5. Unread #3 - Dec 16, 2008 at 1:20 PM
  6. dirtycommando
    Joined:
    Feb 9, 2007
    Posts:
    8
    Referrals:
    0
    Sythe Gold:
    0

    dirtycommando Newcomer

    How to get the cursor position in JAVA? (X,Y)

    Hey thank you for your response, but I am getting a symbol not found error on basically every method in the source. Maybe I am just implementing it wrong...Could you show some sample implementation?
     
  7. Unread #4 - Dec 21, 2008 at 7:19 PM
  8. Buzzyboy
    Joined:
    Nov 28, 2008
    Posts:
    15
    Referrals:
    2
    Sythe Gold:
    0

    Buzzyboy Newcomer

    How to get the cursor position in JAVA? (X,Y)

    Code:
    import java.awt.event.*;
    
    public class myExample extends Frame implements MouseMotionListener {
    	int mouseX=0,mouseY=0;
    	myExample() {
    		addMouseMotionListener(this);
    		reshape(100,100,100,100);
    	}
    	public void mouseMoved(MouseEvent e) {
    		mouseX=e.getX();
    		mouseY=e.getY();
    		System.out.println("You clicked on "+mouseX+","+mouseY);
    	}
    	public void mouseEntered(MouseEvent e) {
    	}
    	public void mouseExited(MouseEvent e) {
    	}
    	public static void main(String[] args) {
    		myExample frame=new myExample();
    		frame.setVisible(true);
    	}
    }
    
    just a basic example. but you should really try google next time!
     
  9. Unread #5 - Dec 22, 2008 at 12:08 PM
  10. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    How to get the cursor position in JAVA? (X,Y)

    Sorry, I fucked up... the import was wrong... it should be fixxed now... here is an example that constantly prints the position of your mouse (every 1 sec)

    Code:
    import java.awt.*;
    
    public class GetMouse
    {
    	public static void main(String[] args)
    	{
    		while (true)
    		{
    			PointerInfo a = MouseInfo.getPointerInfo();
    			Point b = a.getLocation();
    			int x = (int) b.getX();
    			int y = (int) b.getY();
    			System.out.println(x + ":" + y);
    			try
    			{
    				Thread.sleep(1000);
    			}
    			catch (InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
     
< Uploading a Site. Looking for some Help. | Lolcode >

Users viewing this thread
1 guest


 
 
Adblock breaks this site