[vtkusers] Using VTK with Java with some problems

Carlos Martinez Burgos cmarbur at iti.upv.es
Mon Sep 17 07:33:30 EDT 2001


I have solved he first problem but I still get the same result with the
picks (see below).

I repost the code that shows my problem again. Could you run it and tell
me if this is an error of VTK or my mistake.

In case I couldn't get a solution to my problem, how could I implement a
pick in VTK? I think getting the picking coordinates from java mouse
listeners and testing if the ray from the view point, going through the
pick point, intersects the actors.

Please I need help.

Thanks.

On Wed, 12 Sep 2001, Carlos Martinez Burgos wrote:

> Hi all.
>
> I'm developing an application in Java, using VTK to show some 3D objects.
> I need to develop some functionalities like cutting planes, picking
> objects, camera interaction, parallel projections and so on. Furthermore I
> need to put the render panel on a window with some GUI, like buttons,
> labels, textfields, etc.
>
> I'm having problems to implement this in VTK, perhaps because I'm not a
> VTK guru. The main problems are:
>
> I have extended a JPanel in wich I add the Canvas that shows the
> renderWindow. I have copied some code from vtkPanel and added on this
> class to implement the render function, mouse interaction, etc.
> The problem is that when I add other GUI objects to this JPanel extended
> class I can't see it. I add the canvas to the center using a BorderLayout
> and another JPanel with border and some JLabel to the east. I can see this
> panel. How can I show it? Have somebody done this?
>
> Furthermore, sometimes, when I move the mouse outside the canvas, it turns
> gray like if it doesn't show anything. Do you know the problem?
>
>
> I have a problem with picking too. I implement a KeyListener in wich on a
> keyPressed event I create a vtkPicker and run its pick object with the
> position of the mouse, that is updated in a mouseMoved event. I implement
> a pick method on an object that contains the VTK pipeline to show a
> surface (vtkPolyData, vtkPolyDataMapper and vtkActor). In this method I
> show the outline of this surface (using a vtkOutlineFilter).
>
> The problem is that when I press the key, with the mouse pointing near to
> a surface, but not exactly on it, it is hited many times. I send some code
> to test it. I have run pickCells.tcl sample and this doesn't happen. Do
> you know why this occurs? Is this only a Java bindings problems?
>
>
> If you could answer with some code I'll be gratefull.
>
> Thank you and sorry for my english.
>
>

-- 
----------------------------------------------------------------------
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