/***************************************/ // // ColorCubeが平面上を跳ねまわる // /***************************************/ /*・・・・・・・・・・・・・・・・・・・・ プログラムに必要なパッケージの取り込み ・・・・・・・・・・・・・・・・・・・・*/ 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 AnimationColorCube 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 ); ColorCube colorCube = new ColorCube(0.2); Thread th; public AnimationColorCube( ){ 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 Vector3d(0.5, 0.5, 0.0)); transformGroup.setTransform( transform3D ); // transformGroup.addChild( sphere ); transformGroup.addChild( colorCube ); objRoot.addChild( transformGroup ); simpleUniverse.addBranchGraph( objRoot ); th = new Thread(this); th.start(); } //end of public AnimationColorCube() /*・・・・・・・・・・・・・・・・・・・・・・・ 作成したクラスファイルの呼び出し ・・・・・・・・・・・・・・・・・・・・・・・*/ public static void main( String[ ] argV ){ AnimationColorCube animationColorCube = new AnimationColorCube( ); MainFrame mainFrame = new MainFrame( animationColorCube, 500, 500 ); } //end of main // この部分にアニメーションを書く public void run(){ int t; double x = 0.0; //X座標 double y = 0.4; //Y座標 double z = 0.0; //Z座標 double xf = 0.02; //移動量Xの計算結果を入れる変数 double yf = 0.02; //移動量Yの計算結果を入れる変数 double zf = 0.02; //移動量Zの計算結果を入れる変数 double DX = 0.02; //移動量X double DY = 0.02; //移動量Y double DZ = 0.02; //移動量Z for(t=0; t<4000; t++){ // ○○回繰り返す if(x > 1.0 && xf > 0.0) xf = -DX; if(x < -0.9 && xf < 0.0) xf = DX; x = x + xf; if(y > 1.0 && yf > 0.0) yf = -DY; if(y < -0.9 && yf < 0.0) yf = +DY; y = y + yf; if(z > 1.2 && zf > 0.0) zf = -DZ; if(z < -0.9 && zf < 0.0) zf = DZ; z = z + zf; try { transform3D.setTranslation( new Vector3d(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() }