[2229] | 1 | // |
---|
| 2 | // This example shows how to add an observer to a Java program. It extends |
---|
| 3 | // the Step1/Java/Cone.java Java example (see that example for information on |
---|
| 4 | // the basic setup). |
---|
| 5 | // |
---|
| 6 | // VTK uses a command/observer design pattern. That is, observers watch for |
---|
| 7 | // particular events that any vtkObject (or subclass) may invoke on |
---|
| 8 | // itself. For example, the vtkRenderer invokes a "StartEvent" as it begins |
---|
| 9 | // to render. Here we add an observer that invokes a command when this event |
---|
| 10 | // is observed. |
---|
| 11 | // |
---|
| 12 | // |
---|
| 13 | // Show how to add an observer to the Cone example |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | // we import the vtk wrapped classes forst |
---|
| 17 | import vtk.*; |
---|
| 18 | |
---|
| 19 | // then we define our class |
---|
| 20 | public class Cone2 { |
---|
| 21 | // in the static contructor we load in the native code |
---|
| 22 | // The libraries must be in your path to work |
---|
| 23 | static { |
---|
| 24 | System.loadLibrary("vtkCommonJava"); |
---|
| 25 | System.loadLibrary("vtkFilteringJava"); |
---|
| 26 | System.loadLibrary("vtkIOJava"); |
---|
| 27 | System.loadLibrary("vtkImagingJava"); |
---|
| 28 | System.loadLibrary("vtkGraphicsJava"); |
---|
| 29 | System.loadLibrary("vtkRenderingJava"); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | // Define the callback |
---|
| 33 | public void myCallback() |
---|
| 34 | { |
---|
| 35 | System.out.println("Starting a render"); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // now the main program |
---|
| 39 | public static void main (String []args) { |
---|
| 40 | // |
---|
| 41 | // Now we create the pipeline as usual, see Cone.java in Step1 for details |
---|
| 42 | // |
---|
| 43 | vtkConeSource cone = new vtkConeSource(); |
---|
| 44 | cone.SetHeight( 3.0 ); |
---|
| 45 | cone.SetRadius( 1.0 ); |
---|
| 46 | cone.SetResolution( 10 ); |
---|
| 47 | |
---|
| 48 | vtkPolyDataMapper coneMapper = new vtkPolyDataMapper(); |
---|
| 49 | coneMapper.SetInput( cone.GetOutput() ); |
---|
| 50 | vtkActor coneActor = new vtkActor(); |
---|
| 51 | coneActor.SetMapper( coneMapper ); |
---|
| 52 | |
---|
| 53 | vtkRenderer ren1 = new vtkRenderer(); |
---|
| 54 | ren1.AddActor( coneActor ); |
---|
| 55 | ren1.SetBackground( 0.1, 0.2, 0.4 ); |
---|
| 56 | |
---|
| 57 | // Add the observer here, the first argument is the event name |
---|
| 58 | // the second argument is the instance to invoke the method on |
---|
| 59 | // the third argument is which method to invoke |
---|
| 60 | Cone2 me = new Cone2(); |
---|
| 61 | ren1.AddObserver("StartEvent",me,"myCallback"); |
---|
| 62 | |
---|
| 63 | // setup the window |
---|
| 64 | vtkRenderWindow renWin = new vtkRenderWindow(); |
---|
| 65 | renWin.AddRenderer( ren1 ); |
---|
| 66 | renWin.SetSize( 300, 300 ); |
---|
| 67 | |
---|
| 68 | // |
---|
| 69 | // now we loop over 360 degreeees and render the cone each time |
---|
| 70 | // |
---|
| 71 | int i; |
---|
| 72 | for (i = 0; i < 360; ++i) |
---|
| 73 | { |
---|
| 74 | // render the image |
---|
| 75 | renWin.Render(); |
---|
| 76 | // rotate the active camera by one degree |
---|
| 77 | ren1.GetActiveCamera().Azimuth( 1 ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|