Results 1 to 4 of 4
  1. #1
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default can't get x and y from mouselistener

    i'm very new to java 2d and i have the following problem with the code above: the code instantiate a jframe with a jpanel in it that paints a drawRect(50, 50, 50, 50). the jpanel also implements a mouselistener. now when i click the mouse button in the upper left corner of the rect i got back the relative x position of about 100 and the relative y position of y 100. i can't figure out why the returned positions are not about 50, where the rec was painted. other detail: the jpanel is positioned in BorderLayout.CENTER. need help.


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class PaintExample extends JFrame {
    
    	JLabel flabel;
    	JLabel inout;
    	getPanel panel = new getPanel();
    
    	public PaintExample() {
    		super("My Frame");
    		panel = new getPanel().newPanel();
    		Container contentPane = getContentPane();
    		contentPane.add(panel, BorderLayout.CENTER);
    	}
    
    	class getPanel extends JPanel implements MouseListener {
    
    		private int x = 50;
    		private int y = 50;
    		private int length = 50;
    
    		public getPanel() {
    			addMouseListener(this);
    		}
    
    		public getPanel newPanel() {
    			return this;
    		}
    
    		public int getX() {
    			return x;
    		}
    
    		public int getY() {
    			return y;
    		}
    
    		public int getLength() {
    			return length;
    		}
    
    		public void paint(Graphics g) {
    			super.paint(g);
    			g.setColor(Color.BLACK);
    			g.drawRect(50, 50, length, length);
    			repaint();
    		}
    
    		public void mousePressed(MouseEvent e) {
    			System.out.println("x = " + e.getX());
    			System.out.println("y = " + e.getY());
    		}
    
    		public void mouseClicked(MouseEvent e) {
    		};
    
    		public void mouseReleased(MouseEvent e) {
    		};
    
    		public void mouseEntered(MouseEvent e) {
    		};
    
    		public void mouseExited(MouseEvent e) {
    		};
    	}
    
    	public static void main(String[] args) {
    		PaintExample frame = new PaintExample();
    		frame.setSize(500, 500);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't run your code because I'm rounding at the hospital and don't have access to a Java compiler, and so I don't know why you're having the trouble you're having, but something about your code does worry me a bit: your class that extends JPanel is overriding JPanel's getX() and getY() methods (likely unknowingly), and this can cause bad side effects. I'd rename those methods to be sure that they don't override important methods. Fixing this may fix your problem as I'll bet that the MouseEvent object calls the JPanel's getX and getY methods when it calculates its own x and y position.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    I can't run your code because I'm rounding at the hospital

    i hope nothing bad. get well soon!

    thank a lot for your advice. i renamed my getters and now the positions are ok. so silly ... i didn't noticed in eclipse that i was overriding the methods.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by j2me64 View Post
    i hope nothing bad. get well soon!
    No, no, I'm not sick; I am a physician and as I'm on call this weekend, I'm stuck at work making rounds.

    thank a lot for your advice. i renamed my getters and now the positions are ok. so silly ... i didn't noticed in eclipse that i was overriding the methods.
    You're welcome. I don't think Eclipse will warn you about this, and the only reason that I know about it is because I've tripped over this problem myself a time or two.

Similar Threads

  1. using the mousemotionlistener and mouselistener
    By ravirajparab in forum New To Java
    Replies: 1
    Last Post: 12-21-2009, 08:11 PM
  2. MouseListener & GUI
    By Suurisa in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 01:52 AM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 04:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 09:10 AM
  5. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 04:13 AM

Posting Permissions

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