squirt706
squirt706

Reputation: 33

How do I get my mouse coordinates relative to the window rather than the screen?

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.

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.

Upvotes: 2

Views: 2229

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

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.

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!

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...

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

where evt is the MouseEvent

Upvotes: 1

Jonah Haney
Jonah Haney

Reputation: 521

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

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:

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

Upvotes: 2

Related Questions