Message Boards Message Boards

GROUPS:

How do I access coordinates on an image AFTER left-clicking the mouse?

Posted 9 years ago
7174 Views
|
4 Replies
|
1 Total Likes
|

I have an image for which I'd like to click on some location and have the point returned (so that I could process the point such as determining the associated county).

enter image description here

I see how to show the position with

Dynamic[MousePosition["Graphics"]]

(as shown in the bottom of the above figure) but I don't see how to determine the location when the mouse is left-clicked. Just a hint (well, a strong hint) will do. (Also, in the attached example, I haven't been able to remove the axes. A strong hint about that would also be appreciated.) The associated notebook is attached.

Attachments:
POSTED BY: Jim Baldwin
4 Replies
Posted 9 years ago

Thank you very much to both of you. Those pieces of code are much more than hints and are greatly appreciated.

The FrameTicks -> None option is definitely what I needed. I just wanted a box around the graphic. (For some reason I thought the Frame->True and Axes->False should do that but clearly it didn't.)

My real application does involve Manipulate in a CDF which is why I surrounded things with that command. I'm not at liberty to discuss the topic of the application but I can say that I have about 1,000 hexagons in a hexagon grid in which each hexagon is associated with different variables for which an analysis summary would be displayed. I wanted something to allow the user to select a point on the graphic in which I could then determine which hexagon was selected as I can determine the coordinates on the graphic for each hexagon and determine the hexagon with the closest hexagon center. (The hexagon grid on the graphic is likely not regular/uniform enough to allow me to apply the formula for hexagon grids to identify hexagon cells that I've forgotten so long ago.)

But first I needed to know how to capture the coordinates at a mouse click and the two of you have supplied ways to do that. Thanks!

POSTED BY: Jim Baldwin

I forgot about ClickPane... so, there, you have two different approaches!

POSTED BY: David Reiss

I am not 100% sure I understand what you are wanting, but here is a stab at an answer anyway ;-)

First, your graphic doe not have axes, you have removed them, but you do have a frame. perhaps you are wanting the frame ticks to be removed and be left with only a plain rectangle. You can do this by adding the FrameTicks -> None option.

However, the following example I am doing things slightly differently. The Manipulate in your example is not at all necessary since nothing is being manipulated. So here is a bit of code that does what I think you want: that the coordinates do not appear unless you click on a point in graphic and that when you move your mouse without clicking the coordinates do not change.

DynamicModule[{position = None},
 Column[
  {
   EventHandler[
    Framed@ca, {"MouseClicked" :> (position = MousePosition["Graphics"])}],
   Dynamic[position]
   }]
 ]

Is this the sort of thing you are looking for?

POSTED BY: David Reiss

Hi,

To remove the axis add Frame -> False option. Here is how you can determine the location and use it:

DynamicModule[
    {pt={0,0}},
    ClickPane[
       Dynamic[
         Set[POINT,pt];
         Graphics[{White,Rectangle[{-1,1}],Yellow,Disk[],Black,Point[pt],Text[POINT,{1,1}]}]
       ],
       (pt=#)&
    ]
]

Here the most resent clicked position is saved into variable POINT.

POINT    
(* {0.301814, -0.12529}*)

Note, that it is possible to use your function : point -> contry instead of Set[].

I.M.

POSTED BY: Ivan Morozov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract