MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Dynamic Control of Graphics

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105124] Re: Dynamic Control of Graphics
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sat, 21 Nov 2009 03:36:59 -0500 (EST)
  • References: <he0oae$4ov$1@smc.vnet.net>

PeterSF schrieb:
> I am new to the dynamic features of the Mathematica fron end.  Having spent a
> considerable amount of time, I am still strufggling with the following
> problem: how to control dynamically individual elements of a combined graphics?
> Specifically, we are given
> Show[ Plot[ Sin[x], {x,0,Pi} ], Graphics[ { textColor, Text
> ["Test"] } ],
>                                                 Graphics[ { lineWidth,
> Line[{ {0,0}, {0,2}] } ] ]
> The objective is to change:
>     * the color of text (the 2nd element) if and only if a mouse is
>       over that text;
>     * the thickness of the line (the 3rd element) if and only if a
> mouse is
>       over that line.
> I can see how to handle these simultaneously:
> 
> DynamicModule[ { lineWidth = Thin, textColor = Blue},
>    Dynamic @ EventHandler[ Show[ Plot[Sin[x], {x, 0, \[Pi]} ],
>        Graphics[{ textColor, Text["XXX", {1, 1}]}],
>        Graphics[{ lineWidth, Line[{{0, 0}, {2, 1}}]}]],
>   "MouseClicked" :> (textColor  = Red, lineWidth )]]
> 
> The question is, how can one control the wdth and the color
> individually?
> 
> More broadly, I need to perform the following tasks: upon mouseover to
> change the appearance of a single graphics element (line, text, etc.);
> then, upon mouse click, to store in a variable which elements was
> selected (clicked over); and, upon pressing of the Delete key, to
> remove that element from the combined graphics.
> 
> I would be most greatful for any and all of the help you could give
> me.

I think the following is doing what you described, except that I am
using the "d" key instead of the delete key. Unfortunatly there seems to
be no documented way to get ahold of the Delete key pressed events, or
am I missing something? I am using a combination of Mouseover, Dynamic
and Eventhandler:

DynamicModule[{
  selected = None, highlighted = None,
  lineWidth = Thin, textColor = Blue,
  curve, text, line
  },
 curve = Plot[Sin[x], {x, 0, \[Pi]}] /. Line[points_] :> Mouseover[
     Dynamic[highlighted = None; Line[points]],
     Dynamic[highlighted = Hold[curve]; Line[points]]
     ];
 text = Graphics[Mouseover[
    Dynamic[{highlighted = None; Blue, Text["XXX", {1, 1}]}],
    Dynamic[{highlighted = Hold[text]; Red, Text["XXX", {1, 1}]}]
    ]];
 line = Graphics[Mouseover[
    Dynamic[{highlighted = None; Thin, Line[{{0, 0}, {2, 1}}]}],
    Dynamic[{highlighted = Hold[line]; Thick, Line[{{0, 0}, {2, 1}}]}]
    ]];
 EventHandler[
  Column[{
    Dynamic[Show[curve, text, line]],
    Dynamic[highlighted],
    Dynamic[selected]
    }],
  {
   "MouseClicked" :> (selected = highlighted),
   {"KeyDown", "d"} :> (
     Print["deleting: " <> ToString[selected]];
     Set @@ Append[selected, Graphics[]]
     )
   },
  PassEventsDown -> True
  ]]

hth,

albert


  • Prev by Date: Re: Permanent Computation Efficiency
  • Next by Date: Re: Re: I broke the sum into pieces
  • Previous by thread: Re: Dynamic Control of Graphics
  • Next by thread: aliases and hot keys