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

Thread: mouse getX and getY are off?

  1. #1
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default mouse getX and getY are off?

    I created a MouseMotionListener to detect when then mouse moves over a menu item. I noticed that the getX() and getY() are off a little bit. It seems like the Y is off by 20pixels or so, and the X is off by about 5-6. Here is my mouse code:
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;
     
    public class mouseMotion implements MouseMotionListener {
    	public void mouseMoved(MouseEvent e){
    		if (e.getX() >= 608 && e.getX() <= 640 && e.getY() <= 32) {
    			System.out.println("test");
    		}
    	}
    	public void mouseDragged(MouseEvent e){}
    }

    The image is 32x32pixels and is painted at (608,0), Which is why I assumed that I should check if the mouse is in (608-640,0-32)

    I set my JFrame's size with the following code to prevent this bug, but it seems that it's back...

    f.getContentPane().setPreferredSize(new Dimension(640,480));
    f.add(gui);
    f.addKeyListener(kb);
    f.addMouseListener(m1);
    f.addMouseMotionListener(m2);
    f.pack();
    f.setVisible(true);


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: mouse getX and getY are off?


  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: mouse getX and getY are off?

    Thank you so much! Should I also use keyListener on the JPanel?

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: mouse getX and getY are off?

    Only you can answer that.

    db

Similar Threads

  1. Replies: 8
    Last Post: April 21st, 2013, 09:20 AM
  2. 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
  3. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 09:45 AM
  4. mouse Released no work!!
    By frenkelor in forum AWT / Java Swing
    Replies: 1
    Last Post: January 23rd, 2010, 06:41 AM
  5. How to create a system wide mouse or keyboard hook?
    By Freaky Chris in forum Java Native Interface
    Replies: 17
    Last Post: June 17th, 2009, 02:06 PM