After rewriting the VTK example 'MouseEvents' (https://examples.vtk.org/site/Cxx/Interaction/MouseEvents/) in Java,
import vtk.vtkActor;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkNativeLibrary;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkSphereSource;
import vtk.vtkPolyDataMapper;
public class MouseEvents {
static {
vtkNativeLibrary.LoadAllNativeLibraries();
vtkNativeLibrary.DisableOutputWindow(null);
}
public static void main(String[] args) {
new MouseEvents();
}
public MouseEvents() {
vtkSphereSource sphereSource = new vtkSphereSource();
sphereSource.SetCenter(0.0, 0.0, 0.0);
sphereSource.SetRadius(5.0);
sphereSource.Update();
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInputConnection(sphereSource.GetOutputPort());
vtkActor actor = new vtkActor();
actor.SetMapper(mapper);
vtkRenderer renderer = new vtkRenderer();
renderer.AddActor(actor);
vtkRenderWindow renderWindow = new vtkRenderWindow();
renderWindow.AddRenderer(renderer);
renderWindow.SetWindowName("MouseEvents");
vtkRenderWindowInteractor renderWindowInteractor = new vtkRenderWindowInteractor();
renderWindowInteractor.SetRenderWindow(renderWindow);
customMouseInteractorStyle style = new customMouseInteractorStyle();
renderWindowInteractor.SetInteractorStyle(style);
renderWindow.Render();
renderWindowInteractor.Initialize();
renderWindowInteractor.Start();
}
static class customMouseInteractorStyle extends vtkInteractorStyleTrackballCamera {
public customMouseInteractorStyle() {
super();
}
public void OnLeftButtonDown() {
System.out.println("Pressed left mouse button.");
super.OnLeftButtonDown();
}
public void OnMiddleButtonDown() {
System.out.println("Pressed middle mouse button.");
super.OnMiddleButtonDown();
}
public void OnRightButtonDown() {
System.out.println("Pressed right mouse button.");
super.OnRightButtonDown();
}
}
}
I am not getting any output when the mouse buttons are clicked. I am successfully able to manipulate the sphere with all 3 mouse buttons, but nothing is printed out. It is a true translation of the C++ code. The python example, however, adds an Observer.
The following 3 lines were needed to produce the desired output:
renderWindowInteractor.AddObserver("LeftButtonPressEvent", style, "OnLeftButtonDown");
renderWindowInteractor.AddObserver("MiddleButtonPressEvent", style, "OnMiddleButtonDown");
renderWindowInteractor.AddObserver("RightButtonPressEvent", style, "OnRightButtonDown");