3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.ComponentAdapter;
6 import java.awt.event.ComponentEvent;
7 import java.awt.event.InputEvent;
8 import java.awt.event.KeyEvent;
9 import java.awt.event.KeyListener;
10 import java.awt.event.MouseEvent;
11 import java.awt.event.MouseListener;
12 import java.awt.event.MouseMotionListener;
14 import javax.swing.Timer;
17 * Java AWT component that encapsulate vtkGenericRenderWindowInteractor,
18 * vtkPlaneWidget, vtkBoxWidget and extends vtkPanel
23 public class vtkCanvas extends vtkPanel implements MouseListener, MouseMotionListener, KeyListener {
24 private static final long serialVersionUID = 1L;
25 protected vtkGenericRenderWindowInteractor iren = new vtkGenericRenderWindowInteractor();
26 protected Timer timer = new Timer(10, new DelayAction());
27 protected int ctrlPressed = 0;
28 protected int shiftPressed = 0;
29 protected vtkPlaneWidget pw = new vtkPlaneWidget();
30 protected vtkBoxWidget bw = new vtkBoxWidget();
33 // load up hybrid for 3d widgets
34 vtkNativeLibrary.HYBRID.LoadLibrary();
35 vtkNativeLibrary.WIDGETS.LoadLibrary();
38 public void Delete() {
50 public vtkCanvas(vtkRenderWindow renwin) {
55 protected void Initialize() {
56 iren.SetRenderWindow(rw);
57 iren.TimerEventResetsTimerOff();
58 iren.AddObserver("CreateTimerEvent", this, "StartTimer");
59 iren.AddObserver("DestroyTimerEvent", this, "DestroyTimer");
60 iren.SetSize(200, 200);
61 iren.ConfigureEvent();
62 pw.AddObserver("EnableEvent", this, "BeginPlaneInteraction");
63 bw.AddObserver("EnableEvent", this, "BeginBoxInteraction");
64 pw.SetKeyPressActivationValue('l');
65 bw.SetKeyPressActivationValue('b');
66 pw.SetInteractor(iren);
67 bw.SetInteractor(iren);
69 addComponentListener(new ComponentAdapter() {
70 public void componentResized(ComponentEvent event) {
71 // The Canvas is being resized, get the new size
72 int width = getWidth();
73 int height = getHeight();
74 setSize(width, height);
78 ren.SetBackground(0.0, 0.0, 0.0);
80 // Setup same interactor style than vtkPanel
81 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
82 iren.SetInteractorStyle(style);
85 public void StartTimer() {
86 if (timer.isRunning())
89 timer.setRepeats(true);
93 public void DestroyTimer() {
94 if (timer.isRunning())
99 * Replace by getRenderWindowInteractor()
102 public vtkGenericRenderWindowInteractor getIren() {
106 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
110 public void setInteractorStyle(vtkInteractorStyle style) {
111 iren.SetInteractorStyle(style);
114 public void addToPlaneWidget(vtkProp3D prop) {
119 public void addToBoxWidget(vtkProp3D prop) {
124 public void BeginPlaneInteraction() {
125 System.out.println("Plane widget begin interaction");
128 public void BeginBoxInteraction() {
129 System.out.println("Box widget begin interaction");
132 public void setSize(int x, int y) {
134 if (windowset == 1) {
138 iren.ConfigureEvent();
143 public void mouseClicked(MouseEvent e) {
146 public void mousePressed(MouseEvent e) {
147 if (ren.VisibleActorCount() == 0)
150 rw.SetDesiredUpdateRate(5.0);
154 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
155 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
157 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
159 if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
160 iren.LeftButtonPressEvent();
161 } else if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
162 iren.MiddleButtonPressEvent();
163 } else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
164 iren.RightButtonPressEvent();
169 public void mouseReleased(MouseEvent e) {
170 rw.SetDesiredUpdateRate(0.01);
172 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
173 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
175 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
177 if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
179 iren.LeftButtonReleaseEvent();
183 if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
185 iren.MiddleButtonReleaseEvent();
189 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
191 iren.RightButtonReleaseEvent();
196 public void mouseEntered(MouseEvent e) {
198 iren.SetEventInformationFlipY(e.getX(), e.getY(), 0, 0, '0', 0, "0");
202 public void mouseExited(MouseEvent e) {
203 iren.SetEventInformationFlipY(e.getX(), e.getY(), 0, 0, '0', 0, "0");
207 public void mouseMoved(MouseEvent e) {
211 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
212 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
214 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
217 iren.MouseMoveEvent();
221 public void mouseDragged(MouseEvent e) {
222 if (ren.VisibleActorCount() == 0)
225 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
226 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
228 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
231 iren.MouseMoveEvent();
237 public void keyTyped(KeyEvent e) {
240 public void keyPressed(KeyEvent e) {
241 if (ren.VisibleActorCount() == 0)
243 char keyChar = e.getKeyChar();
245 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
246 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
248 iren.SetEventInformationFlipY(lastX, lastY, ctrlPressed, shiftPressed, keyChar, 0, String.valueOf(keyChar));
251 iren.KeyPressEvent();
256 public void keyReleased(KeyEvent e) {
259 private class DelayAction implements ActionListener {
260 public void actionPerformed(ActionEvent evt) {