permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes MouseMotionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseMotionListener" Watch "MouseMotionListener" New topic
Author

MouseMotionListener

Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
I am trying to write a JFrame program that writes the x and Y mouse coordinates. I am trying to use an inner class for the listener. The code below will compile but doesn't do anything. I am not sure what is needed to accomplish this.

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ June 27, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You have a naming conflict. Don't name your class the same name as a class in the standard API that you are also importing and using within the program.
This is your program with the name of the class changed:It would seem to work as you were expecting.
Making sense?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
The code compiles. Thanks for the obvious that I did not catch.
I want the coordinates to show up on the JFrame. How do I do this? Right know I have system.out.println and it's producing 0,0.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You could just override the paint method.
Note: I doubt that this program is going to function as you are hoping that it will. But, I may be wrong - it might be what you are expecting...
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
I am new to Java and this is my first attempt to a JFrame. I have done Frame but with checkbox, textbox ...
I took a applet and I am trying to copy that effect into a JFrame. How do I override the paint method to give me a close look of what I am trying to do.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What are you trying to do with these coordinates exactly? Do you want to display the coordinates of the cursor position at all times?
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
When the mouse travels over the JFrame I want the X and Y coordinates to show.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Your problem is that you aren't getting the coordinates of the mouse cursor. You're getting the coordinates of the top left corner of the JFrame relative position on the user's screen.
Try moving the JFrame around the screen and you'll see that the coordinates change predictably, based on the JFrame's position on the screen.
Take a good look at The API Documentation and see if you can't figure out a different strategy to get the coordinates of the mouse cursor.
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
I am trying to create JFrame, then add 2 mouse listeners. one in which to write as a seperate class and one as an inner class. The use System.out.println to print the coordinates which is recieved from the mouse event.
I hope this explains it better.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
A little bit more of a hint:
Take a good look at how you are getting the coordinates that you get. Whom are you asking for their coordinates with your two method calls?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I am trying to create JFrame, then add 2 mouse listeners. one in which to write as a seperate class and one as an inner class. The use System.out.println to print the coordinates which is recieved from the mouse event.
I don't know what you mean exactly.
You've already a MouseMotionListener that is an anonymouse inner class. With the changes that were suggested, you could use the MouseEvent to get and display the proper coordinates.
Were you wanting to develop a second application using a non-inner and/or a non-anonymous MouseMotionListener class to do the same thing? Or did you want a second listener to use with this application to do something else?
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
int mouseX = e.getX();
int mouseY = e.getY();
I thought the getY() and getX() would bring back the coordinates
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
This is the applet that works. I want to do the same but in a JFrame. I was trying to use an inner class and an adapter to create cleaner code.
public class MouseEvents extends Applet
implements MouseListener, MouseMotionListener {
String msg = "";
int mouseX = 0, mouseY = 0;
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me){}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
}
public void mouseMoved(MouseEvent me) {
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}
public void paint(Graphics g){
g.drawString(msg, mouseX, mouseY);
}
}
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Sandy,
Note that you can preserve code formatting by surrounding the code with the [code] and [/code] UBB Tags.
Most all of the parts of the program that you are trying to put together are mentioned and described above. Let's cut to the chase:Is that what you were trying to do?
Sandy Schneider
Greenhorn

Joined: Mar 14, 2002
Posts: 22
Thanks Dirk
Yes, the code is pretty close to what I needed.
Thanks for your patience.
Sandy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MouseMotionListener
 
Similar Threads
93%
problem while creating sliding panel
JPanel painComponent not working
How to use menu event
Newbie problem with Event ... MouseListener