Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  getting level map coords with mouse click  (Read 368 times)
0 Members and 1 Guest are viewing this topic.
Offline rainey

Junior Newbie





« Posted 2013-08-28 13:44:27 »

Hello,

I am currently stumped with an issue that I have searched high and low for a solution to, but with no luck.
I have a top down map in java 2d which is larger than the view port (a JPanel extending Canvas). The map is 1920x, 1152y and my JPanel viewport is 1280x, 800y.
What I want to be able to do is click on the screen and get the world map x,y coords and not the screens x,y coords.

Currently, its fine until the viewport moves beyond the 1280x800 area of the world map, so that when I click the screen, instead of it returning the world map coords, say 1900x, 900y, its just returning the current x,y position of the viewport.

I'm sure there's a solution out there, but I'm reasonably new to games coding, so any help would be most gratefully received!
Thanks
Offline nerb
« Reply #1 - Posted 2013-08-28 13:56:41 »

Hi rainey,

A click will always return the position in screen coordinates. You need to add your viewport's position to the click coordinates. So for example, if your viewport is at (100, 150) in world coordinates, and your click is at (500, 800) in screen coordinates, then the position of the click in world coordinates is (500 + 100, 800 + 150), hence (600, 950).

Does that make sense?

nerb.
Offline rainey

Junior Newbie





« Reply #2 - Posted 2013-08-28 14:22:15 »

That makes perfect sense!

Now my next question is, how do I keep track of where my screen is at?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lcass
« Reply #3 - Posted 2013-08-28 14:31:25 »

hold on I have some code for this shizzle.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
PointerInfo a = MouseInfo.getPointerInfo();//two imports are needed here
     Point b = a.getLocation();
      mx = (int) b.getX() - frame.getX();//frame is your Jframe;
     my = (int) b.getY() - frame.getY();
      if (mx < 0) {//all of this is to stop out of bounds errors.
        mx = 0;
      }
      if (my < 0) {
         my = 0;
      }
      if (mx > width) {
         mx = 0;
      }
      if (my > height) {
         my = 0;
      }

Thats for use with JFrames if you are using an applet you will need to do this.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
PointerInfo a = MouseInfo.getPointerInfo();
      Point b = a.getLocation();
      Point c = this.getLocationOnScreen();//for getting the applets position.
     mx = (int) ((int) b.getX() -c.getX());//it returns a double so you need to cast it to an int.
     my = (int) ((int) b.getY() -c.getY());
      if (mx < 0) {//checks
        mx = 0;
      }
      if (my < 0) {
         my = 0;
      }
      if (mx > width) {
         mx = 0;
      }
      if (my > height) {
         my = 0;
      }
Offline roseslayer

Junior Member


Medals: 1



« Reply #4 - Posted 2013-08-28 14:33:40 »

That makes perfect sense!

Now my next question is, how do I keep track of where my screen is at?


With a getter, just put in the screen class:
1  
2  
3  
public int getXOffset() {
      return xOffset; //This is your X from the screen
}


This is only for the x, as you already can see Tongue
-RoseSlayer

Fundamentum W.I.P.
Offline nerb
« Reply #5 - Posted 2013-08-28 15:33:49 »

...until the viewport moves...

From that comment, it sounds like you are moving the screen around somehow. I guess you are using an x & y position/offset variable of some sorts? That's the variable that you want to use to keep track of your screen.

So you're going to want a reference to the object that holds the viewport's position variables. If that is your JPanel, then make sure whatever is handling the mouse clicks has a reference to the panel, either directly or indirectly. Then you can grab the x/y offset variables themselves, or use a getter method as roseslayer has pointed out.
Offline rainey

Junior Newbie





« Reply #6 - Posted 2013-08-28 15:54:32 »

Fantastic guys, thanks for all your help!

Much appreciated.
Rainey
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!