Results 1 to 4 of 4
- 04-24-2010, 04:28 PM #1
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); } }
-
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.
- 04-24-2010, 04:46 PM #3
-
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.
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.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.
Similar Threads
-
using the mousemotionlistener and mouselistener
By ravirajparab in forum New To JavaReplies: 1Last Post: 12-21-2009, 07:11 PM -
MouseListener & GUI
By Suurisa in forum New To JavaReplies: 2Last Post: 10-27-2009, 12:52 AM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM -
I need help with my MouseMotionAdapter and MouseListener.
By MurderfaceX4 in forum New To JavaReplies: 1Last Post: 12-07-2007, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks