0

I'm hoping this is an easy question to answer. I am getting the mouse location via:

one1 = e.getX();
one2 = e.getY();

And I do this twice to get both the start and the end of the line, but when the line is drawn the coordinates are a good bit off. I haven't resized the window or anything?

g2d.drawLine(one1, one2, two1, two2);

Is there something extra I need to check or adjust before drawing the line?

James MV
  • 8,569
  • 17
  • 65
  • 96
  • 7
    Are you getting mouse coordinates relative to the canvas itself? If you have the mouse listener hooked on the parent JFrame, the coordinates will obviously be shifted. – Karel Petranek Dec 12 '11 at 19:06
  • Yes, your right charlie, the listener was on the frame no the JLabel, but I've corrected that and still getting off target lines? Is there anything obvious I might have missed? – James MV Dec 12 '11 at 19:41
  • Can you show your code in a short example? – Jonas Dec 12 '11 at 20:30
  • 1
    Yes you have missed posting your SSCCE! We are not mind reader and we can't guess what your code looks like. By the way what was wrong with the help you received in your last question related to this topic? Why haven't you accepted any of the answers. – camickr Dec 12 '11 at 20:30
  • Have done now camickr, takes me a while to work through it all and figure out what you geniuses are telling me! I'll try and show a short code example, but the whole program is spread across 7 or 8 classes and there is resizing of the image involved as well so its hard to show what's relevant. – James MV Dec 12 '11 at 22:01
  • *"its hard to show what's relevant"* If you take something out and the code starts working, it is relevant. Otherwise, no. – Andrew Thompson Dec 12 '11 at 22:35
  • Fixed now, thanks all (again). – James MV Dec 12 '11 at 22:42

1 Answers1

0

Got this sorted now, the comment that pointed out was the listener on the parent frame or the actual canvas. This was part of the problem, but also changing

one1 = e.getX();
one2 = e.getY();

to:

one1 = (int)e.getX();
one2 = (int)e.getY();

I don't really know why that makes a difference but it did.

James MV
  • 8,569
  • 17
  • 65
  • 96