1 package vtk.rendering.awt;
3 import java.awt.Canvas;
4 import java.awt.Dimension;
5 import java.awt.event.ComponentAdapter;
6 import java.awt.event.ComponentEvent;
9 import vtk.vtkRenderWindow;
10 import vtk.rendering.vtkAbstractComponent;
13 * Provide AWT based vtk rendering component
15 * @authors Sebastien Jourdain - sebastien.jourdain@kitware.com
16 * Joachim Pouderoux - joachim.pouderoux@kitware.com
18 public class vtkAwtComponent extends vtkAbstractComponent<Canvas> {
19 protected vtkInternalAwtComponent uiComponent;
20 protected boolean isWindowCreated;
21 protected Runnable onWindowCreatedCallback;
23 public vtkAwtComponent() {
24 this(new vtkRenderWindow());
27 public vtkAwtComponent(vtkRenderWindow renderWindowToUse) {
28 super(renderWindowToUse);
29 this.isWindowCreated = false;
30 this.uiComponent = new vtkInternalAwtComponent(this);
31 this.uiComponent.addComponentListener(new ComponentAdapter() {
33 public void componentResized(ComponentEvent arg0) {
34 Dimension size = vtkAwtComponent.this.uiComponent.getSize();
35 vtkAwtComponent.this.setSize(size.width, size.height);
40 public void Render() {
41 // Make sure we can render
42 if (inRenderCall || renderer == null || renderWindow == null) {
48 lock.lockInterruptibly();
51 // Initialize the window only once
52 if (!isWindowCreated) {
53 uiComponent.RenderCreate(renderWindow);
54 setSize(uiComponent.getWidth(), uiComponent.getHeight());
55 isWindowCreated = true;
58 // Trigger the real render
59 renderWindow.Render();
61 // Execute callback if need be
62 if(this.onWindowCreatedCallback != null) {
63 this.onWindowCreatedCallback.run();
64 this.onWindowCreatedCallback = null;
66 } catch (InterruptedException e) {
67 // Nothing that we can do except skipping execution
74 public Canvas getComponent() {
75 return this.uiComponent;
78 public void Delete() {
81 // We prevent any further rendering
84 if (this.uiComponent.getParent() != null) {
85 this.uiComponent.getParent().remove(this.uiComponent);
89 // On linux we prefer to have a memory leak instead of a crash
90 if (!this.renderWindow.GetClassName().equals("vtkXOpenGLRenderWindow")) {
91 this.renderWindow = null;
93 System.out.println("The renderwindow has been kept around to prevent a crash");
96 vtkObject.JAVA_OBJECT_MANAGER.gc(false);
100 * @return true if the graphical component has been properly set and
101 * operation can be performed on it.
103 public boolean isWindowSet() {
104 return this.isWindowCreated;
108 * Set a callback that gets called once the window is properly created and can be
109 * customized in its settings.
111 * Once called the callback will be released.
115 public void setWindowReadyCallback(Runnable callback) {
116 this.onWindowCreatedCallback = callback;
120 * Just allow class in same package to affect inRenderCall boolean
124 protected void updateInRenderCall(boolean value) {
125 this.inRenderCall = value;