This site requires JavaScript, please enable it in your browser!
Greenfoot back
CreatorMoon
CreatorMoon wrote ...

2021/2/14

How do I get the location of the mouse?

CreatorMoon CreatorMoon

2021/2/14

#
I'm struggling to find a way to get the X and Y co-ordinates of the mouse, as I want to display them on the screen, but I'm getting an error with the showText. I'm probably missing something obvious, because I haven't used Greenfoot to code in two years. Can anyone help? My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
public class MouseInfo
extends java.lang.Object
{
  public int getX;
  public int getY;
  public MouseInfo()
  {
       
  }
  public void act()
  {
    location();//location code
  }
  public void location()
  {
    showText("X: "+getX, 50, 50);
    showText("Y: "+getY, 100, 50);
  }
}
I'm not sure if I need to add anything into another class or subclass, so I haven't yet.
RcCookie RcCookie

2021/2/14

#
First of all, you shouldn’t name your class „MouseInfo“ because that’s how the class from Greenfoot is named. Also, you don’t have to extend from Object, that happens automatically. Right now what happens is: - Two variables named getX and getY are declared but not initialized, so they are 0. - You call showText() even though there is no such method in that class. You pass the value of getX and getY which are unchanged, so still 0 „showText()“ is a method of the class World, more specifically greenfoot.World. So you need to call this method on a world instance (the instance to show the text on - there could be multiple worlds). For that you have two options: - Pass a reference to the world in the constructor to tell the object what world to show the text on, or - make your class extend World so that it is the world itself. I recommend the second, because it makes updating the text on every frame easier. Now for how to get the mouse coordinates: Greenfoot offers the class MouseInfo which represents the state of the mouse at a specific point in time. To get the current MouseInfo you can use
1
Greenfoot.getMouseInfo();
If you want to know the mouse‘ coordinates, you can use:
1
2
3
4
5
MouseInfo mouse = Greenfoot.getMouseInfo();
if(mouse != null) { // May be null if the mouse is outside of the world bounds
    int x = mouse.getX();
    int y = mouse.getY();
}
If you simply want to display the mouse coordinates in any world, you can put this stuff into the act method of any subclass of World without the need of an additional class:
1
2
3
4
5
// Put into act method to let this be updated every frame
MouseInfo mouse = Greenfoot.getMouseInfo();
if(mouse != null) {
    showText("Mouse is at [" + mouse.getX() + "| " + mouse.gerY() + "] ", getWidth() / 2, getHeight() / 2);
}
This will print the current mouse location the center of the world. One more thing: You usually name variables „x“ or „y“. If you want to have control over if and how that variable can be changed from outside the class, you make it private and create a get and a set method. For example:
1
2
3
4
5
6
7
8
9
10
private int x;
 
public int getX() { // Simply returns the value of x
    return x;
}
 
public void setX(int x) { // Sets the value of x to the given one
    if(x < 0) x = 0; // Now we have control so that x can never be set to a negative number
    this.x = x;
}
CreatorMoon CreatorMoon

2021/2/14

#
Thank you! I will test this out and see if it works.
CreatorMoon CreatorMoon

2021/2/14

#
Ok that works fine, thanks so much :D
You need to login to post a reply.