001/* 002 * Copyright 2010, 2011 Institut Pasteur. 003 * 004 * This file is part of ICY. 005 * 006 * ICY is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * ICY is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with ICY. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package plugins.tutorial.vtk; 020 021import icy.painter.Overlay; 022import icy.painter.VtkPainter; 023import icy.system.thread.ThreadUtil; 024 025import java.awt.event.ActionEvent; 026import java.awt.event.ActionListener; 027 028import javax.swing.Timer; 029 030import vtk.vtkActor; 031import vtk.vtkEarthSource; 032import vtk.vtkPolyDataMapper; 033import vtk.vtkProp; 034 035/** 036 * @author stephane 037 */ 038public class VtkAnimatedEarthPainter extends Overlay implements ActionListener, VtkPainter 039{ 040 // vtk object 041 vtkActor earthActor; 042 double posX, posY, posZ; 043 final Timer timer; 044 045 public VtkAnimatedEarthPainter() 046 { 047 super("VTK earth"); 048 049 init(); 050 posX = posY = posZ = 0; 051 052 // start update timer 053 timer = new Timer(20, this); 054 timer.start(); 055 } 056 057 // init vtk objects 058 private void init() 059 { 060 final vtkEarthSource earth = new vtkEarthSource(); 061 earth.SetOnRatio(earth.GetOnRatioMaxValue()); // ( 1 to 16 ) 062 earth.OutlineOn(); 063 earth.SetRadius(150); 064 065 final vtkPolyDataMapper earthMapper = new vtkPolyDataMapper(); 066 earthMapper.SetInputConnection(earth.GetOutputPort()); 067 068 earthActor = new vtkActor(); 069 earthActor.SetMapper(earthMapper); 070 } 071 072 @Override 073 public void actionPerformed(ActionEvent e) 074 { 075 ThreadUtil.invokeNow(new Runnable() 076 { 077 @Override 078 public void run() 079 { 080 // update position 081 earthActor.SetOrientation(posX++, posY, posZ); 082 } 083 }); 084 085 // notify overlay changed (refresh display) 086 painterChanged(); 087 } 088 089 @Override 090 public vtkProp[] getProps() 091 { 092 return new vtkProp[] {earthActor}; 093 } 094}