Mathematica and MouseListener - developing interactive graphics with Mma

旧时模样 提交于 2019-12-09 10:18:32

问题


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 in Mathematica, a click on one of the cubes rotates a cube.

Questions.

  1. Is it possible to catch MouseEvents in Mathematica graphics ( for example with using a Java class or otherwise? )

  2. Or is the use Java then call Mathematica from Java the advised route?

  3. Or ( I hope not ) is developing interactive graphics programs beyond of what one should do with Mathematica?


回答1:


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})}]
   }
  ]
 ]

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})}]
   }
  ]
 ]



来源:https://stackoverflow.com/questions/6189570/mathematica-and-mouselistener-developing-interactive-graphics-with-mma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!