アットウィキロゴ

0921j

import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;

import com.sun.j3d.utils.universe.*;

public class game0921 extends Frame
{
   // main Method
   public static void main(String[] args)
   {   new game0921();
   }

   public game0921(){   // JFrame の初期化
       setSize(300,300);
      
       // Java3D関係の設定
       GraphicsConfiguration g_config = SimpleUniverse.getPreferredConfiguration();
       Canvas3D canvas = new Canvas3D(g_config);
       add([[canvas]]);

       // SimpleUniverseを生成
       SimpleUniverse universe = new SimpleUniverse(canvas);
       universe.getViewingPlatform().setNominalViewingTransform();

       // 立方体の線を生成
       BranchGroup scene = CreateScene();
       universe.addBranchGraph(scene);

       // JFrame を表示
       setVisible(true);
addWindowListener(new stopwin());


  }

   // 立方体の線を生成
   private BranchGroup CreateScene()
   {   BranchGroup objRoot = new BranchGroup();

       // 立方体の頂点座標を定義
       Point3d[] vertices =
       {  new Point3d(-0.5, 0.5, 0.5),  new Point3d(-0.5, -0.5, 0.5),  //左上前  左下前
          new Point3d(0.5, -0.5, 0.5),  new Point3d(0.5, 0.5, 0.5),    //右下前  右上前
          new Point3d(-0.5, 0.5, -0.5), new Point3d(-0.5, -0.5, -0.5), //左上奥  左下奥
          new Point3d(0.5, -0.5, -0.5), new Point3d(0.5, 0.5, -0.5)  };//右下奥  右上奥

       // 立方体を三角ポリゴンで定義
       int[] indices = { 0, 1, 2,  0, 2, 3,    4, 5, 1,  4, 1, 0,
                         4, 0, 3,  4, 3, 7,    3, 2, 6,  3, 6, 7,
                         7, 6, 5,  7, 5, 4,    1, 5, 6,  1, 6, 2 };

       // 色の定義
       Color3f[] colors = { new Color3f(1.0f, 0.0f, 0.0f),    // red
                            new Color3f(0.0f, 1.0f, 0.0f),    // green
                            new Color3f(0.0f, 0.0f, 1.0f),    // blue
                            new Color3f(0.0f, 1.0f, 1.0f),    // cyan
                            new Color3f(1.0f, 0.0f, 1.0f),    // magenta
                            new Color3f(1.0f, 1.0f, 0.0f) };  // yellow

       // 色の定義を使って、立方体の面の色を定義
       int[] colorIndices = { 0, 0, 0,  0, 0, 0,    1, 1, 1,  1, 1, 1,
                              2, 2, 2,  2, 2, 2,    3, 3, 3,  3, 3, 3,
                              4, 4, 4,  4, 4, 4,    5, 5, 5,  5, 5, 5 };

       // 立方体を生成
       IndexedTriangleArray geometry = new IndexedTriangleArray(
           vertices.length, GeometryArray.COORDINATES | GeometryArray.COLOR_3, indices.length);
       geometry.setCoordinates(0, vertices);
       geometry.setCoordinateIndices(0, indices);
       geometry.setColors(0, colors);
       geometry.setColorIndices(0, colorIndices);

       // 回転を設定
       Shape3D shape = new Shape3D(geometry);
       Transform3D t3d = new Transform3D();
       t3d.setRotation( new AxisAngle4d(0.57, 0.57, -0.57, Math.PI / 4.0));
       TransformGroup trans = new TransformGroup(t3d);

       // BranchGroup に登録
       trans.addChild(shape);
       objRoot.addChild(trans);

       return objRoot;
   }
   

class stopwin extends WindowAdapter{
  public void windowClosing(WindowEvent we){System.exit(0);} 
}




}
最終更新:2011年03月04日 00:44