1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Regarding Michael deleted comment: it's wrapped in Dynamic because the radius is meant to change when clicked in the "real" case. $\endgroup$
    – Öskå
    Commented Jan 16, 2014 at 14:36

1 Answer 1

1
$\begingroup$

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}}]
 ]
$\endgroup$
3
  • $\begingroup$ Except that in this case you extract the centers of both when clicked, not the center of the clicked one. $\endgroup$
    – Öskå
    Commented Jan 16, 2014 at 14:25
  • $\begingroup$ @Öskå Each disk buttons acts only when clicked now. You can do whatever you want with the action argument to each button. $\endgroup$
    – Michael E2
    Commented Jan 19, 2014 at 2:58
  • $\begingroup$ Sorry I didn't see the notification since you first deleted your answer. There was no notification that you undeleted it. But thank you :) $\endgroup$
    – Öskå
    Commented Jan 19, 2014 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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