2011-05-31 38 views
8

我想添加與Mathematica 3D圖形的交互性,除了Manipulate這很酷但有其侷限性。想想Mathematica中四個立方體問題演示的四個示例,點擊其中一個立方體旋轉立方體。Mathematica和MouseListener - 使用Mma開發交互式圖形

問題。

  1. 是否有可能趕上數學圖形MouseEvents(例如使用Java類或以其他方式?)

  2. 或者是使用Java然後從Java調用數學的建議路線?或者(我不希望)正在開發交互式圖形程序,而不是Mathematica應該怎麼做?

回答

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

enter image description here

圓圈可以獨立地點擊。分別爲每個對象定義一個動作。

令人驚訝的是,這甚至適用於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})}] 
    } 
    ] 
] 

enter image description here

+1

驚人。謝謝。 – 2011-05-31 15:32:06

+2

@ ndroock1感謝您接受我的回答。下一次您可能再等一會以獲得更多回應。 – 2011-05-31 15:49:47

+2

Sjoerd,我已經爲此投了票,但我會再次投票更新。我從來沒有見過,可旋轉和所有。 :-) – 2011-05-31 22:51:28