can't get my coordinates graphics2D mouseclick java

假如想象 提交于 2019-12-18 09:36:41

问题


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 ;-)

  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!



来源:https://stackoverflow.com/questions/11821381/cant-get-my-coordinates-graphics2d-mouseclick-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!