[vtkusers] Picking problems in Java
Carlos Martinez Burgos
cmarbur at iti.upv.es
Mon Jan 21 06:06:06 EST 2002
Hi all.
Some months ago I sent a mail to the list telling that I had problems with
picking in Java. I send the code as attachment.
Ross Manges answered me with some code. This code uses a
vtkRenderWindowInteractor. I run it and everything is ok. The problem is
that I have to add a GUI to the program and if I add it to the vtkPanel
and show it, two windows appear. One of the GUI and other of the renderer.
I would that the renderer panel was in the same window that the GUI.
Is this resolved on VTK4.0? Why does the interactor open a window? Could
you help me with some code in Java please?
Thanks in advance.
--
----------------------------------------------------------------------
Carlos Martínez Burgos | Instituto Tecnológico de Informática
Ingeniero Informático | Universidad Politécnica de Valencia
Tlf: +34 963877237 | Camí de Vera, S/N
cmarbur at iti.upv.es | 46071 Valencia - Spain
www.iti.upv.es/~cmarbur | www.iti.upv.es
----------------------------------------------------------------------
-------------- next part --------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import vtk.*;
public class PickTest extends JFrame implements KeyListener,MouseListener,MouseMotionListener {
MyVTKPanel panelVTK;
vtkConeSource cone;
vtkOutlineFilter coneOutline;
vtkPolyDataMapper coneMapper,coneOutlineMapper;
vtkActor coneActor,coneOutlineActor;
vtkSphereSource sphere;
vtkOutlineFilter sphereOutline;
vtkPolyDataMapper sphereMapper,sphereOutlineMapper;
vtkActor sphereActor,sphereOutlineActor;
int lastX,lastY;
boolean helpShowed=false;
public PickTest() {
super("Picking test");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
panelVTK=new MyVTKPanel();
panelVTK.setSize(700,700);
panelVTK.LightFollowCameraOn();
panelVTK.getRenderer().SetBackground(1,1,1);
panelVTK.addKeyListener(this);
panelVTK.addMouseListener(this);
panelVTK.addMouseMotionListener(this);
getContentPane().add(panelVTK,BorderLayout.CENTER);
cone=new vtkConeSource();
vtkPolyDataMapper coneMapper=new vtkPolyDataMapper();
coneMapper.SetInput(cone.GetOutput());
coneActor=new vtkActor();
coneActor.SetMapper(coneMapper);
coneOutline=new vtkOutlineFilter();
coneOutline.SetInput(cone.GetOutput());
coneOutlineMapper=new vtkPolyDataMapper();
coneOutlineMapper.SetInput(coneOutline.GetOutput());
coneOutlineActor=new vtkActor();
coneOutlineActor.SetMapper(coneOutlineMapper);
coneOutlineActor.GetProperty().SetColor(0,0,0);
coneOutlineActor.PickableOff();
coneOutlineActor.VisibilityOff();
panelVTK.getRenderer().AddActor(coneActor);
panelVTK.getRenderer().AddActor(coneOutlineActor);
sphere=new vtkSphereSource();
sphereMapper=new vtkPolyDataMapper();
sphereMapper.SetInput(sphere.GetOutput());
sphereActor=new vtkActor();
sphereActor.SetMapper(sphereMapper);
sphereActor.SetPosition(1,0,0);
sphereOutline=new vtkOutlineFilter();
sphereOutline.SetInput(sphere.GetOutput());
sphereOutlineMapper=new vtkPolyDataMapper();
sphereOutlineMapper.SetInput(sphereOutline.GetOutput());
sphereOutlineActor=new vtkActor();
sphereOutlineActor.SetMapper(sphereOutlineMapper);
sphereOutlineActor.GetProperty().SetColor(0,0,0);
sphereOutlineActor.SetPosition(1,0,0);
sphereOutlineActor.PickableOff();
sphereOutlineActor.VisibilityOff();
panelVTK.getRenderer().AddActor(sphereActor);
panelVTK.getRenderer().AddActor(sphereOutlineActor);
}
public void picked() {
vtkPicker picker=new vtkPicker();
System.out.println("Selection point: ("+lastX+","+lastY+")");
picker.Pick(lastX,getSize().getHeight()-lastY,0,panelVTK.getRenderer());
vtkActor actor=picker.GetActor();
if (actor==coneActor) {
pickedCone();
}
else if (actor==sphereActor) {
pickedSphere();
}
}
public void pickedCone() {
if (coneOutlineActor.GetVisibility()==0)
coneOutlineActor.VisibilityOn();
else
coneOutlineActor.VisibilityOff();
}
public void pickedSphere() {
if (sphereOutlineActor.GetVisibility()==0)
sphereOutlineActor.VisibilityOn();
else
sphereOutlineActor.VisibilityOff();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyChar()=='u') {
picked();
}
else if (e.getKeyChar()=='r') {
panelVTK.getCamera().Azimuth(130);
panelVTK.getCamera().OrthogonalizeViewUp();
panelVTK.getLight().SetPosition(panelVTK.getCamera().GetPosition());
panelVTK.getLight().SetFocalPoint(panelVTK.getCamera().GetFocalPoint());
System.out.println("Now point to the cone at left of position (465,350) and pick with key 'u'");
}
else if (e.getKeyChar()=='h') {
System.out.println("1: press the 'r' key to reset the camera.");
System.out.println("2: point to the cone at left of position (465,350)");
System.out.println("3: press the 'u' key to pick the object");
System.out.println("In my computer I get the sphere selected instead of the cone. Do you?");
helpShowed=true;
}
panelVTK.Render();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void mouseEntered(MouseEvent e) {
lastX=e.getX();
lastY=e.getY();
if (helpShowed)
System.out.println("Mouse point: ("+lastX+","+lastY+")");
}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
lastX=e.getX();
lastY=e.getY();
if (helpShowed)
System.out.println("Mouse point: ("+lastX+","+lastY+")");
}
public void mouseClicked(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
lastX=e.getX();
lastY=e.getY();
if (helpShowed)
System.out.println("Mouse point: ("+lastX+","+lastY+")");
}
public static void main(String args[]) {
PickTest frame=new PickTest();
frame.pack();
frame.setVisible(true);
System.out.println("Press the 'h' key to view some instructions.");
}
class MyVTKPanel extends vtkPanel {
public MyVTKPanel() {
super();
}
public vtkCamera getCamera() {
return cam;
}
public vtkLight getLight() {
return lgt;
}
}
}
More information about the vtkusers
mailing list