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

时间:2022-04-16 21:18:16

I am trying to get the coordinates of my mouse relative to the window. I understand that I need to reference the JFrame or something but cannot for the life of me figure out how, my class provided me with this beginner code and I have built it up from there.

我试图获取鼠标相对于窗口的坐标。我知道我需要引用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();
}
}

Here is my mouse listener class:

这是我的鼠标监听器类:

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

}

}

This returns the coordinates of the mouse relative to the screen but not to the window. I understand a fair amount of java except for JFrame type stuff.

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

2 个解决方案

#1


1  

First off, don't use MouseInfo, this isn't really your best choice for getting the mouse location, instead, start by adding a MouseListener to your PlatformGame, see How to Write a Mouse Listener for more details.

首先,不要使用MouseInfo,这不是获取鼠标位置的最佳选择,而是首先将MouseListener添加到PlatformGame,有关详细信息,请参阅如何编写鼠标侦听器。

This will, when the mouse listener is called, pass a MouseEvent which will be within the coordinate space of the component the MouseListener is registered to, so, no need to do any conversions whatsoever!

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

Now, if for some bizarre reason, you still want to convert from the PlatformGame coordinate space to the frame's, you could use something as simple as...

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

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

where evt is the MouseEvent

其中evt是MouseEvent

#2


2  

You can use JFrame's method getLocation() that returns a Point object. Then use something like:

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

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

In your case the getMouseLocation() method would look like this:

在您的情况下,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  

First off, don't use MouseInfo, this isn't really your best choice for getting the mouse location, instead, start by adding a MouseListener to your PlatformGame, see How to Write a Mouse Listener for more details.

首先,不要使用MouseInfo,这不是获取鼠标位置的最佳选择,而是首先将MouseListener添加到PlatformGame,有关详细信息,请参阅如何编写鼠标侦听器。

This will, when the mouse listener is called, pass a MouseEvent which will be within the coordinate space of the component the MouseListener is registered to, so, no need to do any conversions whatsoever!

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

Now, if for some bizarre reason, you still want to convert from the PlatformGame coordinate space to the frame's, you could use something as simple as...

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

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

where evt is the MouseEvent

其中evt是MouseEvent

#2


2  

You can use JFrame's method getLocation() that returns a Point object. Then use something like:

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

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

In your case the getMouseLocation() method would look like this:

在您的情况下,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);
}