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;
13 import java.awt.event.MouseWheelEvent;
14 import java.awt.event.MouseWheelListener;
16 import javax.swing.Timer;
19 * Java AWT component that encapsulate vtkGenericRenderWindowInteractor,
20 * vtkPlaneWidget, vtkBoxWidget and extends vtkPanel
25 public class vtkCanvas extends vtkPanel implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {
26 private static final long serialVersionUID = 1L;
27 protected vtkGenericRenderWindowInteractor iren = new vtkGenericRenderWindowInteractor();
28 protected Timer timer = new Timer(10, new DelayAction());
29 protected int ctrlPressed = 0;
30 protected int shiftPressed = 0;
31 protected vtkPlaneWidget pw = new vtkPlaneWidget();
32 protected vtkBoxWidget bw = new vtkBoxWidget();
34 public void Delete() {
46 public vtkCanvas(vtkRenderWindow renwin) {
51 protected void Initialize() {
52 iren.SetRenderWindow(rw);
53 iren.TimerEventResetsTimerOff();
54 iren.AddObserver("CreateTimerEvent", this, "StartTimer");
55 iren.AddObserver("DestroyTimerEvent", this, "DestroyTimer");
56 iren.SetSize(200, 200);
57 iren.ConfigureEvent();
58 pw.AddObserver("EnableEvent", this, "BeginPlaneInteraction");
59 bw.AddObserver("EnableEvent", this, "BeginBoxInteraction");
60 pw.SetKeyPressActivationValue('l');
61 bw.SetKeyPressActivationValue('b');
62 pw.SetInteractor(iren);
63 bw.SetInteractor(iren);
65 addComponentListener(new ComponentAdapter() {
66 public void componentResized(ComponentEvent event) {
67 // The Canvas is being resized, get the new size
68 int width = getWidth();
69 int height = getHeight();
70 setSize(width, height);
74 ren.SetBackground(0.0, 0.0, 0.0);
76 // Setup same interactor style than vtkPanel
77 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
78 iren.SetInteractorStyle(style);
81 public void StartTimer() {
82 if (timer.isRunning())
85 timer.setRepeats(true);
89 public void DestroyTimer() {
90 if (timer.isRunning())
95 * Replace by getRenderWindowInteractor()
98 public vtkGenericRenderWindowInteractor getIren() {
102 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
106 public void setInteractorStyle(vtkInteractorStyle style) {
107 iren.SetInteractorStyle(style);
110 public void addToPlaneWidget(vtkProp3D prop) {
115 public void addToBoxWidget(vtkProp3D prop) {
120 public void BeginPlaneInteraction() {
121 System.out.println("Plane widget begin interaction");
124 public void BeginBoxInteraction() {
125 System.out.println("Box widget begin interaction");
128 public void setSize(int x, int y) {
130 if (windowset == 1) {
134 iren.ConfigureEvent();
139 public void mouseClicked(MouseEvent e) {
142 public void mousePressed(MouseEvent e) {
143 if (ren.VisibleActorCount() == 0)
146 rw.SetDesiredUpdateRate(5.0);
150 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
151 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
153 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
155 if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
156 iren.LeftButtonPressEvent();
157 } else if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
158 iren.MiddleButtonPressEvent();
159 } else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
160 iren.RightButtonPressEvent();
165 public void mouseReleased(MouseEvent e) {
166 rw.SetDesiredUpdateRate(0.01);
168 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
169 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
171 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
173 if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
175 iren.LeftButtonReleaseEvent();
179 if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
181 iren.MiddleButtonReleaseEvent();
185 if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
187 iren.RightButtonReleaseEvent();
192 public void mouseEntered(MouseEvent e) {
194 iren.SetEventInformationFlipY(e.getX(), e.getY(), 0, 0, '0', 0, "0");
198 public void mouseExited(MouseEvent e) {
199 iren.SetEventInformationFlipY(e.getX(), e.getY(), 0, 0, '0', 0, "0");
203 public void mouseMoved(MouseEvent e) {
207 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
208 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
210 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
213 iren.MouseMoveEvent();
217 public void mouseDragged(MouseEvent e) {
218 if (ren.VisibleActorCount() == 0)
221 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
222 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
224 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
227 iren.MouseMoveEvent();
233 public void mouseWheelMoved(MouseWheelEvent e) {
234 if (ren.VisibleActorCount() == 0)
237 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
238 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
241 if (e.getWheelRotation() > 0) {
242 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
243 iren.MouseWheelBackwardEvent();
245 else if (e.getWheelRotation() < 0) {
246 iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
247 iren.MouseWheelForwardEvent();
254 public void keyTyped(KeyEvent e) {
257 public void keyPressed(KeyEvent e) {
258 if (ren.VisibleActorCount() == 0)
260 char keyChar = e.getKeyChar();
262 ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
263 shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
265 iren.SetEventInformationFlipY(lastX, lastY, ctrlPressed, shiftPressed, keyChar, 0, String.valueOf(keyChar));
268 iren.KeyPressEvent();
273 public void keyReleased(KeyEvent e) {
276 private class DelayAction implements ActionListener {
277 public void actionPerformed(ActionEvent evt) {