Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Linking coordinates on a map to variables  RSS feed

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, as usual I'm not sure if this belongs here or with the noobs, but I think here will be fine. Otherwise please move me.

I would like to create a GUI where the user can click a point on a map and the program can change variables depending on where on the map the user clicks.

More specifically I would like to use this map:

http://www.deresportal.de/images/Gallerie/landkarte_aventurien_hex.jpg

It's from a fantasy role playing game, yes, I know. There HAVE to be rpg-nerds other than me among the computer nerds here, right?

I want the user to click for example on the city of Riva (in the north) and then change whatever variable is associated with Hex X13. Probably a boolean. Something like boolean[][] or ArrayList<boolean> or ArrayList<boolean[]> - haven't planned that far ahead, my question concerns the clicking.

Can I read the mouse's position within, say, a JFrame that shows the map? Can I apply Listeners to each hex? I just don't even know where to start, so I'd appreciate a shove in the right direction.

Thank you, Martin
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. A MouseListener can return you a MouseEvent via its mouseClicked method, and that event tells you the X and Y of the click location.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it give me the coordinates of the screen or of the JFrame? If it's of the entire desktop I'd have to do something like read the coordinates of the JFrame and do some math to see if the mouse is within the frame or not, but that doesn't sound too hard.

Thank you very much, M
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In whatever component you are adding the mouse listener it returns coordinates in that component, if you then want to know what are the coordinates in the entire screen you call getLocationOnScreen() from that component and then translate it with the coordinates which you have from mouse event....
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect. Again, thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!