Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 988 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2013-12-14 15: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: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-12-14 16: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 rankings
Offline CodeHead

JGO Coder


Medals: 40


From rags to riches...to rags.


« Reply #2 - Posted 2013-12-14 16: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: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


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

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

Offline CodeHead

JGO Coder


Medals: 40


From rags to riches...to rags.


« Reply #4 - Posted 2013-12-15 00: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-15 00:11:03 »

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

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #6 - Posted 2013-12-15 01: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: 66
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #7 - Posted 2013-12-15 01: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 Coder


Medals: 40


From rags to riches...to rags.


« Reply #8 - Posted 2013-12-15 01: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.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!