1

I got an extended JLabel class where I draw my Map using the code below : the new AffineTransform() is the identity to left my image as it is (0,0,w,h)

 mygraphics2D = (Graphics2D) getGraphics();
 graphics2D.scale(2.0,2.0) ;
 graphics2D.rotate(....
 graphics2D.drawImage(myImageIcon.getImage(),new AffineTransform(), this);

now when I click on my JLabel using this event :

public void mouseClicked(MouseEvent e) {
x =e.getX() ;
y = e.getY();
NewX = ????
NewY = ????
}

I want to retrieve my new coordinates "the scaled,rotated ... coords" I tried

Point2D ptSrc = new Point2D.Double(x, y);
Point2D ptDst = new Point2D.Double(0, 0);
mygraphics2D.getTransform().transform(ptSrc, ptDst);

but the ptDst is different from the (scaled,rotated,..) coordinates, any help please !!!

S3ddi9
  • 2,121
  • 2
  • 20
  • 34
  • *I want to retrieve my new coordinates "the scaled,rotated ... coords"* Why? What relevance are they, or the scaling/rotation? What is the feature this is supposed to implement? – Andrew Thompson Aug 06 '12 at 00:47
  • I have a map that can be transformed (scale, rotates, translate), & I want to add objects by clicking on the `JLabel` so the new object will be located at the new coords – S3ddi9 Aug 06 '12 at 00:50
  • I think AndrewThompson is right. Do you *really* need to apply the transform when you click or can you just let the paint process take care of it for you? – MadProgrammer Aug 06 '12 at 00:51
  • For better help sooner, post an SSCCE. Even as code snippets go, those are not very instructive. *"I have a map that can be transformed (scale, rotates, translate)"* Since the image is not transformed in any way, it makes me wonder.. What part of the map **is** transformed? – Andrew Thompson Aug 06 '12 at 00:54
  • my map is `myImageIcon.getImage()` when i scale for example the coords changes of course, when i click on a specific region of the image i want to add my object there – S3ddi9 Aug 06 '12 at 00:57
  • a) That was as clear as mud. b) Now that two people have made comments, you need to prefix a comment with @MadProgrammer (or other name) to ensure the person is notified of the comment. – Andrew Thompson Aug 06 '12 at 01:16
  • @SéddikLaraba Are you *sure* `ptDst` doesn't contain the scaled/rotated point? `final Point transformed = graphics.getTransform().transform(e.getPoint(), null);` – obataku Aug 06 '12 at 01:28
  • may be i should use the inverted transformation @veer – S3ddi9 Aug 06 '12 at 01:51
  • @SéddikLaraba yes, sorry, I'm an idiot. I didn't quite realize what you were asking for :p – obataku Aug 06 '12 at 02:00

3 Answers3

4

It sounds like you need both a forward and inverse transform to translate between the two co-ordinate systems. In this example, the scaling equations are explicit; in this alternate approach, a second AffineTransform is used.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • that's exactly what I wanted, retrieve mouse coords from a scaled JPanel, I don't get why none understood that, even if its blod written ???, thank you @trashgod – S3ddi9 Aug 06 '12 at 02:25
  • You're welcome. Credit to commenters for elucidation and to @finw for a working example; many references merely suggest that it's possible. – trashgod Aug 06 '12 at 02:33
2

Its not so hard ;-)

  1. When you repaint the Component save the AffineTransform after the transforming with g2.getTransform()

  2. Then call the function invert() on it

  3. In the mouseClicked() event us the following code:

    Point2D p= trans.transform(new Point2D.Double(evt.getX(), evt.getY()), null);
    System.out.println("click x="+p.getX()+" y="+p.getY());
    

Thats it!

pknoe3lh
  • 374
  • 3
  • 10
  • Necro-whatever blah. This answer is still relevant in 2022! THANK YOU. I was having issues with the mouse after using affineTransform and couldn't get inverseTransform to work. The way you do it here pointed me in the right direction. Now inverseTransform in the mouseMoved method works perfectly. (It is advised to create the Point2D during initiation, then use setLocation to update it instead of creating a potential memory leak by creating a new Point2D during every mouse click or move event.) – Zizzyzizzy Jul 12 '22 at 17:03
1

I found these:

Don't know if they will help or not.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366