Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Please excuse me if that question is too basic but I couldn't find anything in the documentation or on here.

If you consider the following Graphic I would like to know how to detect which primitive is being clicked,

Graphics[{Disk[#, 0.5]} & /@ {{0, 0}, {1, 0}}]

enter image description here

and for example change the color of the clicked Disk, change its radius or get the coordinates of its center or anything related to its geometry.

I am aware that EventHandler is my friend but as the questions states, I have no clue how to detect which primitive is being clicked (except by using Mouseposition and compare it to the centers).

I am also aware that my question is somehow poorly researched but I feel like I'm blocking on a basic notion.

share|improve this question
    
The way to do this is by using EventHandler and Dynamic color on each primitive that you want to track. I don't think you can develop a robust solution (or maybe near impossible) by using just one outer EventHandler and infer which primitive is being clicked. So, in this case, it's just the same as the example under "Scope" in the doc page you linked to. –  rm -rf Jan 6 at 19:19
2  
    
@rm-rf In this István Zachar is kinda doing it (opening a menu on the clicked node), but I can not find the way to extract the interesting pieces of code. –  Öskå Jan 6 at 20:45
    
@SjoerdC.deVries A pity we can't migrate that one –  belisarius Jan 6 at 20:56
    
@belisarius I could cheat and just copy the answer... –  Sjoerd C. de Vries Jan 6 at 21:04
show 6 more comments

1 Answer 1

up vote 3 down vote accepted

EventHandler can be used to catch various mouse events (mouse up, mouse down, mouse clicked, mouse dragged). Use MousePosition to add some intelligence.

Example:

DynamicModule[{col1 = Green, col2 = Blue}, Graphics[
  {
   EventHandler[
    Dynamic[{col1, Disk[]}, 
     ImageSize -> 
      Tiny], {"MouseClicked" :> (col1 = 
        col1 /. {Red -> Green, Green -> Red})}],
   EventHandler[
    Dynamic[{col2, Disk[{1, 1}]}, 
     ImageSize -> 
      Tiny], {"MouseClicked" :> (col2 = 
        col2 /. {Blue -> Yellow, Yellow -> Blue})}]
   }
  ]
 ]

enter image description here

The circles can be clicked independently. An action is defined for each object separately.

Amazingly, this even works for 3D Graphics:

DynamicModule[{col1 = Green, col2 = Blue}, 
 Graphics3D[
  {
   EventHandler[
    Dynamic[{col1, Sphere[]}, 
     ImageSize -> 
      Tiny], {"MouseClicked" :> (col1 = 
        col1 /. {Red -> Green, Green -> Red})}], 
   EventHandler[
    Dynamic[{col2, Sphere[{1, 1, 1}]}, 
     ImageSize -> 
      Tiny], {"MouseClicked" :> (col2 = 
        col2 /. {Blue -> Yellow, Yellow -> Blue})}]
   }
  ]
 ]

enter image description here

Here's some code to test the scalability:

With[{n = 100},
 DynamicModule[{col = RGBColor @@@ RandomReal[{0, 1}, {n, 3}]},
  Graphics[
   Table[
    With[{j = i},
     EventHandler[
      {Dynamic[col[[j]]], Disk[RandomReal[{-10, 10}, {2}]]}, 
      ImageSize -> Tiny,
      {"MouseClicked" :> (col[[j]] = 
          RGBColor @@ RandomReal[{0, 1}, {3}])}
      ]
     ], {i, n}
    ]
   ]
  ]
 ]

Mathematica graphics

This is reasonably responsive. However, try the version with n=1000:

Mathematica graphics

On my PC the color changes only after about three seconds. So, if you need to interact with many objects this may not be the best solution.

share|improve this answer
    
Would you know by any chance how to extract the radius or "physical" information? Your last update is really great & interesting. –  Öskå Jan 6 at 22:19
    
@Öskå Since all of the information to draw the object in the EventHandler is to be found there you probably can communicate this by means of the event action, for instance, by having it set a global variable or so. In the example below the action could be extended from (col[[j]] = RGBColor @@ RandomReal[{0, 1}, {3}]) to (clickedColor=col[[j]] = RGBColor @@ RandomReal[{0, 1}, {3}]) –  Sjoerd C. de Vries Jan 6 at 22:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.