Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the x,y of an object in a canvas

Status
Not open for further replies.

Bong

Programmer
Dec 22, 1999
2,063
US
Greeting.

I am sorry if this is a stupid question but I'm very new to Java. I have an application that takes a file of number pairs (say, x-y data), plots an "x" at every point, does a least squares fit and draws the resulting line. I would like to be able to click on a point (say, an outlying "x") and report back the canvas-referenced coordinates (which I can then turn back into my data scale coordinates).

I tried adding "MouseListener" to my class:
"public class lsFit extends Frame implements ActionListener, MouseListener {" and then adding a method to handle mouse events:
"public synchronized void mousePressed (MouseEvent me) {
int x = me.getX();
int y = me.getY();
}"
but I got all kinds of compiler errors I didn't understand. Can anyone give me a simple example or some really rudimentary guidance?
Bob Rashkin
rrashkin@csc.com
 
Hi Bob,

I am not sure if the code you sent represents your entire class but it currently does not implement all the methods that are required to be an ActionListener and a MouseListener. The following implements all the methods required to compile.


import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;

public class LsFit extends Frame implements ActionListener, MouseListener {
public synchronized void mousePressed (MouseEvent me) {
int x = me.getX();
int y = me.getY();
}

public void mouseClicked(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void actionPerformed(ActionEvent e) {

}
}


If this is no help, please include some of the compile errors you are getting.

Regards,
scrat
 
That was it. Thanks. Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top