//************************************************* // 球が3次元空間を跳ねまわるアニメーション //************************************************* /*・・・・・・・・・・・・・・・・・・・・ プログラムに必要なパッケージの取り込み ・・・・・・・・・・・・・・・・・・・・*/ import java.applet.*; import java.awt.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.applet.MainFrame; /*・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・ アプレットの作成 ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・*/ public class AnimationBall extends Applet implements Runnable{ Transform3D transform3D = new Transform3D(); TransformGroup transformGroup = new TransformGroup(); Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); BorderLayout borderLayout = new BorderLayout( ); SimpleUniverse simpleUniverse = new SimpleUniverse( canvas3D ); BranchGroup objRoot = new BranchGroup( ); BoundingSphere boundingSphere = new BoundingSphere(); DirectionalLight directionalLight = new DirectionalLight( ); Appearance appearance = new Appearance( ); Material material = new Material( ); Sphere sphere = new Sphere( 0.1f, Sphere.GENERATE_NORMALS, appearance ); Thread th; float g_i = 0.0f; public AnimationBall( ){ transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); this.setLayout( borderLayout ); this.add( canvas3D, BorderLayout.CENTER ); simpleUniverse.getViewingPlatform( ).setNominalViewingTransform( ); directionalLight.setInfluencingBounds( boundingSphere ); objRoot.addChild( directionalLight ); appearance.setMaterial( material ); transform3D.setTranslation( new Vector3f(0.5f, 0.5f, 0.0f)); transformGroup.setTransform( transform3D ); transformGroup.addChild( sphere ); objRoot.addChild( transformGroup ); simpleUniverse.addBranchGraph( objRoot ); th = new Thread(this); th.start(); } //end of public AnimationBall() /*・・・・・・・・・・・・・・・・・・・・・・・ 作成したクラスファイルの呼び出し ・・・・・・・・・・・・・・・・・・・・・・・*/ public static void main( String[ ] argV ){ AnimationBall animationBall = new AnimationBall( ); MainFrame mainFrame = new MainFrame( animationBall, 500, 500 ); } //end of main // この部分にアニメーションを書く public void run(){ int t; float x = 0.0f; float xf = 0.02f; float y = 0.4f; float yf = 0.02f; float z = 0.0f; float zf = 0.02f; for(t=0; t<4000; t++){ // ○○回繰り返す if(x > 1.0 && xf > 0.0f) xf = -0.02f; if(x < -0.9 && xf < 0.0f) xf = +0.02f; x = x + xf; if(y > 1.0 && yf > 0.0f) yf = -0.02f; if(y < -0.9 && yf < 0.0f) yf = +0.02f; y = y + yf; if(z > 1.2 && zf > 0.0f) zf = -0.02f; if(z < -0.9 && zf < 0.0f) zf = +0.02f; z = z + zf; try { transform3D.setTranslation( new Vector3f(x, y, z)); transformGroup.setTransform( transform3D ); Thread.sleep(10); } catch(InterruptedException e) { } }//end of for(t=0; t<=100; t++) }//end of public void run() }