问题
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 !!!
回答1:
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.
回答2:
I found these:
- http://www.javalobby.org/java/forums/t19387.html
- http://www.java.net/node/685054
Don't know if they will help or not.
回答3:
Its not so hard ;-)
When you repaint the Component save the
AffineTransform
after the transforming withg2.getTransform()
Then call the function
invert()
on itIn 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!
来源:https://stackoverflow.com/questions/11821381/cant-get-my-coordinates-graphics2d-mouseclick-java