Archive for 12月 30th, 2012

  1. 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!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+