Mathematica and MouseListener - developing interactive graphics with Mma

前端 未结 1 2078
迷失自我
迷失自我 2021-02-09 02:13

I want to add interactivity to Mathematica 3D graphics, other than with Manipulate which is cool but has its limitations. Think four example of a demo of the four cubes problem

相关标签:
1条回答
  • 2021-02-09 02:41

    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

    0 讨论(0)
提交回复
热议问题