本文介绍了Mathematica 和 MouseListener - 使用 Mma 开发交互式图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 Mathematica 3D 图形添加交互性,除了 Manipulate 之外,它很酷但有其局限性.想想 Mathematica 中四个立方体问题的演示的四个示例,单击其中一个立方体会旋转一个立方体.

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.

问题.

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

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

或者是使用 Java 然后从 Java 调用 Mathematica 是建议的路线吗?

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

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

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

推荐答案

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

EventHandler can be used to catch various mouse events (mouse up, mouse down, mouse clicked, mouse dragged). Use MousePosition to add some intelligence.

示例:

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.

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

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

这篇关于Mathematica 和 MouseListener - 使用 Mma 开发交互式图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 03:19