Simple mouse coordinate code

Just a little snippet of code for getting accurate mouse coordinates within a JFrame.
It sets it to either 0 or frame.width/height if its out of bounds

PointerInfo a = MouseInfo.getPointerInfo();
		Point b = a.getLocation();
		mx = (int) b.getX() - frame.getX();
		my = (int) b.getY() - frame.getY();
		if (mx < 0) {
			mx = 0;
		}
		if (my < 0) {
			my = 0;
		}
		if (mx > WIDTH) {
			mx = 0;
		}
		if (my > HEIGHT) {
			my = 0;
		}

Not quite… :point:

Should be:


Point b = MouseInfo.getPointerInfo().getLocation();
mx = b.getX() - frame.getX();
my = b.getY() - frame.getY();

if (mx < 0) {
    mx = 0;
}
else if (mx > WIDTH - 1) {
    mx = WIDTH - 1;
}

if (my < 0) {
    my = 0;
}
else if (my > HEIGHT - 1) {
    my = HEIGHT - 1;
}

Or if you want to be a bit clever:


Point b = MouseInfo.getPointerInfo().getLocation();
mx = Math.min(Math.max(b.getX() - frame.getX(), 0), WIDTH - 1);
my = Math.min(Math.max(b.getY() - frame.getY(), 0), HEIGHT - 1);

Edit: Technically you should compare against WIDTH-1 and HEIGHT-1 otherwise your max extents are going to be 1px outside of your frame. (Gah, I need to drink more caffeine before posting in the morning.)

I remember seeing one post where they ‘discovered’ this. Mind was blown.

Discovered Math.min and Math.max, or discovered how to clamp values in general? ???

ahh sorry , I had my width values pre modified :smiley:

I imagine the mind blowing discovery was finding that an API existed for directly querying the mouse position.
I certainly didn’t know it existed.

Though I can’t really see too many useful applications for it.
I suppose MouseInfo.getNumberOfButtons() is a useful feature.

I’d have preferred the API exposed direct querying of HID devices, so support for multiple mice/keyboards/etc was possible (without 3rd party APIs)

That, for mouse coords, most people made the mistake of being one pixel off, so you need to subtract 1 from width and height. Everyone was just shocked, as they found it in multiple engines.

Ah, base 0 bugs. One of the hardest things to explain to someone who is new to programming. It’s always frustratingly fun to watch someone wrap their head around the concept that something which has a capacity of x actually has a maximum upper bound of x - 1. Visual Basic, which I do some work with at my 9 to 5, has the wonderful “Option Base” setting which lets you make your index referencing 0 or 1 based depending on what you set it to, except for cases where you’re making external API calls. Way to muddy the waters even more MS. Thankfully it defaults to base 0 so things work as expected most of the time. You still get those frustrating moments when you’re trying to debug some code while failing to realize that someone has changed the option. >:(

@Abuse: Good point. I hadn’t thought about that. I think I stumbled on it when working with Java’s Robot class to create a small application that would keep my coworkers computer from logging off due to inactivity. In that case I was searching for a way to set the mouse position.