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.

One can find primitives' properties such as the radius, the center or the color of a Disk using Cases by using:

g = Graphics[{Red, Disk[{0, 0}, 1], Black, Disk[{10, 0}, 5], Blue, Disk[{30, 0}, 3]}]
Cases[g, Disk[_, y_] :> y, Infinity]
Cases[g, Disk[x_, _] :> x, Infinity]
Cases[g, _RGBColor | _Hue | _GrayLevel, Infinity]

yielding the right values.

How can the same functions be applied to an EventHandler?

Consider for instance the following code:

DynamicModule[{list = {{0, 0}, {2, 0}}},
 Graphics[
  EventHandler[
   {Red, Dynamic@Disk[#, 1] & /@ list},
  {"MouseClicked" :> {(* the appropriate function *)}}]]]

The aim would be the extract the radius/center/color of the clicked primitive.

share|improve this question
    
Regarding Michael deleted comment: it's wrapped in Dynamic because the radius is meant to change when clicked in the "real" case. –  Öskå Jan 16 at 14:36

1 Answer 1

up vote 1 down vote accepted

Here's a way, using the built-in event handler Button:

DynamicModule[{list = {{0, 0}, {2, 0}}, res}, 
 Dynamic@{res, 
   Graphics[{Red, Button[Dynamic@Disk[#, 1], res = #] & /@ list}]}
 ]

An illustration of the use-case in the OP's comment to the question:

DynamicModule[{list = {{0, 0}, {2, 0}}, r},
 (r[#] = 1) & /@ list;
 Graphics[
   {Red, 
    Button[Dynamic@Disk[#, r[#]],
           r[#] = 1.5 + #[[1]]/2 - r[#]] &   /@ list},
   PlotRange -> {{-1, 3.5}, {-2, 2}}]
 ]
share|improve this answer
    
Except that in this case you extract the centers of both when clicked, not the center of the clicked one. –  Öskå Jan 16 at 14:25
    
@Öskå Each disk buttons acts only when clicked now. You can do whatever you want with the action argument to each button. –  Michael E2 Jan 19 at 2:58
    
Sorry I didn't see the notification since you first deleted your answer. There was no notification that you undeleted it. But thank you :) –  Öskå Jan 19 at 17:53

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.