0
     String mouse = new String(getMouseX(), getMouseY());

    public void paintFrame(Graphics g) {

        g.drawString("mouse", 100,100);
}

Basically, I'm trying to display the X and Y coordinates of the mouse on the screen when the program is executed. I know this is wrong, but could someone point me in the right direction? I'm fairly new to Java and am still getting used to writing code.

RAS
  • 8,100
  • 16
  • 64
  • 86
jzacharia
  • 492
  • 1
  • 3
  • 14
  • What GUI framework are you working in? – Evan Knowles Apr 23 '14 at 08:22
  • you mean you want to draw the value of variable `mouse`, right? like: `g.drawString(mouse, 100,100);` notice: without quote – Yohanes Khosiawan 许先汉 Apr 23 '14 at 08:22
  • possible duplicate of [Get Mouse Position](http://stackoverflow.com/questions/1439022/get-mouse-position) – Max Apr 23 '14 at 08:22
  • @EvanKnowles no idea, how would I check? – jzacharia Apr 23 '14 at 08:23
  • @YohanesKhosiawan许先汉 I believe so. I want the screen to display X=*mouse x position here* and Y=*mouse x position here* – jzacharia Apr 23 '14 at 08:24
  • @Max I don't think that is a duplicate, I want something visible on my screen that updates as I move the mouse. – jzacharia Apr 23 '14 at 08:24
  • have you tried that? removing the quote..? does it work..? it "might" be your problem.. because I don't know your full code.. – Yohanes Khosiawan 许先汉 Apr 23 '14 at 08:25
  • @YohanesKhosiawan许先汉 Tried it, returned with 'no suitable constructor found for String(int,int)' as well as argument mismatch errors. – jzacharia Apr 23 '14 at 08:28
  • what is that? can you show me your code..? if you are trying this one: `g.drawString(mouse, 100,100);`, then the error like the one you mentioned is not possible.. – Yohanes Khosiawan 许先汉 Apr 23 '14 at 08:30
  • @YohanesKhosiawan许先汉 the code is a program that my Intro to Java professor had written, there's basically a section that he is having us input small snippets of code into, then recompiling the program and checking results. I've managed to get it to work though, thank you. – jzacharia Apr 23 '14 at 08:36

2 Answers2

1

If you want to draw the string 'mouse' at the current position of the mouse, try this:

public void paintFrame(Graphics g) {
    Point p = MouseInfo.getPointerInfo().getLocation();
    g.drawString("mouse", p.x, p.y);
}

Or if you want to draw a string containing the position of the mouse use:

public void paintFrame(Graphics g) {
    Point p = MouseInfo.getPointerInfo().getLocation();
    String mousePos = p.x +";"+ p.y;
    g.drawString(mousePos, 100, 100);
}

I'm not sure of what you exactly want, but the important aspect is that you can get mouse position by calling:

MouseInfo.getPointerInfo().getLocation();

This will return you a Point object, therefore you can refer to the x and y fields of this object: they are the coordinates of the current mouse position.

I hope this will help you.

WoDoSc
  • 2,598
  • 1
  • 13
  • 26
1

If you're using Swing, you can get the current relative position of your mouse in the window as a Point by

Point cursorPosition = MouseInfo.getPointerInfo().getLocation();

afterwards you can print out the X and Y position by

System.out.print("X: " + cursorPosition.x + " Y: " + cursorPosition.y);

or as you mentioned in your question you might want to do

String position = "X: " + cursorPosition.x + " Y: " + cursorPosition.y)";
g.drawString(position, 100, 100);
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • This worked. Thanks! `public void paintFrame(Graphics g) { String position = new String("X: " + getMouseX() + " Y: " + getMouseY()); g.drawString(position, 100, 100); }` – jzacharia Apr 23 '14 at 08:35