Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple mouse coordinate code  (Read 1121 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2013-12-14 14:46:46 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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;
      }
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-12-14 15:14:41 »

It sets it to either 0 or frame.width/height if its out of bounds
Not quite... Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #2 - Posted 2013-12-14 15:50:02 »

Should be:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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:
1  
2  
3  
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);

 Cool

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.)

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 77
Projects: 4
Exp: 3 years


Radirius Games


« Reply #3 - Posted 2013-12-14 22:11:26 »

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

Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #4 - Posted 2013-12-14 23:03:16 »

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? Huh

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline lcass
« Reply #5 - Posted 2013-12-14 23:11:03 »

ahh sorry , I had my width values pre modified Cheesy
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #6 - Posted 2013-12-15 00:02:18 »

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? Huh

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)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline wessles

JGO Wizard


Medals: 77
Projects: 4
Exp: 3 years


Radirius Games


« Reply #7 - Posted 2013-12-15 00:15:55 »

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? Huh
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.

Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #8 - Posted 2013-12-15 00:30:01 »

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. Angry

@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.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!