2

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.

squirt706
  • 33
  • 7
  • Start by having a look at [How to Write a Mouse Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html). Add the `MouseListener` to `PlatformGame` and this will trigger mouse events which are within the components coordinate space – MadProgrammer Feb 23 '16 at 21:47

2 Answers2

2

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);
}
Jonah Haney
  • 521
  • 3
  • 12
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.

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • thank you so much this is a much simpler way to make it work! – squirt706 Feb 24 '16 at 02:23
  • is it possible for me to use e.getX() from the mouse listener without having an event (ex: click), i have something that follows uses the mouse coordinates to shoot an arrow in that direction. what i was using before is slightly off. I just need to access mouse x and y at anytime – squirt706 Feb 24 '16 at 02:59
  • 1
    Maybe a `MouseMotionListener`? – MadProgrammer Feb 24 '16 at 03:35
  • 1
    Something like [this](http://stackoverflow.com/questions/26784303/java-move-image-towards-mouse-position/26791886#26791886) or [this](http://stackoverflow.com/questions/20129281/mouse-motion-listener-only-in-one-direction/20131064#20131064) or [this](http://stackoverflow.com/questions/15607427/java-make-a-directed-line-and-make-it-move/15607737#15607737) or [this](http://stackoverflow.com/questions/27260445/rotating-a-triangle-around-a-point-java/27260788#27260788) – MadProgrammer Feb 24 '16 at 05:02