Archive for 11月 30th, 2012

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