Mathematica和MouseListener - 用Mma开发交互式图形

nil*_*ock 8 wolfram-mathematica

我想为Mathematica 3D图形添加交互性,而不是Manipulate,它很酷但有其局限性.想想Mathematica中四个立方体问题演示的四个例子,点击其中一个立方体就会旋转一个立方体.

问题.

  1. 是否有可能在Mathematica图形中捕获MouseEvents(例如使用Java类或其他?)

  2. 或者是使用Java然后从Java调用Mathematica建议路由?

  3. 或者(我希望不是)正在开发超出Mathematica应该做的交互式图形程序?

Sjo*_*ies 15

EventHandler可用于捕获各种鼠标事件(鼠标向上,鼠标按下,鼠标单击,鼠标拖动).使用MousePosition添加一些智能.

例:

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})}]
   }
  ]
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

可以单独点击圆圈.分别为每个对象定义一个动作.

令人惊讶的是,这甚至适用于3D图形:

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})}]
   }
  ]
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @ ndroock1感谢接受我的回答.下次你可能会等待一段时间以获得更多回复. (2认同)
  • Sjoerd,我已经投票支持了这一点,但我会再次投票支持更新.我以前从未见过,可旋转等等.:-) (2认同)