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;
11 import java.awt.event.MouseWheelEvent;
12 import java.awt.event.MouseWheelListener;
14 import javax.swing.SwingUtilities;
17 * Java AWT component that encapsulate vtkRenderWindow, vtkRenderer, vtkCamera,
20 * If a vtkInteractor is needed, use vtkCanvas instead. This is necessary when
21 * Widget and Picker are used.
25 public class vtkPanel extends Canvas implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {
26 private static final long serialVersionUID = 1L;
27 protected vtkRenderWindow rw = new vtkRenderWindow();
28 protected vtkRenderer ren = new vtkRenderer();
29 protected vtkCamera cam = null;
30 protected vtkLight lgt = new vtkLight();
33 protected int windowset = 0;
34 protected int lightingset = 0;
35 protected int LightFollowCamera = 1;
36 protected int InteractionMode = 1;
37 protected boolean rendering = false;
43 // Allow access to display lock() and unlock().
44 // Call these whenever you call something that causes
48 // // Code that causes a render
54 public void unlock() {
58 public void Delete() {
63 // We prevent any further rendering
65 if (this.getParent() != null) {
66 this.getParent().remove(this);
68 // Free internal VTK objects
72 // On linux we prefer to have a memory leak instead of a crash
73 if(!rw.GetClassName().equals("vtkXOpenGLRenderWindow")) {
76 System.out.println("The renderwindow has been kept around to prevent a crash");
80 protected native int RenderCreate(vtkRenderWindow id0);
82 protected native int Lock();
84 protected native int UnLock();
89 addMouseListener(this);
90 addMouseMotionListener(this);
91 addMouseWheelListener(this);
93 super.setSize(200, 200);
97 public vtkPanel(vtkRenderWindow renwin) {
100 addMouseListener(this);
101 addMouseMotionListener(this);
102 addKeyListener(this);
103 super.setSize(200, 200);
104 rw.SetSize(200, 200);
107 public void Report() {
109 // must be performed on awt event thread
110 Runnable updateAComponent = new Runnable() {
113 System.out.println("direct rendering = " + (rw.IsDirect() == 1));
114 System.out.println("opengl supported = " + (rw.SupportsOpenGL() == 1));
115 System.out.println("report = " + rw.ReportCapabilities());
120 SwingUtilities.invokeLater(updateAComponent);
124 public vtkRenderer GetRenderer() {
128 public vtkRenderWindow GetRenderWindow() {
132 public void setSize(int x, int y) {
134 if (windowset == 1) {
141 public void addNotify() {
144 rw.SetForceMakeCurrent();
148 public void removeNotify() {
150 super.removeNotify();
153 public synchronized void Render() {
156 if (ren.VisibleActorCount() == 0) {
161 if (windowset == 0) {
162 // set the window id and the active camera
163 cam = ren.GetActiveCamera();
164 if (lightingset == 0) {
166 lgt.SetPosition(cam.GetPosition());
167 lgt.SetFocalPoint(cam.GetFocalPoint());
172 rw.SetSize(getWidth(), getHeight());
175 this.setSize(getWidth(), getHeight());
185 public boolean isWindowSet() {
186 return (this.windowset == 1);
189 public void paint(Graphics g) {
193 public void update(Graphics g) {
197 public void LightFollowCameraOn() {
198 this.LightFollowCamera = 1;
201 public void LightFollowCameraOff() {
202 this.LightFollowCamera = 0;
205 public void InteractionModeRotate() {
206 this.InteractionMode = 1;
209 public void InteractionModeTranslate() {
210 this.InteractionMode = 2;
213 public void InteractionModeZoom() {
214 this.InteractionMode = 3;
217 public void UpdateLight() {
218 lgt.SetPosition(cam.GetPosition());
219 lgt.SetFocalPoint(cam.GetFocalPoint());
222 public void resetCameraClippingRange() {
224 ren.ResetCameraClippingRange();
228 public void resetCamera() {
234 public void mouseClicked(MouseEvent e) {
238 public void mousePressed(MouseEvent e) {
240 if (ren.VisibleActorCount() == 0)
242 rw.SetDesiredUpdateRate(5.0);
245 if ((e.getModifiers() == InputEvent.BUTTON2_MASK) || (e.getModifiers() == (InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK))) {
246 InteractionModeTranslate();
247 } else if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
248 InteractionModeZoom();
250 InteractionModeRotate();
254 public void mouseReleased(MouseEvent e) {
255 rw.SetDesiredUpdateRate(0.01);
258 public void mouseEntered(MouseEvent e) {
262 public void mouseExited(MouseEvent e) {
265 public void mouseMoved(MouseEvent e) {
270 public void mouseDragged(MouseEvent e) {
271 if (ren.VisibleActorCount() == 0)
276 if (this.InteractionMode == 1) {
277 cam.Azimuth(lastX - x);
278 cam.Elevation(y - lastY);
279 cam.OrthogonalizeViewUp();
280 resetCameraClippingRange();
281 if (this.LightFollowCamera == 1) {
282 lgt.SetPosition(cam.GetPosition());
283 lgt.SetFocalPoint(cam.GetFocalPoint());
287 if (this.InteractionMode == 2) {
290 double APoint[] = new double[3];
294 // get the current focal point and position
295 FPoint = cam.GetFocalPoint();
296 PPoint = cam.GetPosition();
298 // calculate the focal depth since we'll be using it a lot
299 ren.SetWorldPoint(FPoint[0], FPoint[1], FPoint[2], 1.0);
300 ren.WorldToDisplay();
301 focalDepth = ren.GetDisplayPoint()[2];
303 APoint[0] = rw.GetSize()[0] / 2.0 + (x - lastX);
304 APoint[1] = rw.GetSize()[1] / 2.0 - (y - lastY);
305 APoint[2] = focalDepth;
306 ren.SetDisplayPoint(APoint);
307 ren.DisplayToWorld();
308 RPoint = ren.GetWorldPoint();
309 if (RPoint[3] != 0.0) {
310 RPoint[0] = RPoint[0] / RPoint[3];
311 RPoint[1] = RPoint[1] / RPoint[3];
312 RPoint[2] = RPoint[2] / RPoint[3];
316 * Compute a translation vector, moving everything 1/2 the distance
317 * to the cursor. (Arbitrary scale factor)
319 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]);
320 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]);
321 resetCameraClippingRange();
324 if (this.InteractionMode == 3) {
326 zoomFactor = Math.pow(1.02, (y - lastY));
327 if (cam.GetParallelProjection() == 1) {
328 cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
330 cam.Dolly(zoomFactor);
331 resetCameraClippingRange();
339 public void mouseWheelMoved(MouseWheelEvent e) {
340 if (ren.VisibleActorCount() == 0 || e.getWheelRotation() == 0)
343 exponent = -10 * e.getWheelRotation() / Math.abs(e.getWheelRotation());
345 zoomFactor = Math.pow(1.02, exponent);
346 if (cam.GetParallelProjection() == 1) {
347 cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
349 cam.Dolly(zoomFactor);
350 resetCameraClippingRange();
355 public void keyTyped(KeyEvent e) {
358 public void keyPressed(KeyEvent e) {
359 if (ren.VisibleActorCount() == 0)
361 char keyChar = e.getKeyChar();
363 if ('r' == keyChar) {
367 if ('u' == keyChar) {
368 pickActor(lastX, lastY);
370 if ('w' == keyChar) {
371 vtkActorCollection ac;
375 ac = ren.GetActors();
377 for (i = 0; i < ac.GetNumberOfItems(); i++) {
378 anActor = ac.GetNextActor();
379 anActor.GetProperty().SetRepresentationToWireframe();
383 if ('s' == keyChar) {
384 vtkActorCollection ac;
388 ac = ren.GetActors();
390 for (i = 0; i < ac.GetNumberOfItems(); i++) {
391 anActor = ac.GetNextActor();
392 anActor.GetProperty().SetRepresentationToSurface();
398 public void HardCopy(String filename, int mag) {
402 vtkWindowToImageFilter w2if = new vtkWindowToImageFilter();
408 vtkTIFFWriter writer = new vtkTIFFWriter();
409 writer.SetInputConnection(w2if.GetOutputPort());
410 writer.SetFileName(filename);
416 public void pickActor(int x, int y) {
418 vtkPropPicker picker = new vtkPropPicker();
421 picker.PickProp(x, rw.GetSize()[1] - y, ren);
424 if (picker.GetActor() != null)
425 System.out.println(picker.GetActor().GetClassName());
428 public void keyReleased(KeyEvent e) {