1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
package vtk.rendering.awt;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import vtk.vtkObject;
import vtk.vtkRenderWindow;
import vtk.rendering.vtkAbstractComponent;
/**
* Provide AWT based vtk rendering component
*
* @authors Sebastien Jourdain - sebastien.jourdain@kitware.com
* Joachim Pouderoux - joachim.pouderoux@kitware.com
*/
public class vtkAwtComponent extends vtkAbstractComponent<Canvas> {
protected vtkInternalAwtComponent uiComponent;
protected boolean isWindowCreated;
protected Runnable onWindowCreatedCallback;
public vtkAwtComponent() {
this(new vtkRenderWindow());
}
public vtkAwtComponent(vtkRenderWindow renderWindowToUse) {
super(renderWindowToUse);
this.isWindowCreated = false;
this.uiComponent = new vtkInternalAwtComponent(this);
this.uiComponent.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent arg0) {
Dimension size = vtkAwtComponent.this.uiComponent.getSize();
vtkAwtComponent.this.setSize(size.width, size.height);
}
});
}
public void Render() {
// Make sure we can render
if (inRenderCall || renderer == null || renderWindow == null) {
return;
}
// Try to render
try {
lock.lockInterruptibly();
inRenderCall = true;
// Initialize the window only once
if (!isWindowCreated) {
uiComponent.RenderCreate(renderWindow);
setSize(uiComponent.getWidth(), uiComponent.getHeight());
isWindowCreated = true;
}
// Trigger the real render
renderWindow.Render();
// Execute callback if need be
if(this.onWindowCreatedCallback != null) {
this.onWindowCreatedCallback.run();
this.onWindowCreatedCallback = null;
}
} catch (InterruptedException e) {
// Nothing that we can do except skipping execution
} finally {
lock.unlock();
inRenderCall = false;
}
}
public Canvas getComponent() {
return this.uiComponent;
}
public void Delete() {
this.lock.lock();
// We prevent any further rendering
inRenderCall = true;
if (this.uiComponent.getParent() != null) {
this.uiComponent.getParent().remove(this.uiComponent);
}
super.Delete();
// On linux we prefer to have a memory leak instead of a crash
if (!this.renderWindow.GetClassName().equals("vtkXOpenGLRenderWindow")) {
this.renderWindow = null;
} else {
System.out.println("The renderwindow has been kept arount to prevent a crash");
}
this.lock.unlock();
vtkObject.JAVA_OBJECT_MANAGER.gc(false);
}
/**
* @return true if the graphical component has been properly set and
* operation can be performed on it.
*/
public boolean isWindowSet() {
return this.isWindowCreated;
}
/**
* Set a callback that get's called once the window is properly created and can be
* customized in its settings.
*
* Once called the callback will be released.
*
* @param callback
*/
public void setWindowReadyCallback(Runnable callback) {
this.onWindowCreatedCallback = callback;
}
/**
* Just allow class in same package to affect inRenderCall boolean
*
* @param value
*/
protected void updateInRenderCall(boolean value) {
this.inRenderCall = value;
}
}
|