Commit 3d998a97 authored by Sebastien Jourdain's avatar Sebastien Jourdain Committed by Code Review
Browse files

Merge topic 'fix-java-jogl-widget' into master

4cb28678 Fix JOGL interactor widget issue
parents dc4aea4e 4cb28678
......@@ -65,11 +65,6 @@ public class vtkAbstractJoglComponent<T extends java.awt.Component> extends vtkA
vtkObject.JAVA_OBJECT_MANAGER.gc(false);
}
};
try {
this.uiComponent.getClass().getMethod("addGLEventListener", GLEventListener.class).invoke(this.uiComponent, this.glEventListener);
} catch (Exception e) {
throw new RuntimeException("Impossible to call addGLEventListener(listener) in a reflexive way.");
}
// Bind the interactor forwarder
vtkInteractorForwarder forwarder = this.getInteractorForwarder();
......@@ -80,6 +75,8 @@ public class vtkAbstractJoglComponent<T extends java.awt.Component> extends vtkA
// Make sure when VTK internaly request a Render, the Render get
// properly triggered
renderWindowToUse.AddObserver("WindowFrameEvent", this, "Render");
renderWindowToUse.GetInteractor().AddObserver("RenderEvent", this, "Render");
renderWindowToUse.GetInteractor().SetEnableRender(false);
}
public T getComponent() {
......
......@@ -19,5 +19,6 @@ public class vtkJoglCanvasComponent extends vtkAbstractJoglComponent<GLCanvas> {
public vtkJoglCanvasComponent(vtkRenderWindow renderWindow, GLCapabilities capabilities) {
super(renderWindow, new GLCanvas(capabilities));
this.getComponent().addGLEventListener(this.glEventListener);
}
}
......@@ -19,5 +19,6 @@ public class vtkJoglPanelComponent extends vtkAbstractJoglComponent<GLJPanel> {
public vtkJoglPanelComponent(vtkRenderWindow renderWindow, GLCapabilities capabilities) {
super(renderWindow, new GLJPanel(capabilities));
this.getComponent().addGLEventListener(this.glEventListener);
}
}
......@@ -2,10 +2,12 @@ package vtk.rendering;
import java.util.concurrent.locks.ReentrantLock;
import vtk.vtkAxesActor;
import vtk.vtkCamera;
import vtk.vtkGenericRenderWindowInteractor;
import vtk.vtkInteractorStyle;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkOrientationMarkerWidget;
import vtk.vtkRenderWindow;
import vtk.vtkRenderer;
......@@ -156,4 +158,24 @@ public abstract class vtkAbstractComponent<T> implements vtkComponent<T> {
}
public abstract T getComponent();
/**
* Generic helper method used to attach orientation axes to a vtkComponent
*
* @param vtkComponent<?>
*/
public static void attachOrientationAxes(vtkComponent<?> component) {
// only build this once, because it creates its own renderer.
// Extra renderers causes issues with resetting.
vtkAxesActor axes = new vtkAxesActor();
vtkOrientationMarkerWidget axesWidget = new vtkOrientationMarkerWidget();
axesWidget.SetOutlineColor(0.9300, 0.5700, 0.1300);
axesWidget.SetOrientationMarker(axes);
axesWidget.SetInteractor(component.getRenderWindowInteractor());
axesWidget.SetDefaultRenderer(component.getRenderer());
axesWidget.SetViewport(0.0, 0.0, .2, .2);
axesWidget.EnabledOn();
axesWidget.InteractiveOff();
}
}
......@@ -8,9 +8,15 @@ import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import vtk.vtkActor;
import vtk.vtkBoxRepresentation;
import vtk.vtkBoxWidget2;
import vtk.vtkConeSource;
import vtk.vtkLookupTable;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.vtkScalarBarRepresentation;
import vtk.vtkScalarBarWidget;
import vtk.vtkTransform;
import vtk.rendering.jogl.vtkAbstractJoglComponent;
import vtk.rendering.jogl.vtkJoglCanvasComponent;
import vtk.rendering.jogl.vtkJoglPanelComponent;
......@@ -38,11 +44,12 @@ public class JoglConeRendering {
// build VTK Pipeline
vtkConeSource cone = new vtkConeSource();
cone.SetResolution(8);
cone.Update();
vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
coneMapper.SetInputConnection(cone.GetOutputPort());
vtkActor coneActor = new vtkActor();
final vtkActor coneActor = new vtkActor();
coneActor.SetMapper(coneMapper);
// VTK rendering part
......@@ -51,6 +58,54 @@ public class JoglConeRendering {
joglWidget.getRenderer().AddActor(coneActor);
// Add orientation axes
vtkAbstractJoglComponent.attachOrientationAxes(joglWidget);
// Add Scalar bar widget
vtkLookupTable lut = new vtkLookupTable();
lut.SetHueRange(.66, 0);
lut.Build();
vtkScalarBarWidget scalarBar = new vtkScalarBarWidget();
scalarBar.SetInteractor(joglWidget.getRenderWindowInteractor());
scalarBar.GetScalarBarActor().SetTitle("Example");
scalarBar.GetScalarBarActor().SetLookupTable(lut);
scalarBar.GetScalarBarActor().SetOrientationToHorizontal();
scalarBar.GetScalarBarActor().SetTextPositionToPrecedeScalarBar();
vtkScalarBarRepresentation srep = (vtkScalarBarRepresentation) scalarBar.GetRepresentation();
srep.SetPosition(0.5, 0.053796);
srep.SetPosition2(0.33, 0.106455);
//scalarBar.ProcessEventsOff();
scalarBar.EnabledOn();
scalarBar.RepositionableOn();
// Add interactive 3D Widget
final vtkBoxRepresentation representation = new vtkBoxRepresentation();
representation.SetPlaceFactor(1.25);
representation.PlaceWidget(cone.GetOutput().GetBounds());
final vtkBoxWidget2 boxWidget = new vtkBoxWidget2();
boxWidget.SetRepresentation(representation);
boxWidget.SetInteractor(joglWidget.getRenderWindowInteractor());
boxWidget.SetPriority(1);
final Runnable callback = new Runnable() {
vtkTransform trasform = new vtkTransform();
public void run() {
vtkBoxRepresentation rep = (vtkBoxRepresentation) boxWidget.GetRepresentation();
rep.GetTransform(trasform);
coneActor.SetUserTransform(trasform);
}
};
// Bind widget
boxWidget.AddObserver("InteractionEvent", callback, "run");
representation.VisibilityOn();
representation.HandlesOn();
boxWidget.SetEnabled(1);
boxWidget.SetMoveFacesEnabled(1);
// UI part
JFrame frame = new JFrame("SimpleVTK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
......@@ -60,27 +115,28 @@ public class JoglConeRendering {
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
joglWidget.resetCamera();
joglWidget.getComponent().requestFocus();
// Add r:ResetCamera and q:Quit key binding
joglWidget.getComponent().addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar() == 'r') {
joglWidget.resetCamera();
} else if (e.getKeyChar() == 'q') {
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
});
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'r') {
joglWidget.resetCamera();
} else if (e.getKeyChar() == 'q') {
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
});
}
});
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment