Search code examples
javaswinggraphics2daffinetransform

can't get my coordinates graphics2D mouseclick java


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 !!!


Solution

  • 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.