Archive for the ‘Java 3D’ Category

  1. Hello, Java 3D(Jasmin) World!

    Posted on 1月 5th, 2013 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Jasmin による Java 3D の使用例となっている。

    ソースコード

    ; Produced by NeoJasminVisitor (tinapoc)
    ; http://tinapoc.sourceforge.net
    ; The original JasminVisitor is part of the BCEL
    ; http://jakarta.apache.org/bcel/
    ; Sun Dec 02 01:53:35 JST 2012
     
    .bytecode 51.0
    .source Hello.java
    .class public Hello
    .super javax/swing/JFrame
     
    .method public static main([Ljava/lang/String;)V
        .limit stack 3
        .limit locals 2
        .var 0 is arg0 [Ljava/lang/String; from Label0 to Label1
     
        Label0:
    .line 12
           0: new Hello
           3: dup
           4: ldc "Hello, World"
           6: invokespecial Hello/<init>(Ljava/lang/String;)V
           9: astore_1
     
        .line 13
          10: aload_1
          11: iconst_1
          12: invokevirtual Hello/setVisible(Z)V
     
        Label1:
    .line 14
          15: return
     
    .end method
     
    .method public <init>(Ljava/lang/String;)V
        .limit stack 3
        .limit locals 4
        .var 0 is this LHello; from Label0 to Label1
        .var 1 is arg0 Ljava/lang/String; from Label0 to Label1
     
        Label0:
    .line 17
           0: aload_0
           1: aload_1
           2: invokespecial javax/swing/JFrame/<init>(Ljava/lang/String;)V
     
        .line 18
           5: aload_0
           6: sipush 640
           9: sipush 480
          12: invokevirtual Hello/setSize(II)V
     
        .line 19
          15: aload_0
          16: iconst_3
          17: invokevirtual Hello/setDefaultCloseOperation(I)V
     
        .line 20
          20: aload_0
          21: invokespecial Hello/createCanvas3D()Ljavax/media/j3d/Canvas3D;
          24: astore_2
     
        .line 21
          25: aload_0
          26: invokevirtual Hello/createSceneGraph()Ljavax/media/j3d/BranchGroup;
          29: astore_3
     
        .line 22
          30: aload_0
          31: aload_2
          32: aload_3
          33: invokespecial Hello/connect(Ljavax/media/j3d/Canvas3D;Ljavax/media/j3d/BranchGroup;)V
     
        Label1:
    .line 23
          36: return
     
    .end method
     
    .method private createCanvas3D()Ljavax/media/j3d/Canvas3D;
        .limit stack 3
        .limit locals 3
        .var 0 is this LHello; from Label0 to Label1
     
        Label0:
    .line 26
           0: aload_0
           1: invokevirtual Hello/getContentPane()Ljava/awt/Container;
           4: new java/awt/BorderLayout
           7: dup
           8: invokespecial java/awt/BorderLayout/<init>()V
          11: invokevirtual java/awt/Container/setLayout(Ljava/awt/LayoutManager;)V
     
        .line 27
          14: invokestatic com/sun/j3d/utils/universe/SimpleUniverse/getPreferredConfiguration()Ljava/awt/GraphicsConfiguration;
          17: astore_1
     
        .line 28
          18: new javax/media/j3d/Canvas3D
          21: dup
          22: aload_1
          23: invokespecial javax/media/j3d/Canvas3D/<init>(Ljava/awt/GraphicsConfiguration;)V
          26: astore_2
     
        .line 29
          27: aload_0
          28: invokevirtual Hello/getContentPane()Ljava/awt/Container;
          31: aload_2
          32: invokevirtual java/awt/Container/add(Ljava/awt/Component;)Ljava/awt/Component;
          35: pop
     
        .line 30
          36: aload_2
     
        Label1:
          37: areturn
     
    .end method
     
    .method public createSceneGraph()Ljavax/media/j3d/BranchGroup;
        .limit stack 7
        .limit locals 5
        .var 0 is this LHello; from Label0 to Label1
     
        Label0:
    .line 34
           0: new javax/media/j3d/BranchGroup
           3: dup
           4: invokespecial javax/media/j3d/BranchGroup/<init>()V
           7: astore_1
     
        .line 35
           8: aload_0
           9: invokespecial Hello/moveTextBack()Ljavax/media/j3d/TransformGroup;
          12: astore_2
     
        .line 36
          13: aload_0
          14: invokespecial Hello/createSpinner()Ljavax/media/j3d/TransformGroup;
          17: astore_3
     
        .line 37
          18: new javax/media/j3d/RotationInterpolator
          21: dup
          22: new javax/media/j3d/Alpha
          25: dup
          26: iconst_m1
          27: ldc2_w 4000
          30: invokespecial javax/media/j3d/Alpha/<init>(IJ)V
          33: aload_3
          34: new javax/media/j3d/Transform3D
          37: dup
          38: invokespecial javax/media/j3d/Transform3D/<init>()V
          41: fconst_0
          42: ldc 6.2831855
          44: invokespecial javax/media/j3d/RotationInterpolator/<init>(Ljavax/media/j3d/Alpha;Ljavax/media/j3d/TransformGroup;Ljavax/media/j3d/Transform3D;FF)V
          47: astore 4
     
        .line 38
          49: aload 4
          51: new javax/media/j3d/BoundingSphere
          54: dup
          55: invokespecial javax/media/j3d/BoundingSphere/<init>()V
          58: invokevirtual javax/media/j3d/RotationInterpolator/setSchedulingBounds(Ljavax/media/j3d/Bounds;)V
     
        .line 39
          61: aload_3
          62: aload 4
          64: invokevirtual javax/media/j3d/TransformGroup/addChild(Ljavax/media/j3d/Node;)V
     
        .line 40
          67: aload_1
          68: aload_2
          69: invokevirtual javax/media/j3d/BranchGroup/addChild(Ljavax/media/j3d/Node;)V
     
        .line 41
          72: aload_2
          73: aload_3
          74: invokevirtual javax/media/j3d/TransformGroup/addChild(Ljavax/media/j3d/Node;)V
     
        .line 42
          77: aload_3
          78: aload_0
          79: invokespecial Hello/createTextShape()Ljavax/media/j3d/Shape3D;
          82: invokevirtual javax/media/j3d/TransformGroup/addChild(Ljavax/media/j3d/Node;)V
     
        .line 43
          85: aload_0
          86: aload_2
          87: invokespecial Hello/setLighting(Ljavax/media/j3d/TransformGroup;)V
     
        .line 44
          90: aload_1
     
        Label1:
          91: areturn
     
    .end method
     
    .method private createSpinner()Ljavax/media/j3d/TransformGroup;
        .limit stack 2
        .limit locals 2
        .var 0 is this LHello; from Label0 to Label1
     
        Label0:
    .line 48
           0: new javax/media/j3d/TransformGroup
           3: dup
           4: invokespecial javax/media/j3d/TransformGroup/<init>()V
           7: astore_1
     
        .line 49
           8: aload_1
           9: bipush 18
          11: invokevirtual javax/media/j3d/TransformGroup/setCapability(I)V
     
        .line 50
          14: aload_1
     
        Label1:
          15: areturn
     
    .end method
     
    .method private moveTextBack()Ljavax/media/j3d/TransformGroup;
        .limit stack 6
        .limit locals 2
        .var 0 is this LHello; from Label0 to Label1
     
        Label0:
    .line 54
           0: new javax/media/j3d/Transform3D
           3: dup
           4: invokespecial javax/media/j3d/Transform3D/<init>()V
           7: astore_1
     
        .line 55
           8: aload_1
           9: new javax/vecmath/Vector3f
          12: dup
          13: fconst_0
          14: fconst_0
          15: ldc -10.0
          17: invokespecial javax/vecmath/Vector3f/<init>(FFF)V
          20: invokevirtual javax/media/j3d/Transform3D/setTranslation(Ljavax/vecmath/Vector3f;)V
     
        .line 56
          23: new javax/media/j3d/TransformGroup
          26: dup
          27: aload_1
          28: invokespecial javax/media/j3d/TransformGroup/<init>(Ljavax/media/j3d/Transform3D;)V
     
        Label1:
          31: areturn
     
    .end method
     
    .method private createTextShape()Ljavax/media/j3d/Shape3D;
        .limit stack 7
        .limit locals 5
        .var 0 is this LHello; from Label0 to Label1
     
        Label0:
    .line 60
           0: new javax/media/j3d/Appearance
           3: dup
           4: invokespecial javax/media/j3d/Appearance/<init>()V
           7: astore_1
     
        .line 61
           8: aload_1
           9: new javax/media/j3d/Material
          12: dup
          13: invokespecial javax/media/j3d/Material/<init>()V
          16: invokevirtual javax/media/j3d/Appearance/setMaterial(Ljavax/media/j3d/Material;)V
     
        .line 62
          19: new javax/media/j3d/Font3D
          22: dup
          23: new java/awt/Font
          26: dup
          27: ldc "MS Pゴシック"
          29: iconst_0
          30: iconst_1
          31: invokespecial java/awt/Font/<init>(Ljava/lang/String;II)V
          34: new javax/media/j3d/FontExtrusion
          37: dup
          38: invokespecial javax/media/j3d/FontExtrusion/<init>()V
          41: invokespecial javax/media/j3d/Font3D/<init>(Ljava/awt/Font;Ljavax/media/j3d/FontExtrusion;)V
          44: astore_2
     
        .line 63
          45: new javax/media/j3d/Text3D
          48: dup
          49: aload_2
          50: new java/lang/String
          53: dup
          54: ldc "Hello, Java 3D World!"
          56: invokespecial java/lang/String/<init>(Ljava/lang/String;)V
          59: invokespecial javax/media/j3d/Text3D/<init>(Ljavax/media/j3d/Font3D;Ljava/lang/String;)V
          62: astore_3
     
        .line 64
          63: aload_3
          64: iconst_0
          65: invokevirtual javax/media/j3d/Text3D/setAlignment(I)V
     
        .line 65
          68: new javax/media/j3d/Shape3D
          71: dup
          72: invokespecial javax/media/j3d/Shape3D/<init>()V
          75: astore 4
     
        .line 66
          77: aload 4
          79: aload_3
          80: invokevirtual javax/media/j3d/Shape3D/setGeometry(Ljavax/media/j3d/Geometry;)V
     
        .line 67
          83: aload 4
          85: aload_1
          86: invokevirtual javax/media/j3d/Shape3D/setAppearance(Ljavax/media/j3d/Appearance;)V
     
        .line 68
          89: aload 4
     
        Label1:
          91: areturn
     
    .end method
     
    .method private setLighting(Ljavax/media/j3d/TransformGroup;)V
        .limit stack 6
        .limit locals 3
        .var 0 is this LHello; from Label0 to Label1
        .var 1 is arg0 Ljavax/media/j3d/TransformGroup; from Label0 to Label1
     
        Label0:
    .line 72
           0: new javax/media/j3d/DirectionalLight
           3: dup
           4: invokespecial javax/media/j3d/DirectionalLight/<init>()V
           7: astore_2
     
        .line 73
           8: aload_2
           9: new javax/media/j3d/BoundingSphere
          12: dup
          13: invokespecial javax/media/j3d/BoundingSphere/<init>()V
          16: invokevirtual javax/media/j3d/DirectionalLight/setInfluencingBounds(Ljavax/media/j3d/Bounds;)V
     
        .line 74
          19: aload_2
          20: new javax/vecmath/Vector3f
          23: dup
          24: fconst_0
          25: fconst_0
          26: ldc -1.0
          28: invokespecial javax/vecmath/Vector3f/<init>(FFF)V
          31: invokevirtual javax/media/j3d/DirectionalLight/setDirection(Ljavax/vecmath/Vector3f;)V
     
        .line 75
          34: aload_2
          35: new javax/vecmath/Color3f
          38: dup
          39: fconst_1
          40: fconst_1
          41: fconst_1
          42: invokespecial javax/vecmath/Color3f/<init>(FFF)V
          45: invokevirtual javax/media/j3d/DirectionalLight/setColor(Ljavax/vecmath/Color3f;)V
     
        .line 76
          48: aload_1
          49: aload_2
          50: invokevirtual javax/media/j3d/TransformGroup/addChild(Ljavax/media/j3d/Node;)V
     
        Label1:
    .line 77
          53: return
     
    .end method
     
    .method private connect(Ljavax/media/j3d/Canvas3D;Ljavax/media/j3d/BranchGroup;)V
        .limit stack 3
        .limit locals 4
        .var 0 is this LHello; from Label0 to Label1
        .var 1 is arg0 Ljavax/media/j3d/Canvas3D; from Label0 to Label1
        .var 2 is arg1 Ljavax/media/j3d/BranchGroup; from Label0 to Label1
     
        Label0:
    .line 80
           0: new com/sun/j3d/utils/universe/SimpleUniverse
           3: dup
           4: aload_1
           5: invokespecial com/sun/j3d/utils/universe/SimpleUniverse/<init>(Ljavax/media/j3d/Canvas3D;)V
           8: astore_3
     
        .line 81
           9: aload_3
          10: invokevirtual com/sun/j3d/utils/universe/SimpleUniverse/getViewingPlatform()Lcom/sun/j3d/utils/universe/ViewingPlatform;
          13: invokevirtual com/sun/j3d/utils/universe/ViewingPlatform/setNominalViewingTransform()V
     
        .line 82
          16: aload_3
          17: aload_2
          18: invokevirtual com/sun/j3d/utils/universe/SimpleUniverse/addBranchGraph(Ljavax/media/j3d/BranchGroup;)V
     
        Label1:
    .line 83
          21: return
     
    .end method

    コンパイル&実行方法

    C:¥> java -jar jasmin.jar Hello.j
    C:¥> javaw Hello

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, Java 3D(Fantom) World!

    Posted on 12月 30th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Fantom による Java 3D の使用例となっている。

    ソースコード

    using [java] java.awt
    using [java] javax.swing
    using [java] javax.media.j3d
    using [java] javax.vecmath
    using [java] com.sun.j3d.utils.universe
     
    using concurrent
     
    class Hello
    {
        Void main()
        {
            frame := JFrame("Hello, World")
            {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
                setSize(640, 480)
            }
            canvas3D := createCanvas3D(frame)
            scene := createSceneGraph()
            connect(canvas3D, scene)
            frame.setVisible(true)
     
            Actor.sleep(Duration.maxVal)
        }
        Canvas3D createCanvas3D( JFrame frame )
        {
            frame.getContentPane().setLayout( BorderLayout() )
            config := SimpleUniverse.getPreferredConfiguration()
            canvas3D := Canvas3D( config )
            frame.getContentPane().add( canvas3D )
            return canvas3D
        }
     
        BranchGroup createSceneGraph()
        {
            objRoot := BranchGroup()
            mover := moveTextBack()
            spinner := createSpinner()
            rotator := RotationInterpolator(Alpha(-1, 4000), spinner, Transform3D(), 0.0f, 3.14159265f * 2)
            rotator.setSchedulingBounds( BoundingSphere() )
            spinner.addChild(rotator)
            objRoot.addChild(mover)
            mover.addChild(spinner)
            spinner.addChild( createTextShape())
            setLighting(mover)
            return objRoot
        }
     
        TransformGroup createSpinner()
        {
            spinner := TransformGroup()
            spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)
            return spinner
        }
     
        TransformGroup moveTextBack()
        {
            transform3D := Transform3D()
            transform3D.setTranslation( Vector3f(0.0f, 0.0f, -10.0f))
            return TransformGroup(transform3D)
        }
     
        Shape3D createTextShape()
        {
            textAppear := Appearance()
            textAppear.setMaterial(Material())
            font3D := Font3D( Font("MS Pゴシック", Font.PLAIN, 1), FontExtrusion())
            textGeom := Text3D(font3D, "Hello, Java 3D World!")
            textGeom.setAlignment(Text3D.ALIGN_CENTER)
            textShape := Shape3D()
            textShape.setGeometry(textGeom)
            textShape.setAppearance(textAppear)
            return textShape
        }
     
        Void setLighting( TransformGroup objMove )
        {
            light := DirectionalLight()
            light.setInfluencingBounds(BoundingSphere())
            light.setDirection(Vector3f(0.0f,0.0f,-1.0f))
            light.setColor(Color3f(1.0f, 1.0f, 1.0f))
            objMove.addChild(light)
        }
     
        Void connect( Canvas3D canvas3D, BranchGroup scene )
        {
            universe := SimpleUniverse(canvas3D)
            universe.getViewingPlatform().setNominalViewingTransform()
            universe.addBranchGraph(scene)
        }
    }

    実行方法

    C:¥> fan Hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, Java 3D(Rhino) World!

    Posted on 12月 24th, 2012 by cx20

    Java 3D(Rhino)

    Java 3DJava による 3D グラフィックライブラリである。
    以下は Rhino による Java 3D の使用例となっている。

    ソースコード

    importPackage(java.awt);
    importPackage(javax.swing);
    importPackage(javax.media.j3d);
    importPackage(javax.vecmath);
    importPackage(com.sun.j3d.utils.universe);
     
    var frame = new JFrame();
    frame.setTitle("Hello, World");
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setSize(640,480);
     
    var canvas3D = createCanvas3D(frame);
    var scene = createSceneGraph();
    connect(canvas3D, scene);
     
    frame.setVisible(true);
     
    function createCanvas3D( frame ) {
        frame.getContentPane().setLayout(new BorderLayout());
        var config = SimpleUniverse.getPreferredConfiguration();
        var canvas3D = new Canvas3D(config);
        frame.getContentPane().add(canvas3D);
        return canvas3D;
    }
     
    function createSceneGraph() {
        var objRoot = new BranchGroup();
        var mover = moveTextBack();
        var spinner = createSpinner();
        var rotator = new RotationInterpolator(new Alpha(-1, 4000), spinner, new Transform3D(), 0.0, Math.PI * 2);
        rotator.setSchedulingBounds( new BoundingSphere() );
        spinner.addChild(rotator);
        objRoot.addChild(mover);
        mover.addChild(spinner);
        spinner.addChild( createTextShape());
        setLighting(mover);
        return objRoot;
    }
     
    function createSpinner() {
        var spinner = new TransformGroup();
        spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        return spinner;
    }
     
    function moveTextBack() {
        var transform3D = new Transform3D();
        transform3D.setTranslation(new Vector3f(0.0, 0.0, -10.0));
        return new TransformGroup(transform3D);
    }
     
    function createTextShape() {
        var textAppear = new Appearance();
        textAppear.setMaterial(new Material());
        var font3D = new Font3D(new Font("MS Pゴシック", Font.PLAIN, 1), new FontExtrusion());
        var textGeom = new Text3D(font3D, new String("Hello, Java 3D World!"));
        textGeom.setAlignment(Text3D.ALIGN_CENTER);
        var textShape = new Shape3D();
        textShape.setGeometry(textGeom);
        textShape.setAppearance(textAppear);
        return textShape;
    }
     
    function setLighting( objMove ) {
        var light = new DirectionalLight();
        light.setInfluencingBounds(new BoundingSphere());
        light.setDirection(new Vector3f(0.0,0.0,-1.0));
        light.setColor(new Color3f(1.0, 1.0, 1.0));
        objMove.addChild(light);
    }
     
    function connect( canvas3D, scene ) {
        var universe = new SimpleUniverse(canvas3D);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(scene);
    }
     
    while(1) {java.lang.Thread.sleep(1000);}

    実行方法

    C:¥> jrunscript Hello.js

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. Hello, Java 3D(Tcl/Java) World!

    Posted on 12月 18th, 2012 by cx20

    Java 3D(Tcl/Java)

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Tcl/Java による Java 3D の使用例となっている。

    ソースコード

    package require java
     
    java::import java.awt.BorderLayout
    java::import java.awt.GraphicsConfiguration
    java::import java.awt.Font
    java::import javax.media.j3d.Canvas3D
    java::import javax.media.j3d.BranchGroup
    java::import javax.media.j3d.Transform3D
    java::import javax.media.j3d.TransformGroup
    java::import javax.media.j3d.Appearance
    java::import javax.media.j3d.Material
    java::import javax.media.j3d.Font3D
    java::import javax.media.j3d.FontExtrusion
    java::import javax.media.j3d.Text3D
    java::import javax.media.j3d.Shape3D
    java::import javax.media.j3d.DirectionalLight
    java::import javax.media.j3d.RotationInterpolator
    java::import javax.media.j3d.Alpha
    java::import javax.media.j3d.BoundingSphere
    java::import javax.swing.JFrame
    java::import javax.vecmath.Vector3f
    java::import javax.vecmath.Color3f
    java::import com.sun.j3d.utils.universe.SimpleUniverse
     
    proc createCanvas3D { frame } {
        $frame setLayout [java::new BorderLayout]
        set config [java::call SimpleUniverse getPreferredConfiguration]
        set canvas3D [java::new Canvas3D $config]
        $frame add $canvas3D
        return $canvas3D
    }
     
    proc createSceneGraph {} {
        set objRoot [java::new BranchGroup]
        set mover [moveTextBack]
        set spinner [createSpinner]
        set rotator [java::new RotationInterpolator [java::new Alpha -1 4000] $spinner [java::new Transform3D] 0.0  [expr [java::field Math PI] * 2]]
        $rotator setSchedulingBounds [java::new BoundingSphere]
        $spinner addChild $rotator
        $objRoot addChild $mover
        $mover addChild $spinner
        $spinner addChild [createTextShape]
        setLighting $mover
        return $objRoot
    }
     
    proc createSpinner {} {
        set spinner [java::new TransformGroup]
        $spinner setCapability [java::field TransformGroup ALLOW_TRANSFORM_WRITE]
        return $spinner
    }
     
    proc moveTextBack {} {
        set transform3D [java::new Transform3D]
        $transform3D setTranslation [java::new Vector3f 0.0 0.0 -10.0]
        return [java::new TransformGroup $transform3D]
    }
     
    proc createTextShape {} {
        set textAppear [java::new Appearance]
        $textAppear setMaterial [java::new Material]
        set font3D [java::new Font3D [java::new Font "MS Pゴシック" [java::field Font PLAIN] 1] [java::new FontExtrusion]]
        set textGeom [java::new Text3D $font3D [java::new String "Hello, Java 3D World!"]]
        $textGeom setAlignment [java::field Text3D ALIGN_CENTER]
        set textShape [java::new Shape3D]
        $textShape setGeometry $textGeom
        $textShape setAppearance $textAppear
        return $textShape
    }
     
    proc setLighting { objMove } {
        set light [java::new DirectionalLight]
        $light setInfluencingBounds [java::new BoundingSphere]
        $light setDirection [java::new Vector3f 0.0 0.0 -1.0]
        $light setColor [java::new Color3f 1.0 1.0 1.0]
        $objMove addChild $light
    }
     
    proc connect { canvas3D scene } {
        set universe [java::new SimpleUniverse $canvas3D]
        set view [$universe getViewingPlatform]
        [$universe getViewingPlatform] setNominalViewingTransform
        $universe addBranchGraph $scene
    }
     
    set frame [java::new JFrame] 
    $frame setDefaultCloseOperation [java::field JFrame EXIT_ON_CLOSE]
    $frame setTitle "Hello, World"
    $frame setSize 640 480
    set canvas3D [createCanvas3D $frame]
    set scene [createSceneGraph]
    connect $canvas3D $scene
    $frame setVisible true
     
    vwait done

    実行方法

    C:¥> jaclsh hello.tcl

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  5. Hello, Java 3D(BeanShell) World!

    Posted on 12月 12th, 2012 by cx20

    Java 3D(BeanShell)

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は BeanShell による Java 3D の使用例となっている。

    ソースコード

    import javax.media.j3d.*;
    import javax.swing.JFrame;
    import javax.vecmath.Vector3f;
    import javax.vecmath.Color3f;
    import java.awt.BorderLayout;
    import java.awt.GraphicsConfiguration;
    import java.awt.Font;
    import com.sun.j3d.utils.universe.SimpleUniverse;
     
    class Hello extends JFrame {
        Hello( title ) {
            super(title);
            setSize(640, 480);
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            canvas3D = createCanvas3D();
            scene = createSceneGraph();
            connect(canvas3D, scene);
        }
     
        Canvas3D createCanvas3D() {
            getContentPane().setLayout(new BorderLayout());
            config = SimpleUniverse.getPreferredConfiguration();
            canvas3D = new Canvas3D(config);
            getContentPane().add(canvas3D);
            return canvas3D;
        }
     
        BranchGroup createSceneGraph() {
            objRoot = new BranchGroup();
            mover = moveTextBack();
            spinner = createSpinner();
            rotator = new RotationInterpolator(new Alpha(-1, 4000), spinner, new Transform3D(), 0.0f, (float)Math.PI * 2);
            rotator.setSchedulingBounds( new BoundingSphere() );
            spinner.addChild(rotator);
            objRoot.addChild(mover);
            mover.addChild(spinner);
            spinner.addChild( createTextShape());
            setLighting(mover);
            return objRoot;
        }
     
        TransformGroup createSpinner() {
            spinner = new TransformGroup();
            spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            return spinner;
        }
     
        TransformGroup moveTextBack() {
            transform3D = new Transform3D();
            transform3D.setTranslation(new Vector3f(0.0f, 0.0f, -10.0f));
            return new TransformGroup(transform3D);
        }
     
        Shape3D createTextShape() {
            textAppear = new Appearance();
            textAppear.setMaterial(new Material());
            font3D = new Font3D(new Font("MS Pゴシック", Font.PLAIN, 1), new FontExtrusion());
            textGeom = new Text3D(font3D, new String("Hello, Java 3D World!"));
            textGeom.setAlignment(Text3D.ALIGN_CENTER);
            textShape = new Shape3D();
            textShape.setGeometry(textGeom);
            textShape.setAppearance(textAppear);
            return textShape;
        }
     
        void setLighting(objMove) {
            light = new DirectionalLight();
            light.setInfluencingBounds(new BoundingSphere());
            light.setDirection(new Vector3f(0.0f,0.0f,-1.0f));
            light.setColor(new Color3f(1.0f, 1.0f, 1.0f));
            objMove.addChild(light);
        }
     
        void connect(canvas3D, scene) {
            universe = new SimpleUniverse(canvas3D);
            universe.getViewingPlatform().setNominalViewingTransform();
            universe.addBranchGraph(scene);
        }
    }
     
    frame = new Hello("Hello, World");
    frame.setVisible(true);

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, Java 3D(Clojure) World!

    Posted on 12月 6th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Clojure による Java 3D の使用例となっている。

    ソースコード

    (import 
      (java.awt BorderLayout)
      (java.awt GraphicsConfiguration)
      (java.awt Font)
      (javax.swing JFrame)
      (javax.swing JPanel)
      (javax.media.j3d BranchGroup)
      (javax.media.j3d Canvas3D)
      (javax.media.j3d Transform3D)
      (javax.media.j3d Appearance)
      (javax.media.j3d Material)
      (javax.media.j3d Font3D)
      (javax.media.j3d FontExtrusion)
      (javax.media.j3d Text3D)
      (javax.media.j3d Shape3D)
      (javax.media.j3d DirectionalLight)
      (javax.media.j3d TransformGroup)
      (javax.media.j3d RotationInterpolator)
      (javax.media.j3d Alpha)
      (javax.media.j3d BoundingSphere)
      (javax.vecmath Vector3f)
      (javax.vecmath Color3f)
      (com.sun.j3d.utils.universe SimpleUniverse)
      (com.sun.j3d.utils.universe ViewingPlatform)
      (com.sun.j3d.utils.geometry ColorCube))
     
    (def frame
      (JFrame.))
     
    (defn create-text-shape []
      (let [
        textAppear (Appearance.)
        font (Font. "MS Pゴシック" Font/PLAIN 1 )
        fontEx (FontExtrusion.)
        font3D (Font3D. font fontEx)
        textGeom (Text3D. font3D "Hello, Java 3D World!")
        textShape (Shape3D.)]
     
        (.setMaterial textAppear (Material.))
        (.setAlignment textGeom Text3D/ALIGN_CENTER)
        (.setGeometry textShape textGeom)
        (.setAppearance textShape textAppear)
        textShape))
     
    (defn move-text-back []
      (let [
        transform3D (Transform3D.)
        vector3f (Vector3f. 0.0 0.0 -10.0)]
     
        (.setTranslation transform3D vector3f)
        (TransformGroup. transform3D)))
     
    (defn create-spinner []
      (let [
        spinner (TransformGroup.)]
     
        (.setCapability spinner (TransformGroup/ALLOW_TRANSFORM_WRITE))
        spinner))
     
    (defn set-lighting [objMove]
      (let [
        light (DirectionalLight.)]
     
        (.setInfluencingBounds light (BoundingSphere.))
        (.setDirection light (Vector3f. 0.0 0.0 -1.0))
        (.setColor light (Color3f. 1.0 1.0 1.0))
        (.addChild objMove light)))
     
    (defn create-scene-graph []
      (let [
        objRoot (BranchGroup.)
        mover (move-text-back)
        spinner (create-spinner)
        rotator (RotationInterpolator. (Alpha. -1 4000), spinner, (Transform3D.) 0.0 (* (Math/PI) 2))]
     
        (.addChild spinner (create-text-shape))
        (.addChild mover spinner)
        (.addChild objRoot mover)
        (.setSchedulingBounds rotator (BoundingSphere.))
        (.addChild spinner rotator)
        (set-lighting mover)
        objRoot))
     
    (def canvas
      (Canvas3D. (SimpleUniverse/getPreferredConfiguration)))
     
    (def universe
      (SimpleUniverse. canvas))
     
    (doto universe
      (.addBranchGraph (create-scene-graph)))
     
    (.setNominalViewingTransform (.getViewingPlatform universe))
    (.add (.getContentPane frame) canvas)
     
    (doto frame
      (.setDefaultCloseOperation
        javax.swing.WindowConstants/EXIT_ON_CLOSE)
      (.setSize 640 480)
      (.setTitle "Hello, World")
      (.setVisible true))

    実行方法

    C:¥> clj Hello.clj

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, Java 3D(Jython) World!

    Posted on 11月 30th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Jython による Java 3D の使用例となっている。

    ソースコード

    from java.lang import Math
    from java.awt import BorderLayout
    from java.awt import GraphicsConfiguration
    from java.awt import Font
    from javax.media.j3d import Canvas3D
    from javax.media.j3d import BranchGroup
    from javax.media.j3d import Transform3D
    from javax.media.j3d import TransformGroup
    from javax.media.j3d import Appearance
    from javax.media.j3d import Material
    from javax.media.j3d import Font3D
    from javax.media.j3d import FontExtrusion
    from javax.media.j3d import Text3D
    from javax.media.j3d import Shape3D
    from javax.media.j3d import DirectionalLight
    from javax.media.j3d import RotationInterpolator
    from javax.media.j3d import Alpha
    from javax.media.j3d import BoundingSphere
    from javax.swing import JFrame
    from javax.vecmath import Vector3f
    from javax.vecmath import Color3f
    from com.sun.j3d.utils.universe import SimpleUniverse
     
    class HelloFrame(JFrame):
        def __init__(self, title ):
            self.setTitle(title)
            self.setSize(640, 480)
            self.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
            canvas3D = self.createCanvas3D()
            scene = self.createSceneGraph()
            self.connect(canvas3D, scene)
     
        def createCanvas3D(self):
            self.getContentPane().setLayout(BorderLayout())
            config = SimpleUniverse.getPreferredConfiguration()
            canvas3D = Canvas3D(config)
            self.getContentPane().add(canvas3D)
            return canvas3D
     
        def createSceneGraph(self):
            objRoot = BranchGroup()
            mover = self.moveTextBack()
            spinner = self.createSpinner()
            rotator = RotationInterpolator(Alpha(-1, 4000), spinner, Transform3D(), 0.0, Math.PI * 2)
            rotator.setSchedulingBounds( BoundingSphere() )
            spinner.addChild(rotator)
            objRoot.addChild(mover)
            mover.addChild(spinner)
            spinner.addChild(self.createTextShape())
            self.setLighting(mover)
            return objRoot
     
        def createSpinner(self):
            spinner = TransformGroup()
            spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)
            return spinner
     
        def moveTextBack(self):
            transform3D = Transform3D()
            transform3D.setTranslation(Vector3f(0.0, 0.0, -10.0))
            return TransformGroup(transform3D)
     
        def createTextShape(self):
            textAppear = Appearance()
            textAppear.setMaterial(Material())
            font3D = Font3D(Font("MS Pゴシック", Font.PLAIN, 1), FontExtrusion())
            textGeom = Text3D(font3D, "Hello, Java 3D World!")
            textGeom.setAlignment(Text3D.ALIGN_CENTER)
            textShape = Shape3D()
            textShape.setGeometry(textGeom)
            textShape.setAppearance(textAppear)
            return textShape
     
        def setLighting(self,objMove):
            light = DirectionalLight()
            light.setInfluencingBounds(BoundingSphere())
            light.setDirection(Vector3f(0.0,0.0,-1.0))
            light.setColor(Color3f(1.0, 1.0, 1.0))
            objMove.addChild(light)
     
        def connect(self,canvas3D, scene):
            universe = SimpleUniverse(canvas3D)
            universe.getViewingPlatform().setNominalViewingTransform()
            universe.addBranchGraph(scene)
     
    if __name__ == '__main__':
        frame = HelloFrame("Hello, World")
        frame.setVisible(True)

    実行方法

    C:¥> jython Hello.rb

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  8. Hello, Java 3D(JRuby) World!

    Posted on 11月 24th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は JRuby による Java 3D の使用例となっている。

    ソースコード

    require 'java'
     
    import 'java.awt.BorderLayout'
    import 'java.awt.GraphicsConfiguration'
    import 'java.awt.Font'
    import 'javax.media.j3d.Canvas3D'
    import 'javax.media.j3d.BranchGroup'
    import 'javax.media.j3d.Transform3D'
    import 'javax.media.j3d.TransformGroup'
    import 'javax.media.j3d.Appearance'
    import 'javax.media.j3d.Material'
    import 'javax.media.j3d.Font3D'
    import 'javax.media.j3d.FontExtrusion'
    import 'javax.media.j3d.Text3D'
    import 'javax.media.j3d.Shape3D'
    import 'javax.media.j3d.DirectionalLight'
    import 'javax.media.j3d.RotationInterpolator'
    import 'javax.media.j3d.Alpha'
    import 'javax.media.j3d.BoundingSphere'
    import 'javax.swing.JFrame'
    import 'javax.vecmath.Vector3f'
    import 'javax.vecmath.Color3f'
    import 'com.sun.j3d.utils.universe.SimpleUniverse'
     
    class HelloFrame < JFrame
        def initialize( title )
            super(title)
            self.setSize(640, 480)
            self.setDefaultCloseOperation( JFrame::EXIT_ON_CLOSE )
            @canvas3D = createCanvas3D()
            @scene = createSceneGraph()
            connect(@canvas3D, @scene)
        end
     
        def createCanvas3D()
            getContentPane().setLayout(BorderLayout.new())
            @config = SimpleUniverse.getPreferredConfiguration()
            @canvas3D = Canvas3D.new(@config)
            getContentPane().add(@canvas3D)
            return @canvas3D
        end
     
        def createSceneGraph()
            @objRoot = BranchGroup.new()
            @mover = moveTextBack()
            @spinner = createSpinner()
            @rotator = RotationInterpolator.new(Alpha.new(-1, 4000), @spinner, Transform3D.new(), 0.0, Math::PI * 2)
            @rotator.setSchedulingBounds( BoundingSphere.new() )
            @spinner.addChild(@rotator)
            @objRoot.addChild(@mover)
            @mover.addChild(@spinner)
            @spinner.addChild( createTextShape())
            setLighting(@mover)
            return @objRoot
        end
     
        def createSpinner()
            @spinner = TransformGroup.new()
            @spinner.setCapability(TransformGroup::ALLOW_TRANSFORM_WRITE)
            return @spinner
        end
     
        def moveTextBack() 
            @transform3D = Transform3D.new()
            @transform3D.setTranslation(Vector3f.new(0.0, 0.0, -10.0))
            return TransformGroup.new(@transform3D)
        end
     
        def createTextShape()
            @textAppear = Appearance.new()
            @textAppear.setMaterial(Material.new())
            @font3D = Font3D.new(Font.new("MS Pゴシック", Font::PLAIN, 1), FontExtrusion.new())
            @textGeom = Text3D.new(@font3D, String.new("Hello, Java 3D World!"))
            @textGeom.setAlignment(Text3D::ALIGN_CENTER)
            @textShape = Shape3D.new()
            @textShape.setGeometry(@textGeom)
            @textShape.setAppearance(@textAppear)
            return @textShape
        end
     
        def setLighting(objMove)
            @light = DirectionalLight.new()
            @light.setInfluencingBounds(BoundingSphere.new())
            @light.setDirection(Vector3f.new(0.0,0.0,-1.0))
            @light.setColor(Color3f.new(1.0, 1.0, 1.0))
            objMove.addChild(@light)
        end
     
        def connect(canvas3D, scene)
            @universe = SimpleUniverse.new(canvas3D)
            @universe.getViewingPlatform().setNominalViewingTransform()
            @universe.addBranchGraph(scene)
        end
    end 
     
    @frame = HelloFrame.new("Hello, World")
    @frame.setVisible(true)

    実行方法

    C:¥> jrubyw Hello.rb

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  9. Hello, Java 3D(Scala) World!

    Posted on 11月 18th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Scala による Java 3D の使用例となっている。

    ソースコード

    import javax.media.j3d._
    import javax.swing.JFrame
    import javax.vecmath.Vector3f
    import javax.vecmath.Color3f
    import java.awt.BorderLayout
    import java.awt.GraphicsConfiguration
    import java.awt.Font
    import com.sun.j3d.utils.universe.SimpleUniverse
     
    object Hello {
        def main(args: Array[String]) {
            var frame = new HelloFrame("Hello, World")
            frame.setVisible(true)
        }
    }
     
    class HelloFrame( title: String ) extends JFrame {
        setTitle(title)
        setSize(640, 480)
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
        var canvas3D = createCanvas3D()
        var scene = createSceneGraph()
        connect(canvas3D, scene)
     
        def createCanvas3D(): Canvas3D = {
            getContentPane().setLayout(new BorderLayout())
            var config = SimpleUniverse.getPreferredConfiguration()
            var canvas3D = new Canvas3D(config)
            getContentPane().add(canvas3D)
            return canvas3D
        }
     
        def createSceneGraph(): BranchGroup = {
            var objRoot = new BranchGroup()
            var mover = moveTextBack()
            var spinner = createSpinner()
            var rotator = new RotationInterpolator(new Alpha(-1, 4000), spinner, new Transform3D(), 0.0f, Math.Pi.toFloat * 2.0f)
            rotator.setSchedulingBounds( new BoundingSphere() )
            spinner.addChild(rotator)
            objRoot.addChild(mover)
            mover.addChild(spinner)
            spinner.addChild( createTextShape())
            setLighting(mover)
            return objRoot
        }
     
        def createSpinner(): TransformGroup = {
            var spinner = new TransformGroup()
            spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)
            return spinner
        }
     
        def moveTextBack(): TransformGroup = {
            var transform3D = new Transform3D()
            transform3D.setTranslation(new Vector3f(0.0f, 0.0f, -10.0f))
            return new TransformGroup(transform3D)
        }
     
        def createTextShape(): Shape3D = {
            var textAppear = new Appearance()
            textAppear.setMaterial(new Material())
            var font3D = new Font3D(new Font("MS Pゴシック", Font.PLAIN, 1), new FontExtrusion())
            var textGeom = new Text3D(font3D, new String("Hello, Java 3D World!"))
            textGeom.setAlignment(Text3D.ALIGN_CENTER)
            var textShape = new Shape3D()
            textShape.setGeometry(textGeom)
            textShape.setAppearance(textAppear)
            return textShape
        }
     
        def setLighting(objMove: TransformGroup) {
            var light = new DirectionalLight()
            light.setInfluencingBounds(new BoundingSphere())
            light.setDirection(new Vector3f(0.0f,0.0f,-1.0f))
            light.setColor(new Color3f(1.0f, 1.0f, 1.0f))
            objMove.addChild(light)
        }
     
        def connect(canvas3D: Canvas3D, scene: BranchGroup) {
            var universe = new SimpleUniverse(canvas3D)
            universe.getViewingPlatform().setNominalViewingTransform()
            universe.addBranchGraph(scene)
        }
    }

    実行方法

    C:¥> scalac Hello.scala
    C:¥> scala Hello

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  10. Hello, Java 3D(Groovy) World!

    Posted on 11月 12th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Groovy による Java 3D の使用例となっている。

    ソースコード

    import javax.media.j3d.*
    import javax.swing.JFrame
    import javax.vecmath.Vector3f
    import javax.vecmath.Color3f
    import java.awt.BorderLayout
    import java.awt.GraphicsConfiguration
    import java.awt.Font
    import com.sun.j3d.utils.universe.SimpleUniverse
     
    class Hello extends JFrame {
        static void main(args) {
            def frame = new Hello("Hello, World")
            frame.setVisible(true)
        }
     
        Hello( title ) {
            super(title)
            setSize(640, 480)
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
            def canvas3D = createCanvas3D()
            BranchGroup scene = createSceneGraph()
            connect(canvas3D, scene)
        }
     
        Canvas3D createCanvas3D() {
            getContentPane().setLayout(new BorderLayout())
            GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration()
            def canvas3D = new Canvas3D(config)
            getContentPane().add(canvas3D)
            return canvas3D
        }
     
        BranchGroup createSceneGraph() {
            def objRoot = new BranchGroup()
            def mover = moveTextBack()
            def spinner = createSpinner()
            def rotator = new RotationInterpolator(new Alpha(-1, 4000), spinner, new Transform3D(), 0.0f, (float)Math.PI * 2)
            rotator.setSchedulingBounds( new BoundingSphere() )
            spinner.addChild(rotator)
            objRoot.addChild(mover)
            mover.addChild(spinner)
            spinner.addChild( createTextShape())
            setLighting(mover)
            return objRoot
        }
     
        TransformGroup createSpinner() {
            def spinner = new TransformGroup()
            spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)
            return spinner
        }
     
        TransformGroup moveTextBack() {
            def transform3D = new Transform3D()
            transform3D.setTranslation(new Vector3f(0.0f, 0.0f, -10.0f))
            return new TransformGroup(transform3D)
        }
     
        Shape3D createTextShape() {
            def textAppear = new Appearance()
            textAppear.setMaterial(new Material())
            def font3D = new Font3D(new Font("MS Pゴシック", Font.PLAIN, 1), new FontExtrusion())
            def textGeom = new Text3D(font3D, new String("Hello, Java 3D World!"))
            textGeom.setAlignment(Text3D.ALIGN_CENTER)
            def textShape = new Shape3D()
            textShape.setGeometry(textGeom)
            textShape.setAppearance(textAppear)
            return textShape
        }
     
        void setLighting(objMove) {
            def light = new DirectionalLight()
            light.setInfluencingBounds(new BoundingSphere())
            light.setDirection(new Vector3f(0.0f,0.0f,-1.0f))
            light.setColor(new Color3f(1.0f, 1.0f, 1.0f))
            objMove.addChild(light)
        }
     
        void connect(canvas3D, scene) {
            def universe = new SimpleUniverse(canvas3D)
            universe.getViewingPlatform().setNominalViewingTransform()
            universe.addBranchGraph(scene)
        }
    }

    実行方法

    C:¥> groovyw Hello.groovy

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+