我试图获取鼠标在面板中的位置,如面板左上角 = x/y 0,0。
我目前所拥有的给出了整个屏幕上的位置,因此根据面板(位于框架中)在屏幕上的位置,坐标是不同的。我想你可以添加 x/y 坐标来解决这个问题,但这似乎是一个混乱的解决方案。有人可以帮忙吗?
这是我正在使用的 mouseListener,它已添加到面板中。
private class MouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
// Finds the location of the mouse
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
// Gets the x -> and y co-ordinates
int x = (int) b.getX();
int y = (int) b.getY();
System.out.println("Mouse x: " + x);
System.out.println("Mouse y: " + y);
// Determines which tile the click occured on
int xTile = x/tileSize;
int yTile = y/tileSize;
System.out.println("X Tile: " + xTile);
System.out.println("Y Tile: " + yTile);
}
}
See 鼠标事件.getPoint() http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html#getPoint%28%29.
返回事件的 x,y 位置相对于源组件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)