import java.awt.BorderLayout;

import javax.swing.JFrame;

import vtk.vtkActor;
import vtk.vtkAlgorithmOutput;
import vtk.vtkBoxWidget;
import vtk.vtkCanvas;
import vtk.vtkDataSetMapper;
import vtk.vtkDataSetReader;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkTransform;

public class MyWidget {
	private vtkBoxWidget boxw;
	vtkActor actor;

	public MyWidget() {
		vtkCanvas panel = new vtkCanvas();
		//
		vtkDataSetReader reader = new vtkDataSetReader();
		reader.SetFileName("image.txt");
		reader.Update();
		reader.CloseVTKFile();

		// vtkDataSet data = reader.GetOutput();
		vtkAlgorithmOutput output = reader.GetOutputPort();
		vtkDataSetMapper mapper = new vtkDataSetMapper();
		actor = new vtkActor();

		mapper.SetInputConnection(output);

		boxw = new vtkBoxWidget();

		actor.SetMapper(mapper);

		panel.setInteractorStyle(new vtkInteractorStyleTrackballCamera());

		boxw.SetInteractor(panel.getIren());
		boxw.SetPlaceFactor(1.5);

		boxw.SetProp3D(actor);
		boxw.PlaceWidget();

		boxw.AddObserver("InteractionEvent", this, "myCallBack");

		boxw.On();

		panel.GetRenderer().AddActor(actor);

		//
		JFrame f = new JFrame("Widget Test");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(500, 500);
		f.setLocationRelativeTo(null);
		f.getContentPane().setLayout(new BorderLayout());
		f.getContentPane().add(panel, BorderLayout.CENTER);
		f.setVisible(true);

	}

	public void myCallBack() {
		vtkTransform t = new vtkTransform();
		boxw.GetTransform(t);
		boxw.GetProp3D().SetUserTransform(t);
		System.out.println("callback: " + t);
	}

	public static void main(String[] args) {
		new MyWidget();
	}
}
