如何相对于窗口而不是屏幕获取鼠标坐标?

时间:2016-02-23 20:00:37

标签: java jframe mouse mouselistener

我试图获取鼠标相对于窗口的坐标。 我知道我需要引用JFrame或其他东西,但不能为我的生活弄清楚如何,我的课程为我提供了这个初学者代码并且我已经从那里构建了它。

public class TheGame extends JFrame 
{
public static final int WIDTH = 785;
public static final int HEIGHT = 670;
public static int width;
public static int height;
public static int borderX;
public static int borderY;

public TheGame()
{
    super("PLATFORMGAME");
    setSize(WIDTH,HEIGHT);

    PlatformGame game = new PlatformGame();

    ((Component)game).setFocusable(true);
    getContentPane().add(game);



    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main( String args[] )
{
    TheGame run = new TheGame();
}
}

这是我的鼠标监听器类:

public class mouse extends JFrame {

static Point p;
BufferedImage sprite;

public mouse() {
Point p = MouseInfo.getPointerInfo().getLocation();
try {
    sprite = ImageIO.read(new File("mouse_pointer.png")) ;
    } catch (IOException e1) {
        e1.printStackTrace();
}

}

public Point getlocation() {
 return p = MouseInfo.getPointerInfo().getLocation();
}

public static double getNorP(double uno, double dos) {
    if ((uno - dos) > 0) {return -1;}
    else {return 1;}

}
public static double getX(Char c) {
    return MouseInfo.getPointerInfo().getLocation().x ;
    //return  MouseInfo.getPointerInfo().getLocation().x -       ReferenceJFrame.getLocationOnScreen().x;

}
public static double getY(Char c) {
    return MouseInfo.getPointerInfo().getLocation().y -29;
    //return MouseInfo.getPointerInfo().getLocation().y - ReferenceJFrame.getLocationOnScreen().y;
}
public static double getXD(Char c) {
return Math.abs(c.rX-(double)getX(c));
}
public static double getYD(Char c) {
return Math.abs(c.rY-(double)getY(c));
}
    //  public static float getAngle(Point target,int x,int y) {
    //    return (float) Math.toDegrees(Math.atan2(getX()-x, getY()-y));
}
public static double getXP(Char c) {
    return (getXD(c))/(getXD(c)+getYD(c)) * getNorP(c.rX,getX(c));
}
public static double getYP(Char c) {
    return (getYD(c))/(getXD(c)+getYD(c)) * getNorP(c.rY,getY(c));
}

public void draw(Graphics window, Char c) {

    window.drawImage(sprite,(int) getX(c)-16,(int) getY(c)-8, 16, 16, null);

}

}

返回鼠标相对于屏幕的坐标,但不返回窗口。除了JFrame类型的东西,我理解了相当数量的java。

2 个答案:

答案 0 :(得分:2)

您可以使用返回JFrame对象的getLocation()方法Point。然后使用类似的东西:

mouseX = MouseInfo.getPointerInfo().getLocation().x - frame.getLocation().x;
mouseY = MouseInfo.getPointerInfo().getLocation().y - frame.getLocation().y;

在您的情况下,getMouseLocation()方法如下所示:

public Point getMouseLocation(){
    int x = MouseInfo.getPointerInfo().getLocation().x - run.getLocation().x;
    int y = MouseInfo.getPointerInfo().getLocation().y - run.getLocation().y;
    return new Point(x,y);
}

答案 1 :(得分:1)

首先,不要使用MouseInfo,这不是获取鼠标位置的最佳选择,而是首先向{{1}添加MouseListener有关详细信息,请参阅How to Write a Mouse Listener

当调用鼠标侦听器时,这将传递PlatformGame,该MouseEvent将位于MouseListener注册的组件的坐标空间内,因此,无需进行任何转换!

现在,如果由于某些奇怪的原因,你仍然希望从PlatformGame坐标空间转换为框架,你可以使用像......一样简单的东西......

evt = SwingUtilities.convertMouseEvent(evt.getComponent(), evt, SwingUtilities.windowForComponent(evt.getComponent()));

其中evtMouseEvent