Results 1 to 5 of 5
  1. #1
    Zypher is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Mouse Movement Code Help

    Hi, I have been working on this mouse movement code that detects which direction the mouse moves in hope to use it in a game. The only problem is that I can not figure out how to make it move to the other side of the screen so I could continue turning to the right or left without having to stop. How would I actually get the x and y coordinants of the mouse to change? Any help would be greatly appreciated.

    PHP Code:
    		if(x == xscreen){
    			//set x to 0
    			
    		}else if(x == 0){
    			//set x to xscreen
    			
    		}else if(y == yscreen){
    			//set y to 0
    			
    		}else if(y == 0){
    			//set y to yscreen
    			
    		}

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,345
    Rep Power
    5

    Default Re: Mouse Movement Code Help

    Are you asking how to cause the mouse to wrap around the screen (vertically or horizontally) without changing the direction of the mouse? I don't believe
    you can do it because the mouse stops generating events when it goes out of focus or reaches an edge of that which has the focus. Perhaps someone else may be able to offer a solution.

    Regards,
    Jim
    Last edited by jim829; 02-11-2014 at 12:55 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Zypher is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Mouse Movement Code Help

    Well with this program I was able to avoid using events completely by using an infinite loop. I have seen people use robots to move the mouse but I do not understand how to use one in this sense. The code is a little long because I was hoping to avoid the event statements but here is all of it:
    PHP Code:
    //Made by Zypher
    import java.awt.*;
    
    public class Mouseget {
    	
    	public static void main (String[] args){
    	java.awt.MouseInfo.getPointerInfo();
    	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    		
    		//finds size end of the screen
    		int xscreen = (dim.width);
            int yscreen = (dim.height);
            
            //these are what the current coordinates of the mouse are
            int currentx;
            int currenty;
            
            //this is what the mouse changes to
    		int x;
    		int y;
    		
    		//this is to make the loop think it is not infinite...
    		int loop = 20;
    		
    		//setting the location for the start of the program
    		Point p = MouseInfo.getPointerInfo().getLocation();
    		currentx = p.x;
    		currenty = p.y;
    		
    		
    		
    		/*Mouse loop for location*/
    		do
    		{
    		
    		//this gets the x and y and displays them
    		Point r = MouseInfo.getPointerInfo().getLocation();	
    		x = r.x;	//x variable of the mouse location
    		y = r.y;	//y variable of the mouse location				
    		
    		//printing the x and y variable for testing
    			//System.out.print(x);
    			//System.out.print(" , ");
    			//System.out.print(y);
    			//System.out.println("");
    		
    		//compares x variable
    		
    		if(x > currentx){
    			//comment this out when you make this into something else, this is just for example/testing
    			System.out.print("right, ");
    			
    			//Put code for moving right here
    			
    			
    		}else if(x < currentx){
    			//comment this out when you make this into something else, this is just for example/testing
    			System.out.print("left, ");
    			
    			//put code for moving left here
    			
    			
    		}else if(x == currentx){
    			//comment this out when you make this into something else, this is just for example/testing
    			System.out.print("no change, ");
    			
    			//no code, doesn't move.
    			
    			
    		}
    		
    		//Compares y variable
    		
    		if(y < currenty){
    			//comment this out when you make this into something else, this is just for example/testing
    			System.out.println("up");
    			
    			//put code for moving up here
    			
    			
    		}else if(y > currenty){
    			//comment this out when you make this into something else, this is just for example/testing
    			System.out.println("down");
    			
    			//put code for moving down here
    			
    			
    		}else if(y == currenty){
    			//comment this out when you make this into something else, this is just for example/testing
    			System.out.println("no change");
    			
    			//no code, doesn't move
    			
    		}
    		//this makes the mouse switch to the other side of the screen... I have not figured out how to do this yet. If someone comes up with a way to do this
    		//please inform me on the code that can do this
    		if(x == xscreen){
    			//set x to 0
    			
    		}else if(x == 0){
    			//set x to xscreen
    			
    		}else if(y == yscreen){
    			//set y to 0
    			
    		}else if(y == 0){
    			//set y to yscreen
    			
    		}
    		
    		
    		//setting the current location to the new variable
    		currentx = x;
    		currenty = y;
    		
    		
    		
    		//again, making the loop think it is not infinite
    		loop = loop - 1;
    		loop = loop +1;
    		
    		try 
    		//sleeping code, makes the delay
    		{Thread.sleep(5);}
    		catch (InterruptedException e){
    			e.printStackTrace();}
    
    		
    		}while (loop >1);			//this is the part that finishes the infinite loop
    		
    		
    	}}
    Thanks

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,345
    Rep Power
    5

    Default Re: Mouse Movement Code Help

    This code does not do what you said you wanted to do (based on my interpretation). All it does is get the current mouse movement and print it out. If you keep on moving the mouse to the right past the screen, it doesn't show the mouse was moved to the right once it at the right edge of the screen. So I guess I don't understand what you want to do. I thought you wanted to have a virtual infinite mouse pad in all directions. And when you exceeded the borders of the screen or window, the cursor or object you are moving would reappear on the opposite side.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zypher is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Mouse Movement Code Help

    I got i to work the way I needed it to. All i did was made the code around lines 97 into:
    PHP Code:
     if(x == xscreen){
    			 x = 0;
    			 robot.mouseMove(x, y);
    			
    		}else if(x == 0){
    			 robot.mouseMove(xscreen, y);
    			
    		}else if(y == yscreen){
    			 y = 0;
    			 robot.mouseMove(x, y);
    			
    		}else if(y == 0){
    			 robot.mouseMove(x, yscreen);
    			
    		}
    Thanks for your responses though! Sorry for being unclear.

Similar Threads

  1. Robot Mouse Movement. Please Help!
    By slugy12345 in forum New To Java
    Replies: 0
    Last Post: 09-23-2013, 03:48 AM
  2. I need help adding a mouse click event to my code
    By Djblacklantern in forum New To Java
    Replies: 1
    Last Post: 11-09-2012, 06:41 PM
  3. how to call mouse event code
    By such orb in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 11:11 PM
  4. Replies: 2
    Last Post: 03-10-2012, 04:26 AM
  5. Key/mouse movement
    By falkon114 in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 02:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •