3 import java.awt.Canvas;
4 import java.awt.Graphics;
5 import java.awt.event.InputEvent;
6 import java.awt.event.KeyEvent;
7 import java.awt.event.KeyListener;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.awt.event.MouseMotionListener;
12 import javax.swing.SwingUtilities;
15 * Java AWT component that encapsulate vtkRenderWindow, vtkRenderer, vtkCamera,
18 * If a vtkInteractor is needed, use vtkCanvas instead. This is necessary when
19 * Widget and Picker are used.
23 public class vtkPanel extends Canvas implements MouseListener, MouseMotionListener, KeyListener {
24 private static final long serialVersionUID = 1L;
25 protected vtkRenderWindow rw = new vtkRenderWindow();
26 protected vtkRenderer ren = new vtkRenderer();
27 protected vtkCamera cam = null;
28 protected vtkLight lgt = new vtkLight();
31 protected int windowset = 0;
32 protected int lightingset = 0;
33 protected int LightFollowCamera = 1;
34 protected int InteractionMode = 1;
35 protected boolean rendering = false;
38 System.loadLibrary("vtksys");
39 System.loadLibrary("vtkCommon");
40 System.loadLibrary("vtkCommonJava");
41 System.loadLibrary("vtkFiltering");
42 System.loadLibrary("vtkexpat");
43 System.loadLibrary("vtkjpeg");
44 System.loadLibrary("vtkzlib");
45 System.loadLibrary("vtktiff");
46 System.loadLibrary("vtkpng");
47 System.loadLibrary("vtkmetaio");
48 System.loadLibrary("vtkhdf5");
49 System.loadLibrary("vtkhdf5_hl");
50 System.loadLibrary("vtkNetCDF");
51 System.loadLibrary("vtkNetCDF_cxx");
52 System.loadLibrary("vtkDICOMParser");
53 System.loadLibrary("vtkFilteringJava");
54 System.loadLibrary("LSDyna");
55 System.loadLibrary("vtkIO");
56 System.loadLibrary("vtkIOJava");
57 System.loadLibrary("vtkImaging");
58 // System.loadLibrary("jawt");
59 System.loadLibrary("vtkImagingJava");
60 System.loadLibrary("vtkverdict");
61 System.loadLibrary("vtkGraphics");
62 System.loadLibrary("vtkfreetype");
63 System.loadLibrary("vtkftgl");
64 System.loadLibrary("vtkGraphicsJava");
65 System.loadLibrary("vtkRendering");
66 System.loadLibrary("vtkRenderingJava");
67 vtkNativeLibrary.COMMON.LoadLibrary();
68 vtkNativeLibrary.FILTERING.LoadLibrary();
69 vtkNativeLibrary.IO.LoadLibrary();
70 vtkNativeLibrary.IMAGING.LoadLibrary();
71 vtkNativeLibrary.GRAPHICS.LoadLibrary();
72 vtkNativeLibrary.RENDERING.LoadLibrary();
74 System.loadLibrary("vtkexoIIc");
75 System.loadLibrary("vtkHybrid");
76 System.loadLibrary("vtkHybridJava");
77 } catch (UnsatisfiedLinkError e) {
78 System.out.println("cannot load vtkHybrid, skipping...");
81 System.loadLibrary("vtkVolumeRendering");
82 System.loadLibrary("vtkVolumeRenderingJava");
83 vtkNativeLibrary.VOLUME_RENDERING.LoadLibrary();
84 } catch (Throwable e) {
85 System.out.println("cannot load vtkVolumeRendering, skipping...");
87 System.loadLibrary("vtkWidgets");
88 System.loadLibrary("vtkWidgetsJava");
91 // Allow access to display lock() and unlock().
92 // Call these whenever you call something that causes
96 // // Code that causes a render
102 public void unlock() {
106 public void Delete() {
111 // We prevent any further rendering
113 if (this.getParent() != null) {
114 this.getParent().remove(this);
116 // Free internal VTK objects
120 // On linux we prefer to have a memory leak instead of a crash
121 if(!rw.GetClassName().equals("vtkXOpenGLRenderWindow")) {
124 System.out.println("The renderwindow has been kept arount to prevent a crash");
128 protected native int RenderCreate(vtkRenderWindow id0);
130 protected native int Lock();
132 protected native int UnLock();
136 addMouseListener(this);
137 addMouseMotionListener(this);
138 addKeyListener(this);
139 super.setSize(200, 200);
140 rw.SetSize(200, 200);
143 public vtkPanel(vtkRenderWindow renwin) {
146 addMouseListener(this);
147 addMouseMotionListener(this);
148 addKeyListener(this);
149 super.setSize(200, 200);
150 rw.SetSize(200, 200);
153 public void Report() {
155 // must be performed on awt event thread
156 Runnable updateAComponent = new Runnable() {
159 System.out.println("direct rendering = " + (rw.IsDirect() == 1));
160 System.out.println("opengl supported = " + (rw.SupportsOpenGL() == 1));
161 System.out.println("report = " + rw.ReportCapabilities());
166 SwingUtilities.invokeLater(updateAComponent);
170 public vtkRenderer GetRenderer() {
174 public vtkRenderWindow GetRenderWindow() {
178 public void setSize(int x, int y) {
180 if (windowset == 1) {
187 public void addNotify() {
190 rw.SetForceMakeCurrent();
194 public void removeNotify() {
196 super.removeNotify();
199 public synchronized void Render() {
202 if (ren.VisibleActorCount() == 0) {
207 if (windowset == 0) {
208 // set the window id and the active camera
209 cam = ren.GetActiveCamera();
210 if (lightingset == 0) {
212 lgt.SetPosition(cam.GetPosition());
213 lgt.SetFocalPoint(cam.GetFocalPoint());
218 rw.SetSize(getWidth(), getHeight());
221 this.setSize(getWidth(), getHeight());
231 public boolean isWindowSet() {
232 return (this.windowset == 1);
235 public void paint(Graphics g) {
239 public void update(Graphics g) {
243 public void LightFollowCameraOn() {
244 this.LightFollowCamera = 1;
247 public void LightFollowCameraOff() {
248 this.LightFollowCamera = 0;
251 public void InteractionModeRotate() {
252 this.InteractionMode = 1;
255 public void InteractionModeTranslate() {
256 this.InteractionMode = 2;
259 public void InteractionModeZoom() {
260 this.InteractionMode = 3;
263 public void UpdateLight() {
264 lgt.SetPosition(cam.GetPosition());
265 lgt.SetFocalPoint(cam.GetFocalPoint());
268 public void resetCameraClippingRange() {
270 ren.ResetCameraClippingRange();
274 public void resetCamera() {
280 public void mouseClicked(MouseEvent e) {
284 public void mousePressed(MouseEvent e) {
286 if (ren.VisibleActorCount() == 0)
288 rw.SetDesiredUpdateRate(5.0);
291 if ((e.getModifiers() == InputEvent.BUTTON2_MASK) || (e.getModifiers() == (InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK))) {
292 InteractionModeTranslate();
293 } else if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
294 InteractionModeZoom();
296 InteractionModeRotate();
300 public void mouseReleased(MouseEvent e) {
301 rw.SetDesiredUpdateRate(0.01);
304 public void mouseEntered(MouseEvent e) {
308 public void mouseExited(MouseEvent e) {
311 public void mouseMoved(MouseEvent e) {
316 public void mouseDragged(MouseEvent e) {
317 if (ren.VisibleActorCount() == 0)
322 if (this.InteractionMode == 1) {
323 cam.Azimuth(lastX - x);
324 cam.Elevation(y - lastY);
325 cam.OrthogonalizeViewUp();
326 resetCameraClippingRange();
327 if (this.LightFollowCamera == 1) {
328 lgt.SetPosition(cam.GetPosition());
329 lgt.SetFocalPoint(cam.GetFocalPoint());
333 if (this.InteractionMode == 2) {
336 double APoint[] = new double[3];
340 // get the current focal point and position
341 FPoint = cam.GetFocalPoint();
342 PPoint = cam.GetPosition();
344 // calculate the focal depth since we'll be using it a lot
345 ren.SetWorldPoint(FPoint[0], FPoint[1], FPoint[2], 1.0);
346 ren.WorldToDisplay();
347 focalDepth = ren.GetDisplayPoint()[2];
349 APoint[0] = rw.GetSize()[0] / 2.0 + (x - lastX);
350 APoint[1] = rw.GetSize()[1] / 2.0 - (y - lastY);
351 APoint[2] = focalDepth;
352 ren.SetDisplayPoint(APoint);
353 ren.DisplayToWorld();
354 RPoint = ren.GetWorldPoint();
355 if (RPoint[3] != 0.0) {
356 RPoint[0] = RPoint[0] / RPoint[3];
357 RPoint[1] = RPoint[1] / RPoint[3];
358 RPoint[2] = RPoint[2] / RPoint[3];
362 * Compute a translation vector, moving everything 1/2 the distance
363 * to the cursor. (Arbitrary scale factor)
365 cam.SetFocalPoint((FPoint[0] - RPoint[0]) / 2.0 + FPoint[0], (FPoint[1] - RPoint[1]) / 2.0 + FPoint[1], (FPoint[2] - RPoint[2]) / 2.0 + FPoint[2]);
366 cam.SetPosition((FPoint[0] - RPoint[0]) / 2.0 + PPoint[0], (FPoint[1] - RPoint[1]) / 2.0 + PPoint[1], (FPoint[2] - RPoint[2]) / 2.0 + PPoint[2]);
367 resetCameraClippingRange();
370 if (this.InteractionMode == 3) {
372 zoomFactor = Math.pow(1.02, (y - lastY));
373 if (cam.GetParallelProjection() == 1) {
374 cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
376 cam.Dolly(zoomFactor);
377 resetCameraClippingRange();
385 public void keyTyped(KeyEvent e) {
388 public void keyPressed(KeyEvent e) {
389 if (ren.VisibleActorCount() == 0)
391 char keyChar = e.getKeyChar();
393 if ('r' == keyChar) {
397 if ('u' == keyChar) {
398 pickActor(lastX, lastY);
400 if ('w' == keyChar) {
401 vtkActorCollection ac;
405 ac = ren.GetActors();
407 for (i = 0; i < ac.GetNumberOfItems(); i++) {
408 anActor = ac.GetNextActor();
409 anActor.GetProperty().SetRepresentationToWireframe();
413 if ('s' == keyChar) {
414 vtkActorCollection ac;
418 ac = ren.GetActors();
420 for (i = 0; i < ac.GetNumberOfItems(); i++) {
421 anActor = ac.GetNextActor();
422 anActor.GetProperty().SetRepresentationToSurface();
428 public void HardCopy(String filename, int mag) {
432 vtkWindowToImageFilter w2if = new vtkWindowToImageFilter();
435 w2if.SetMagnification(mag);
438 vtkTIFFWriter writer = new vtkTIFFWriter();
439 writer.SetInput(w2if.GetOutput());
440 writer.SetFileName(filename);
446 public void pickActor(int x, int y) {
448 vtkPropPicker picker = new vtkPropPicker();
451 picker.PickProp(x, rw.GetSize()[1] - y, ren);
454 if (picker.GetActor() != null)
455 System.out.println(picker.GetActor().GetClassName());
458 public void keyReleased(KeyEvent e) {