【问题标题】:How do I get my mouse coordinates relative to the window rather than the screen?如何获得相对于窗口而不是屏幕的鼠标坐标?
【发布时间】:2016-02-23 20:00:36
【问题描述】:

我正在尝试获取鼠标相对于窗口的坐标。 我知道我需要参考 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。

【问题讨论】:

标签: java jframe mouse mouselistener


【解决方案1】:

您可以使用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);
}

【讨论】:

  • 可以,但是我的框架的名称是什么?即我如何引用它?
  • 可能是this,因为你的课是extends JFrame
  • @squirt706 实际上,如果这是您要比较的框架,您可能需要引用 run
  • 好吧,这很有意义,你能给我提供一个如何引用它的代码示例吗? @Jonah_Haney 并感谢您的帮助
【解决方案2】:

首先,不要使用MouseInfo,这并不是获取鼠标位置的最佳选择,相反,首先将MouseListener 添加到PlatformGame,请参阅How to Write a Mouse Listener 了解更多详情.

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

现在,如果出于某种奇怪的原因,您仍想将 PlatformGame 坐标空间转换为框架的坐标空间,您可以使用简单的...

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

其中evtMouseEvent

【讨论】:

  • 非常感谢,这是一种更简单的方法!
  • 我是否可以在没有事件的情况下从鼠标侦听器中使用 e.getX()(例​​如:单击),我有以下内容使用鼠标坐标向那个方向射箭.我之前使用的有点偏离。我只需要随时访问鼠标 x 和 y
  • 可能是MouseMotionListener
  • 类似thisthisthisthis
猜你喜欢
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多