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.